Repository navigation
Expand file tree
/
Copy pathStaticMethodCallableRuleTest.php
More file actions
142 lines (130 loc) · 3.69 KB
/
Copy pathStaticMethodCallableRuleTest.php
File metadata and controls
142 lines (130 loc) · 3.69 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PHPStan\Classes\ForbiddenClassNameExtension;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
use PHPStan\Rules\ClassNameCheck;
use PHPStan\Rules\RestrictedUsage\RestrictedClassNameUsageExtension;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<StaticMethodCallableRule>
*/
class StaticMethodCallableRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
$reflectionProvider = self::createReflectionProvider();
$ruleLevelHelper = new RuleLevelHelper(
$reflectionProvider,
checkNullables: true,
checkThisOnly: false,
checkUnionTypes: true,
checkExplicitMixed: false,
checkImplicitMixed: false,
checkBenevolentUnionTypes: false,
discoveringSymbolsTip: true,
);
$container = self::getContainer();
return new StaticMethodCallableRule(
new StaticMethodCallCheck(
$reflectionProvider,
$ruleLevelHelper,
new ClassNameCheck(
new ClassCaseSensitivityCheck(
$reflectionProvider,
checkInternalClassCaseSensitivity: true,
checkImportedClassNameCase: true,
),
new ClassForbiddenNameCheck($container->getExtensionsCollection(ForbiddenClassNameExtension::class)),
$reflectionProvider,
$container->getExtensionsCollection(RestrictedClassNameUsageExtension::class),
),
checkFunctionNameCase: true,
discoveringSymbolsTip: true,
reportMagicMethods: true,
),
);
}
#[RequiresPhp('< 8.1.0')]
public function testNotSupportedOnOlderVersions(): void
{
$this->analyse([__DIR__ . '/data/static-method-callable-not-supported.php'], [
[
'First-class callables are supported only on PHP 8.1 and later.',
10,
],
]);
}
#[RequiresPhp('>= 8.1.0')]
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/static-method-callable.php'], [
[
'Call to static method StaticMethodCallable\Foo::doFoo() with incorrect case: dofoo',
11,
],
[
'Call to static method doFoo() on an unknown class StaticMethodCallable\Nonexistent.',
12,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'Call to an undefined static method StaticMethodCallable\Foo::nonexistent().',
13,
],
[
'Static call to instance method StaticMethodCallable\Foo::doBar().',
14,
],
[
'Call to private static method doBar() of class StaticMethodCallable\Bar.',
15,
],
[
'Cannot call abstract static method StaticMethodCallable\Bar::doBaz().',
16,
],
[
'Call to static method doFoo() on an unknown class StaticMethodCallable\Nonexistent.',
21,
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
],
[
'Cannot call static method doFoo() on int.',
22,
],
[
'Call to an undefined static method StaticMethodCallable\Bar|StaticMethodCallable\Foo::doFoo().',
77,
],
]);
}
public function testBug8752(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-8752.php'], []);
}
public function testCallsOnGenericClassString(): void
{
$this->analyse([__DIR__ . '/../Comparison/data/impossible-method-exists-on-generic-class-string.php'], []);
}
public function testConditionallyExecutedCode(): void
{
$errors = [
[
'First-class callables are supported only on PHP 8.1 and later.',
19,
],
];
if (PHP_VERSION_ID < 80100) {
$errors[] = [
'First-class callables are supported only on PHP 8.1 and later.',
22,
];
}
$this->analyse([__DIR__ . '/data/static-method-callable-php-versions.php'], $errors);
}
}