Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d5d679

Browse files
Christopher Wosinskiondrejmirtes
Christopher Wosinski
authored andcommitted
Fixes #1482: method_exists() with string
1 parent e90c166 commit 2d5d679

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

‎src/Type/Php/MethodExistsTypeSpecifyingExtension.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use PHPStan\Type\Constant\ConstantStringType;
1414
use PHPStan\Type\FunctionTypeSpecifyingExtension;
1515
use PHPStan\Type\IntersectionType;
16+
use PHPStan\Type\ObjectType;
1617
use PHPStan\Type\ObjectWithoutClassType;
18+
use PHPStan\Type\StringType;
1719

1820
class MethodExistsTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
1921
{
@@ -44,6 +46,13 @@ public function specifyTypes(
4446
TypeSpecifierContext $context
4547
): SpecifiedTypes
4648
{
49+
$objectType = $scope->getType($node->args[0]->value);
50+
if (!$objectType instanceof ObjectType) {
51+
if ((new StringType())->isSuperTypeOf($objectType)->yes()) {
52+
return new SpecifiedTypes([], []);
53+
}
54+
}
55+
4756
$methodNameType = $scope->getType($node->args[1]->value);
4857
if (!$methodNameType instanceof ConstantStringType) {
4958
return new SpecifiedTypes([], []);

‎tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ public function testImpossibleCheckTypeFunctionCall(): void
140140
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'testWithStringFirst...\' will always evaluate to true.',
141141
585,
142142
],
143-
[
144-
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'undefinedMethod\' will always evaluate to false.',
145-
588,
146-
],
147143
[
148144
'Call to function method_exists() with \'UndefinedClass\' and string will always evaluate to false.',
149145
594,
@@ -154,43 +150,43 @@ public function testImpossibleCheckTypeFunctionCall(): void
154150
],
155151
[
156152
'Call to function method_exists() with CheckTypeFunctionCall\MethodExists and \'testWithNewObjectIn...\' will always evaluate to true.',
157-
606,
153+
609,
158154
],
159155
[
160156
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'method\' will always evaluate to true.',
161-
621,
157+
624,
162158
],
163159
[
164160
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'someAnother\' will always evaluate to true.',
165-
624,
161+
627,
166162
],
167163
[
168164
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'unknown\' will always evaluate to false.',
169-
627,
165+
630,
170166
],
171167
[
172168
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'method\' will always evaluate to true.',
173-
630,
169+
633,
174170
],
175171
[
176172
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'someAnother\' will always evaluate to true.',
177-
633,
173+
636,
178174
],
179175
[
180176
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'unknown\' will always evaluate to false.',
181-
636,
177+
639,
182178
],
183179
[
184180
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'method\' will always evaluate to true.',
185-
639,
181+
642,
186182
],
187183
[
188184
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'someAnother\' will always evaluate to true.',
189-
642,
185+
645,
190186
],
191187
[
192188
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'unknown\' will always evaluate to false.',
193-
645,
189+
648,
194190
],
195191
]
196192
);
@@ -262,10 +258,6 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
262258
'Call to function is_callable() with mixed will always evaluate to false.',
263259
571,
264260
],
265-
[
266-
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'undefinedMethod\' will always evaluate to false.',
267-
588,
268-
],
269261
[
270262
'Call to function method_exists() with \'UndefinedClass\' and string will always evaluate to false.',
271263
594,
@@ -276,15 +268,15 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
276268
],
277269
[
278270
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'unknown\' will always evaluate to false.',
279-
627,
271+
630,
280272
],
281273
[
282274
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'unknown\' will always evaluate to false.',
283-
636,
275+
639,
284276
],
285277
[
286278
'Call to function method_exists() with \'CheckTypeFunctionCa...\' and \'unknown\' will always evaluate to false.',
287-
645,
279+
648,
288280
],
289281
]
290282
);

‎tests/PHPStan/Rules/Comparison/data/check-type-function-call.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ public function testWithStringFirstArgument(): void
596596

597597
if (method_exists('UndefinedClass', 'test')) {
598598
}
599+
600+
if (method_exists($string, 'test')) {
601+
}
599602
}
600603

601604
public function testWithNewObjectInFirstArgument(): void

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /