Skip to content

Commit

Permalink
UnsetCastRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 13, 2020
1 parent 32ca405 commit 56471f6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rules:
- PHPStan\Rules\Arrays\DuplicateKeysInLiteralArraysRule
- PHPStan\Rules\Arrays\EmptyArrayItemRule
- PHPStan\Rules\Arrays\OffsetAccessWithoutDimForReadingRule
- PHPStan\Rules\Cast\UnsetCastRule
- PHPStan\Rules\Classes\ClassConstantRule
- PHPStan\Rules\Classes\DuplicateDeclarationRule
- PHPStan\Rules\Classes\ExistingClassesInClassImplementsRule
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public function supportsParameterTypeWidening(): bool
return $this->versionId >= 70200;
}

public function supportsUnsetCast(): bool
{
return $this->versionId < 80000;
}

}
40 changes: 40 additions & 0 deletions src/Rules/Cast/UnsetCastRule.php
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(),
];
}

}
53 changes: 53 additions & 0 deletions tests/PHPStan/Rules/Cast/UnsetCastRuleTest.php
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);
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Cast/data/unset-cast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace UnsetCast;

function ($a): void {
$null = (unset) $a;
};

0 comments on commit 56471f6

Please sign in to comment.