|
| 1 | +# PHPStan extensions for Nette libraries |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +[](https://packagist.org/packages/nette/phpstan-rules) |
| 6 | +[](https://github.com/nette/phpstan-rules/actions) |
| 7 | +[](https://github.com/nette/phpstan-rules/releases) |
| 8 | +[](https://github.com/nette/phpstan-rules/blob/master/license.md) |
| 9 | + |
| 10 | + <!----> |
| 11 | + |
| 12 | +<h3> |
| 13 | + |
| 14 | +Provides custom type extensions and error suppression rules used when analysing Nette libraries with [PHPStan](https://phpstan.org). |
| 15 | + |
| 16 | +</h3> |
| 17 | + |
| 18 | + <!----> |
| 19 | + |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Install via Composer: |
| 24 | + |
| 25 | +```shell |
| 26 | +composer require --dev nette/phpstan-rules |
| 27 | +``` |
| 28 | + |
| 29 | +Requirements: PHP 8.1 or higher and PHPStan 2.1+. |
| 30 | + |
| 31 | +If you use [phpstan/extension-installer](https://github.com/phpstan/extension-installer), the extension is registered automatically. Otherwise add to your `phpstan.neon`: |
| 32 | + |
| 33 | +```neon |
| 34 | +includes: |
| 35 | + - vendor/nette/phpstan-rules/extension.neon |
| 36 | +``` |
| 37 | + |
| 38 | + <!----> |
| 39 | + |
| 40 | +## What's Included |
| 41 | + |
| 42 | + <!----> |
| 43 | + |
| 44 | +### Narrow Return Types |
| 45 | + |
| 46 | +Removes `|false` from return types of native PHP functions and methods where `false` is trivial or outdated. For example, `getcwd()` returns `string|false`, but on modern systems `false` is unrealistic. This extension narrows it to `string`. |
| 47 | + |
| 48 | +Covered functions include `getcwd`, `json_encode`, `preg_match`, `preg_split`, `hash`, `explode`, `array_combine`, and [many more](extension-narrowReturnType.neon). |
| 49 | + |
| 50 | +```php |
| 51 | +// Without extension: string|false |
| 52 | +// With extension: non-empty-string |
| 53 | +$cwd = getcwd(); |
| 54 | +``` |
| 55 | + |
| 56 | + <!----> |
| 57 | + |
| 58 | +### Type Validation Call Ignore |
| 59 | + |
| 60 | +Suppresses `expr.resultUnused` for the runtime type validation pattern commonly used in Nette: |
| 61 | + |
| 62 | +```php |
| 63 | +/** @param string[] $items */ |
| 64 | +public function setItems(array $items): void |
| 65 | +{ |
| 66 | + (function (string ...$items) {})(...$items); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +PHPStan normally reports the closure call result as unused. This extension recognizes the pattern (empty closure body, all parameters variadic with type hints) and ignores the error. |
| 71 | + |
| 72 | + <!----> |
| 73 | + |
| 74 | +### Type Assertion Testing Helper |
| 75 | + |
| 76 | +The package also provides `Nette\PHPStan\Tester\TypeAssert` for testing type inference in Nette packages using [Nette Tester](https://tester.nette.org): |
| 77 | + |
| 78 | +```php |
| 79 | +use Nette\PHPStan\Tester\TypeAssert; |
| 80 | + |
| 81 | +// Verify assertType() calls in a data file |
| 82 | +TypeAssert::assertTypes(__DIR__ . '/data/types.php', [__DIR__ . '/../extension.neon']); |
| 83 | + |
| 84 | +// Verify PHPStan reports no errors on a file |
| 85 | +TypeAssert::assertNoErrors(__DIR__ . '/data/clean.php', [__DIR__ . '/../extension.neon']); |
| 86 | +``` |
| 87 | + |
| 88 | +The data file uses `assertType()` from PHPStan: |
| 89 | + |
| 90 | +```php |
| 91 | +use function PHPStan\Testing\assertType; |
| 92 | + |
| 93 | +assertType('non-empty-string', getcwd()); |
| 94 | +assertType('string', Normalizer::normalize('foo')); |
| 95 | +``` |
| 96 | + |
| 97 | + <!----> |
| 98 | + |
| 99 | +## [Support Me](https://github.com/sponsors/dg) |
| 100 | + |
| 101 | +Do you like Nette? Are you looking forward to the new features? |
| 102 | + |
| 103 | +[](https://github.com/sponsors/dg) |
| 104 | + |
| 105 | +Thank you! |
0 commit comments