|
| 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 | +Makes [PHPStan](https://phpstan.org) smarter about Nette code. Install, and it just works — more precise types, fewer false positives. |
| 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 | +**Precise return types** — narrows return types of `Strings::match()`, `matchAll()`, `split()`, `Helpers::falseToNull()`, and `Expect::array()` based on the arguments you pass. |
| 45 | + |
| 46 | +**Removes `|false` from PHP functions** — functions like `getcwd`, `json_encode`, `preg_split`, and [many more](extension-php.neon) return `string|false` even though `false` is unrealistic on modern systems. |
| 47 | + |
| 48 | +**Assert type narrowing** — PHPStan understands type guarantees after `Tester\Assert` calls like `notNull()`, `type()`, `true()`, etc. |
| 49 | + |
| 50 | +**False positive suppression** — silences known PHPStan false positives in Nette patterns (arrow functions passed as `void` callbacks, runtime type validation closures). |
| 51 | + |
| 52 | + <!----> |
| 53 | + |
| 54 | +### Type Assertion Testing Helper |
| 55 | + |
| 56 | +For Nette package developers: `TypeAssert` lets you verify type inference in tests using [Nette Tester](https://tester.nette.org): |
| 57 | + |
| 58 | +```php |
| 59 | +use Nette\PHPStan\Tester\TypeAssert; |
| 60 | + |
| 61 | +TypeAssert::assertTypes(__DIR__ . '/data/types.php', [__DIR__ . '/../extension.neon']); |
| 62 | +``` |
| 63 | + |
| 64 | +The data file uses `assertType()` from PHPStan: |
| 65 | + |
| 66 | +```php |
| 67 | +use function PHPStan\Testing\assertType; |
| 68 | + |
| 69 | +assertType('non-empty-string', getcwd()); |
| 70 | +assertType('string', Normalizer::normalize('foo')); |
| 71 | +``` |
| 72 | + |
| 73 | + <!----> |
| 74 | + |
| 75 | +## [Support Me](https://github.com/sponsors/dg) |
| 76 | + |
| 77 | +Do you like Nette? Are you looking forward to the new features? |
| 78 | + |
| 79 | +[](https://github.com/sponsors/dg) |
| 80 | + |
| 81 | +Thank you! |
0 commit comments