Skip to content

Commit 5390561

Browse files
committed
added readme
1 parent 76afb3a commit 5390561

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

readme.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# PHPStan extensions for Nette libraries
2+
3+
![Nette PHPStan Rules](https://github.com/user-attachments/assets/c231ed47-a413-4dd2-81ef-83e52c080427)
4+
5+
[![Downloads this Month](https://img.shields.io/packagist/dm/nette/phpstan-rules.svg)](https://packagist.org/packages/nette/phpstan-rules)
6+
[![Tests](https://github.com/nette/phpstan-rules/workflows/Tests/badge.svg?branch=master)](https://github.com/nette/phpstan-rules/actions)
7+
[![Latest Stable Version](https://poser.pugx.org/nette/phpstan-rules/v/stable)](https://github.com/nette/phpstan-rules/releases)
8+
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](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+
[![Buy me a coffee](https://files.nette.org/icons/donation-3.svg)](https://github.com/sponsors/dg)
104+
105+
Thank you!

0 commit comments

Comments
 (0)