-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract introduced
Property Hooks
-related functionalities into thei…
…r own rules
- Loading branch information
1 parent
c263971
commit d6cad22
Showing
14 changed files
with
241 additions
and
24 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
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
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,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PropertyHooks; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\ClassPropertyNode; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<ClassPropertyNode> | ||
*/ | ||
final class NonEmptyPropertyHookRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassPropertyNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->getClassReflection()->isInterface()) { | ||
return []; | ||
} | ||
|
||
if (!$this->phpVersion->supportsPropertyHooksInInterfaces() || !$node->isPropertyHook()) { | ||
return []; | ||
} | ||
|
||
if ($node->areHooksBodiesEmpty()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Property hook must not be empty.') | ||
->nonIgnorable() | ||
->identifier('propertyHook.nonEmpty') | ||
->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,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PropertyHooks; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\ClassPropertyNode; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<ClassPropertyNode> | ||
*/ | ||
final class NonPublicPropertyHookRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassPropertyNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->getClassReflection()->isInterface()) { | ||
return []; | ||
} | ||
|
||
if (!$this->phpVersion->supportsPropertyHooksInInterfaces() || !$node->isPropertyHook()) { | ||
return []; | ||
} | ||
|
||
if ($node->isPublic()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Property hook must be public.') | ||
->nonIgnorable() | ||
->identifier('propertyHook.nonPublic') | ||
->build(), | ||
]; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
tests/PHPStan/Rules/Properties/NonEmptyPropertyHookRuleTest.php
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\Properties; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\PropertyHooks\NonEmptyPropertyHookRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<NonEmptyPropertyHookRule> | ||
*/ | ||
class NonEmptyPropertyHookRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NonEmptyPropertyHookRule(new PhpVersion(PHP_VERSION_ID)); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 80400) { | ||
$this->markTestSkipped('This test requires at least PHP 8.4.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/non-empty-property-hook.php'], [ | ||
[ | ||
'Property hook must not be empty.', | ||
7, | ||
], | ||
[ | ||
'Property hook must not be empty.', | ||
14, | ||
], | ||
]); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
tests/PHPStan/Rules/Properties/NonPublicPropertyHookRuleTest.php
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\Properties; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\PropertyHooks\NonPublicPropertyHookRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<NonPublicPropertyHookRule> | ||
*/ | ||
class NonPublicPropertyHookRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NonPublicPropertyHookRule(new PhpVersion(PHP_VERSION_ID)); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 80400) { | ||
$this->markTestSkipped('This test requires at least PHP 8.4.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/non-public-property-hook.php'], [ | ||
[ | ||
'Property hook must be public.', | ||
7, | ||
], | ||
[ | ||
'Property hook must be public.', | ||
9, | ||
], | ||
]); | ||
} | ||
|
||
} |
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
26 changes: 26 additions & 0 deletions
26
tests/PHPStan/Rules/Properties/data/non-empty-property-hook.php
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,26 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace NonEmptyPropertyHook; | ||
|
||
interface HelloWorld | ||
{ | ||
public string $name { | ||
get { | ||
return $this->name; | ||
} | ||
set; | ||
} | ||
|
||
public string $surname { | ||
get; | ||
set { | ||
$this->name = $value; | ||
} | ||
} | ||
|
||
public string $middleName { get ; } | ||
|
||
public string $title { set; } | ||
|
||
public string $familyName { get; set ;} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/PHPStan/Rules/Properties/data/non-public-property-hook.php
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,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace NonPublicPropertyHook; | ||
|
||
interface HelloWorld | ||
{ | ||
private string $firstName { get; } | ||
|
||
protected string $lastName { get; } | ||
|
||
public string $fullName { get; } | ||
} |