After six years of development, the first stable release of the PHPStan 1.0 static analyzer has taken place, which allows you to find errors in PHP code without executing it and using unit tests. The project code is written in PHP and distributed under the MIT license.
The analyzer provides 10 levels of verification, in which each subsequent level expands the capabilities of the previous one and provides more stringent checks:
Examples of underlying problems identified:
- Existence of classes used in instanceof, catch, typehints and other language constructs.
- Existence and availability of called methods and functions, as well as the number of arguments passed.
- Checking if a method returns data with the same type as defined in the return statement.
- Existence and visibility of the properties being accessed, and validation of the declared and actual data types used in the properties.
- The correctness of the number of parameters passed to sprintf/printf calls in the string formatter.
- The existence of variables, taking into account blocks formed by branch operators and loops.
- Useless typecasts (like "(string) 'foo'") and strict checks ("===" and "!==") on data with different types and operands always returning false.
Key innovations of PHPStan 1.0:
- The check level "9" is implemented, which checks the use of the "mixed" type, which is intended for organizing the reception of parameters with different types by the function. The ninth level exposes the unsafe uses of "mixed", such as passing values ββof type "mixed" to another type, calling methods of type "mixed", and accessing its properties because they might not exist.
- Controlling return value identity checks for identical function calls using the @phpstan-pure and @phpstan-impure annotations.
- Type analysis in try-catch-finally constructs using @throws annotations.
- Identification of defined but unused internal (private) properties, methods and constants.
- Passing incompatible callbacks to array functions such as array_map and usort.
- Type inspection for missing typehint annotations.
- Type declarations have been made compatible with PHPDocs, allowing PHPDocs to use types from error messages.
Source: opennet.ru