Skip to content

Commit

Permalink
Restrict narrowing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 3, 2020
1 parent b168777 commit 20a9b10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
26 changes: 18 additions & 8 deletions src/Psalm/Internal/Analyzer/FileAnalyzer.php
Expand Up @@ -288,22 +288,32 @@ public function populateCheckers(array $stmts)
*/
private function populateClassLikeAnalyzers(PhpParser\Node\Stmt\ClassLike $stmt)
{
if (!$stmt->name) {
return;
}
if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
if (!$stmt->name) {
return;
}

// this can happen when stubbing
if (!$this->codebase->classExists($stmt->name->name)) {
return;
}

// this can happen when stubbing
if (!$this->codebase->classOrInterfaceExists($stmt->name->name)) {
return;
}

if ($stmt instanceof PhpParser\Node\Stmt\Class_) {
$class_analyzer = new ClassAnalyzer($stmt, $this, $stmt->name->name);

$fq_class_name = $class_analyzer->getFQCLN();

$this->class_analyzers_to_analyze[strtolower($fq_class_name)] = $class_analyzer;
} elseif ($stmt instanceof PhpParser\Node\Stmt\Interface_) {
if (!$stmt->name) {
return;
}

// this can happen when stubbing
if (!$this->codebase->interfaceExists($stmt->name->name)) {
return;
}

$class_analyzer = new InterfaceAnalyzer($stmt, $this, $stmt->name->name);

$fq_class_name = $class_analyzer->getFQCLN();
Expand Down
10 changes: 6 additions & 4 deletions src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php
Expand Up @@ -1947,14 +1947,14 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m

$storage->required_param_count = $required_param_count;

if (($stmt instanceof PhpParser\Node\Stmt\Function_
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod)
&& $stmt->stmts
if ($stmt instanceof PhpParser\Node\Stmt\Function_
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod
) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
&& $storage instanceof MethodStorage
&& $class_storage
&& !$class_storage->mutation_free
&& $stmt->stmts
&& count($stmt->stmts) === 1
&& !count($stmt->params)
&& $stmt->stmts[0] instanceof PhpParser\Node\Stmt\Return_
Expand All @@ -1974,7 +1974,9 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m

$class_storage->properties[$property_name]->getter_method = strtolower($stmt->name->name);
}
} elseif (strpos($stmt->name->name, 'assert') === 0) {
} elseif (strpos($stmt->name->name, 'assert') === 0
&& $stmt->stmts
) {
$var_assertions = [];

foreach ($stmt->stmts as $function_stmt) {
Expand Down
6 changes: 4 additions & 2 deletions src/Psalm/Type/Reconciler.php
Expand Up @@ -345,13 +345,15 @@ public static function reconcileKeyedTypes(
$changed_var_ids,
$result_type
);
} else {
} elseif ($key !== '$this') {
foreach ($existing_types as $new_key => $_) {
if ($new_key === $key) {
continue;
}

if (preg_match('/' . preg_quote($key, '/') . '[\]\[\-]/', $new_key)) {
if (!isset($new_types[$new_key])
&& preg_match('/' . preg_quote($key, '/') . '[\]\[\-]/', $new_key)
) {
unset($existing_types[$new_key]);
}
}
Expand Down

0 comments on commit 20a9b10

Please sign in to comment.