1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fixed #1848 - allow static class strings to be compared

This commit is contained in:
Brown 2019-06-26 15:11:16 -04:00
parent 372a512a86
commit 5ed49c0c03
5 changed files with 35 additions and 4 deletions

View File

@ -172,7 +172,13 @@ class ConstFetchAnalyzer
}
}
$stmt->inferredType = Type::getLiteralClassString($fq_class_name);
if ($first_part_lc === 'static') {
$stmt->inferredType = new Type\Union([
new Type\Atomic\TClassString($fq_class_name, new Type\Atomic\TNamedObject($fq_class_name))
]);
} else {
$stmt->inferredType = Type::getLiteralClassString($fq_class_name);
}
if ($codebase->store_node_types) {
$codebase->analyzer->addNodeReference(

View File

@ -249,6 +249,7 @@ class Reconciler
&& !$result_type->hasTemplate()
&& !$result_type->hasType('iterable')
&& (!$has_isset || substr($key, -1, 1) !== ']')
&& !($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
) {
$reconcile_key = implode(
'&',
@ -1307,6 +1308,9 @@ class Reconciler
&& $new_type->getId() === $existing_var_type->getId()
&& !$is_equality
&& !$is_maybe_callable_array
&& (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
|| ($key !== '$this'
&& !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString())))
) {
self::triggerIssueForImpossible(
$existing_var_type,
@ -1358,8 +1362,9 @@ class Reconciler
// fall through
}
}
} elseif ($key !== '$this'
|| !($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
} elseif (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
|| ($key !== '$this'
&& !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString()))
) {
if ($existing_var_type->from_docblock) {
if (IssueBuffer::accepts(

View File

@ -657,6 +657,14 @@ class Union
|| $this->typed_class_strings;
}
/**
* @return bool
*/
public function hasLiteralClassString()
{
return count($this->typed_class_strings) > 0;
}
/**
* @return bool
*/

View File

@ -1591,7 +1591,7 @@ class AnnotationTest extends TestCase
*/
function f($reference) {}',
'error_message' => 'MissingDocblockType'
]
],
];
}
}

View File

@ -591,6 +591,18 @@ class ClassStringTest extends TestCase
}
}'
],
'allowComparisonToStaticClassString' => [
'<?php
class A {
const CLASSES = ["foobar" => B::class];
function foo(): bool {
return self::CLASSES["foobar"] === static::class;
}
}
class B extends A {}'
],
];
}