-
Notifications
You must be signed in to change notification settings - Fork 473
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
7042805
commit 02b2e3d
Showing
5 changed files
with
131 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,58 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
class ReadOnlyClassRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
if (!$classReflection->isReadOnly()) { | ||
return []; | ||
} | ||
if ($classReflection->isAnonymous()) { | ||
if ($this->phpVersion->supportsReadOnlyAnonymousClasses()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Anonymous readonly classes are supported only on PHP 8.3 and later.') | ||
->identifier('classConstant.nativeTypeNotSupported') | ||
->nonIgnorable() | ||
->build(), | ||
]; | ||
} | ||
|
||
if ($this->phpVersion->supportsReadOnlyClasses()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Readonly classes are supported only on PHP 8.2 and later.') | ||
->identifier('classConstant.nativeTypeNotSupported') | ||
->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,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule as TRule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<ReadOnlyClassRule> | ||
*/ | ||
class ReadOnlyClassRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): TRule | ||
{ | ||
return new ReadOnlyClassRule(self::getContainer()->getByType(PhpVersion::class)); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$errors = []; | ||
if (PHP_VERSION_ID < 80200) { | ||
$errors = [ | ||
[ | ||
'Readonly classes are supported only on PHP 8.2 and later.', | ||
5, | ||
], | ||
]; | ||
} elseif (PHP_VERSION_ID < 80300) { | ||
$errors = [ | ||
[ | ||
'Anonymous readonly classes are supported only on PHP 8.3 and later.', | ||
15, | ||
], | ||
]; | ||
} | ||
$this->analyse([__DIR__ . '/data/readonly-class.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,20 @@ | ||
<?php // lint >= 8.3 | ||
|
||
namespace ReadonlyClass; | ||
|
||
readonly class Foo | ||
{ | ||
|
||
} | ||
|
||
class Bar | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
$c = new readonly class () { | ||
|
||
}; | ||
} | ||
|
||
} |