Skip to content

Commit df27a12

Browse files
committed
TypeSpecifier - basic support for get_class() in == operator
1 parent 98ba1e1 commit df27a12

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Analyser/TypeSpecifier.php

+34
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,40 @@ public function specifyTypesInCondition(
277277
$context
278278
);
279279
}
280+
281+
if (
282+
$expr->left instanceof FuncCall
283+
&& $expr->left->name instanceof Name
284+
&& strtolower($expr->left->name->toString()) === 'get_class'
285+
&& isset($expr->left->args[0])
286+
&& $rightType instanceof ConstantStringType
287+
) {
288+
return $this->specifyTypesInCondition(
289+
$scope,
290+
new Instanceof_(
291+
$expr->left->args[0]->value,
292+
new Name($rightType->getValue())
293+
),
294+
$context
295+
);
296+
}
297+
298+
if (
299+
$expr->right instanceof FuncCall
300+
&& $expr->right->name instanceof Name
301+
&& strtolower($expr->right->name->toString()) === 'get_class'
302+
&& isset($expr->right->args[0])
303+
&& $leftType instanceof ConstantStringType
304+
) {
305+
return $this->specifyTypesInCondition(
306+
$scope,
307+
new Instanceof_(
308+
$expr->right->args[0]->value,
309+
new Name($leftType->getValue())
310+
),
311+
$context
312+
);
313+
}
280314
} elseif ($expr instanceof Node\Expr\BinaryOp\NotEqual) {
281315
return $this->specifyTypesInCondition(
282316
$scope,

tests/PHPStan/Analyser/TypeSpecifierTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ public function dataCondition(): array
162162
['$foo' => 'object'],
163163
[],
164164
],
165+
[
166+
new Equal(
167+
new FuncCall(new Name('get_class'), [
168+
new Arg(new Variable('foo')),
169+
]),
170+
new String_('Foo')
171+
),
172+
['$foo' => 'Foo'],
173+
['$foo' => '~Foo'],
174+
],
175+
[
176+
new Equal(
177+
new String_('Foo'),
178+
new FuncCall(new Name('get_class'), [
179+
new Arg(new Variable('foo')),
180+
])
181+
),
182+
['$foo' => 'Foo'],
183+
['$foo' => '~Foo'],
184+
],
165185
[
166186
new BooleanNot(
167187
new Expr\Instanceof_(

0 commit comments

Comments
 (0)