Skip to content

Commit e8ec2db

Browse files
committed
[explicit] Add NoProtectedClassStmtRule
1 parent 64fc9e9 commit e8ec2db

File tree

9 files changed

+203
-1
lines changed

9 files changed

+203
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ abstract class AbstractClass
245245

246246
<br>
247247

248+
### NoProtectedClassStmtRule
249+
250+
Avoid protected class stmts as they yield unexpected behavior. Use clear interface contract instead
251+
252+
```yaml
253+
rules:
254+
- Symplify\PHPStanRules\Rules\Explicit\NoProtectedClassStmtRule
255+
```
256+
257+
<br>
258+
248259
### ForbiddenArrayMethodCallRule
249260

250261
Array method calls [$this, "method"] are not allowed. Use explicit method instead to help PhpStorm, PHPStan and Rector understand your code

src/Enum/MethodName.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,18 @@ final class MethodName
1111
*/
1212
public const INVOKE = '__invoke';
1313

14+
/**
15+
* @var string
16+
*/
1417
public const CONSTRUCTOR = '__construct';
18+
19+
/**
20+
* @var string
21+
*/
22+
public const SET_UP = 'setUp';
23+
24+
/**
25+
* @var string
26+
*/
27+
public const TEAR_DOWN = 'tearDown';
1528
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Rules\Explicit;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
9+
use PhpParser\Node\Stmt\ClassConst;
10+
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\Property;
12+
use PHPStan\Analyser\Scope;
13+
use PHPStan\Node\InClassNode;
14+
use PHPStan\Reflection\ClassReflection;
15+
use PHPStan\Rules\Rule;
16+
use PHPStan\Rules\RuleError;
17+
use PHPStan\Rules\RuleErrorBuilder;
18+
use Symplify\PHPStanRules\Enum\MethodName;
19+
20+
/**
21+
* @implements Rule<InClassNode>
22+
*
23+
* @see \Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule\NoProtectedClassStmtRuleTest
24+
*/
25+
final class NoProtectedClassStmtRule implements Rule
26+
{
27+
/**
28+
* @var string
29+
*/
30+
public const ERROR_MESSAGE = 'Avoid protected class stmts as they yield unexpected behavior. Use clear interface contract instead';
31+
32+
public function getNodeType(): string
33+
{
34+
return InClassNode::class;
35+
}
36+
37+
/**
38+
* @param InClassNode $node
39+
* @return RuleError[]
40+
*/
41+
public function processNode(Node $node, Scope $scope): array
42+
{
43+
$class = $node->getOriginalNode();
44+
if (! $class instanceof Class_) {
45+
return [];
46+
}
47+
48+
// skip abstract classes for onw
49+
if ($class->isAbstract()) {
50+
return [];
51+
}
52+
53+
$ruleErrors = [];
54+
55+
foreach ($class->stmts as $classStmt) {
56+
if (! $classStmt instanceof ClassMethod && ! $classStmt instanceof ClassConst && ! $classStmt instanceof Property) {
57+
continue;
58+
}
59+
60+
// skip test one
61+
if ($this->shouldSkipClassMethod($classStmt, $scope)) {
62+
continue;
63+
}
64+
65+
if (! $classStmt->isProtected()) {
66+
continue;
67+
}
68+
69+
$ruleErrors[] = RuleErrorBuilder::message(self::ERROR_MESSAGE)
70+
->line($classStmt->getStartLine())
71+
->identifier('symplify.noProtectedClassStmt')
72+
->build();
73+
}
74+
75+
return $ruleErrors;
76+
}
77+
78+
private function shouldSkipClassMethod(ClassMethod|ClassConst|Property $classStmt, Scope $scope): bool
79+
{
80+
if (! $classStmt instanceof ClassMethod) {
81+
return false;
82+
}
83+
84+
// PHPUnit test methods
85+
if (in_array($classStmt->name->toString(), [MethodName::SET_UP, MethodName::TEAR_DOWN])) {
86+
return false;
87+
}
88+
89+
return $this->isParentMethodOverride($classStmt, $scope);
90+
}
91+
92+
private function isParentMethodOverride(ClassMethod $classMethod, Scope $scope): bool
93+
{
94+
$classReflection = $scope->getClassReflection();
95+
if (! $classReflection instanceof ClassReflection) {
96+
return false;
97+
}
98+
99+
$parentClassReflection = $classReflection->getParentClass();
100+
if (! $parentClassReflection instanceof ClassReflection) {
101+
return false;
102+
}
103+
104+
return $parentClassReflection->hasMethod($classMethod->name->toString());
105+
}
106+
}

src/Rules/Symfony/RequireInvokableControllerRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use Symplify\PHPStanRules\Enum\MethodName;
13-
use Symplify\PHPStanRules\Enum\SymfonyClass;
1413
use Symplify\PHPStanRules\Enum\SymfonyRuleIdentifier;
1514
use Symplify\PHPStanRules\Symfony\NodeAnalyzer\SymfonyControllerAnalyzer;
1615

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule\Fixture;
4+
5+
final class ClassWithProtected
6+
{
7+
protected int $item;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule\Fixture;
4+
5+
abstract class SkipAbstractWithProtected
6+
{
7+
protected int $item;
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule\Fixture;
4+
5+
use Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule\Source\ParentClassWithMethod;
6+
7+
final class SkipParentRequired extends ParentClassWithMethod
8+
{
9+
protected function some()
10+
{
11+
}
12+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule;
6+
7+
use Iterator;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Testing\RuleTestCase;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
use Symplify\PHPStanRules\Rules\Explicit\NoProtectedClassStmtRule;
12+
13+
final class NoProtectedClassStmtRuleTest extends RuleTestCase
14+
{
15+
#[DataProvider('provideData')]
16+
public function testRule(string $filePath, array $expectedErrorsWithLines): void
17+
{
18+
$this->analyse([$filePath], $expectedErrorsWithLines);
19+
}
20+
21+
public static function provideData(): Iterator
22+
{
23+
yield [__DIR__ . '/Fixture/ClassWithProtected.php', [
24+
[NoProtectedClassStmtRule::ERROR_MESSAGE, 7],
25+
]];
26+
27+
yield [__DIR__ . '/Fixture/SkipParentRequired.php', []];
28+
yield [__DIR__ . '/Fixture/SkipAbstractWithProtected.php', []];
29+
}
30+
31+
protected function getRule(): Rule
32+
{
33+
return new NoProtectedClassStmtRule();
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symplify\PHPStanRules\Tests\Rules\Explicit\NoProtectedClassStmtRule\Source;
4+
5+
class ParentClassWithMethod
6+
{
7+
protected function some()
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)