-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32ca405
commit 56471f6
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Cast; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\Cast\Unset_> | ||
*/ | ||
class UnsetCastRule implements Rule | ||
{ | ||
|
||
private PhpVersion $phpVersion; | ||
|
||
public function __construct(PhpVersion $phpVersion) | ||
{ | ||
$this->phpVersion = $phpVersion; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Cast\Unset_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->phpVersion->supportsUnsetCast()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('The (unset) cast is no longer supported in PHP 8.0 and later.')->nonIgnorable()->build(), | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Cast; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<UnsetCastRule> | ||
*/ | ||
class UnsetCastRuleTest extends RuleTestCase | ||
{ | ||
|
||
/** @var int */ | ||
private $phpVersion; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new UnsetCastRule(new PhpVersion($this->phpVersion)); | ||
} | ||
|
||
public function dataRule(): array | ||
{ | ||
return [ | ||
[ | ||
70400, | ||
[], | ||
], | ||
[ | ||
80000, | ||
[ | ||
[ | ||
'The (unset) cast is no longer supported in PHP 8.0 and later.', | ||
6, | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataRule | ||
* @param int $phpVersion | ||
* @param mixed[] $errors | ||
*/ | ||
public function testRule(int $phpVersion, array $errors): void | ||
{ | ||
$this->phpVersion = $phpVersion; | ||
$this->analyse([__DIR__ . '/data/unset-cast.php'], $errors); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace UnsetCast; | ||
|
||
function ($a): void { | ||
$null = (unset) $a; | ||
}; |