forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodVisibilityComparisonHelper.php
More file actions
executable file
·56 lines (49 loc) · 1.72 KB
/
MethodVisibilityComparisonHelper.php
File metadata and controls
executable file
·56 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PhpParser\Node;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
#[AutowiredService]
final class MethodVisibilityComparisonHelper
{
/** @return list<IdentifierRuleError> */
public function compare(ExtendedMethodReflection $prototype, ClassReflection $prototypeDeclaringClass, PhpMethodFromParserNodeReflection $method, Node $node): array
{
/** @var list<IdentifierRuleError> $messages */
$messages = [];
if ($prototype->isPublic()) {
if (!$method->isPublic()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'%s method %s::%s() overriding public method %s::%s() should also be public.',
$method->isPrivate() ? 'Private' : 'Protected',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$prototypeDeclaringClass->getDisplayName(true),
$prototype->getName(),
))
->nonIgnorable()
->identifier('method.visibility')
->line($node->getStartLine())
->build();
}
} elseif ($method->isPrivate()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Private method %s::%s() overriding protected method %s::%s() should be protected or public.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$prototypeDeclaringClass->getDisplayName(true),
$prototype->getName(),
))
->nonIgnorable()
->identifier('method.visibility')
->line($node->getStartLine())
->build();
}
return $messages;
}
}