File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919 - PHPStan\Rules\Arrays\DuplicateKeysInLiteralArraysRule
2020 - PHPStan\Rules\Arrays\EmptyArrayItemRule
2121 - PHPStan\Rules\Arrays\OffsetAccessWithoutDimForReadingRule
22+ - PHPStan\Rules\Cast\UnsetCastRule
2223 - PHPStan\Rules\Classes\ClassConstantRule
2324 - PHPStan\Rules\Classes\DuplicateDeclarationRule
2425 - PHPStan\Rules\Classes\ExistingClassesInClassImplementsRule
Original file line number Diff line number Diff line change @@ -86,4 +86,9 @@ public function supportsParameterTypeWidening(): bool
8686 return $ this ->versionId >= 70200 ;
8787 }
8888
89+ public function supportsUnsetCast (): bool
90+ {
91+ return $ this ->versionId < 80000 ;
92+ }
93+
8994}
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \Rules \Cast ;
4+
5+ use PhpParser \Node ;
6+ use PHPStan \Analyser \Scope ;
7+ use PHPStan \Php \PhpVersion ;
8+ use PHPStan \Rules \Rule ;
9+ use PHPStan \Rules \RuleErrorBuilder ;
10+
11+ /**
12+ * @implements Rule<Node\Expr\Cast\Unset_>
13+ */
14+ class UnsetCastRule implements Rule
15+ {
16+
17+ private PhpVersion $ phpVersion ;
18+
19+ public function __construct (PhpVersion $ phpVersion )
20+ {
21+ $ this ->phpVersion = $ phpVersion ;
22+ }
23+
24+ public function getNodeType (): string
25+ {
26+ return Node \Expr \Cast \Unset_::class;
27+ }
28+
29+ public function processNode (Node $ node , Scope $ scope ): array
30+ {
31+ if ($ this ->phpVersion ->supportsUnsetCast ()) {
32+ return [];
33+ }
34+
35+ return [
36+ RuleErrorBuilder::message ('The (unset) cast is no longer supported in PHP 8.0 and later. ' )->nonIgnorable ()->build (),
37+ ];
38+ }
39+
40+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \Rules \Cast ;
4+
5+ use PHPStan \Php \PhpVersion ;
6+ use PHPStan \Rules \Rule ;
7+ use PHPStan \Testing \RuleTestCase ;
8+
9+ /**
10+ * @extends RuleTestCase<UnsetCastRule>
11+ */
12+ class UnsetCastRuleTest extends RuleTestCase
13+ {
14+
15+ /** @var int */
16+ private $ phpVersion ;
17+
18+ protected function getRule (): Rule
19+ {
20+ return new UnsetCastRule (new PhpVersion ($ this ->phpVersion ));
21+ }
22+
23+ public function dataRule (): array
24+ {
25+ return [
26+ [
27+ 70400 ,
28+ [],
29+ ],
30+ [
31+ 80000 ,
32+ [
33+ [
34+ 'The (unset) cast is no longer supported in PHP 8.0 and later. ' ,
35+ 6 ,
36+ ],
37+ ],
38+ ],
39+ ];
40+ }
41+
42+ /**
43+ * @dataProvider dataRule
44+ * @param int $phpVersion
45+ * @param mixed[] $errors
46+ */
47+ public function testRule (int $ phpVersion , array $ errors ): void
48+ {
49+ $ this ->phpVersion = $ phpVersion ;
50+ $ this ->analyse ([__DIR__ . '/data/unset-cast.php ' ], $ errors );
51+ }
52+
53+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace UnsetCast ;
4+
5+ function ($ a ): void {
6+ $ null = (unset ) $ a ;
7+ };
You can’t perform that action at this time.
0 commit comments