Error Identifier: typeAlias.circular
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
/**
* @phpstan-type RecursiveAlias RecursiveAlias[]
*/
class Foo
{
}
Why is it reported? #
The type alias references itself, creating a circular definition that cannot be resolved. PHPStan needs to be able to fully resolve all type aliases to perform type analysis. A type alias that directly or indirectly refers back to itself creates an infinite loop in resolution.
This also applies to mutually recursive aliases:
<?php declare(strict_types = 1);
/**
* @phpstan-type AliasA AliasB
* @phpstan-type AliasB AliasA
*/
class Bar
{
}
How to fix it #
Break the circular reference by using a concrete type instead of self-referencing:
<?php declare(strict_types = 1);
/**
- * @phpstan-type RecursiveAlias RecursiveAlias[]
+ * @phpstan-type RecursiveAlias array<int, mixed>
*/
class Foo
{
}
For tree-like structures, use a class or interface type instead of a type alias:
<?php declare(strict_types = 1);
-/**
- * @phpstan-type TreeNode array{value: int, children: TreeNode[]}
- */
-class Foo
+/**
+ * @phpstan-type TreeNode array{value: int, children: list<TreeNodeClass>}
+ */
+class Foo
{
}
How to ignore this error #
You can use the identifier typeAlias.circular to ignore this error using a comment:
// @phpstan-ignore typeAlias.circular
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: typeAlias.circular