Skip to content

Commit

Permalink
Merge pull request #6589 from orklah/strict-callable
Browse files Browse the repository at this point in the history
Better understanding of Templates in lhs of callable TKeyedArray
  • Loading branch information
orklah committed Oct 5, 2021
2 parents 7332c51 + 3cf5dc5 commit 96ed252
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Psalm/Internal/Type/Comparator/CallableTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TClosure;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
Expand Down Expand Up @@ -430,6 +431,24 @@ public static function getCallableMethodIdFromTKeyedArray(
strtolower($lhs_atomic_type->value) . '::',
$calling_method_id ?: $file_name
);
} elseif ($lhs_atomic_type instanceof Atomic\TTemplateParam) {
$lhs_template_type = $lhs_atomic_type->as;
if ($lhs_template_type->isSingle()) {
$lhs_template_atomic_type = $lhs_template_type->getSingleAtomic();
$member_id = null;
if ($lhs_template_atomic_type instanceof TNamedObject) {
$member_id = $lhs_template_atomic_type->value;
} elseif ($lhs_template_atomic_type instanceof TClassString) {
$member_id = $lhs_template_atomic_type->as;
}

if ($member_id) {
$codebase->analyzer->addMixedMemberName(
strtolower($member_id) . '::',
$calling_method_id ?: $file_name
);
}
}
}
}
}
Expand All @@ -450,6 +469,16 @@ public static function getCallableMethodIdFromTKeyedArray(
foreach ($lhs->getAtomicTypes() as $lhs_atomic_type) {
if ($lhs_atomic_type instanceof TNamedObject) {
$class_name = $lhs_atomic_type->value;
} elseif ($lhs_atomic_type instanceof Atomic\TTemplateParam) {
$lhs_template_type = $lhs_atomic_type->as;
if ($lhs_template_type->isSingle()) {
$lhs_template_atomic_type = $lhs_template_type->getSingleAtomic();
if ($lhs_template_atomic_type instanceof TNamedObject) {
$class_name = $lhs_template_atomic_type->value;
} elseif ($lhs_template_atomic_type instanceof TClassString) {
$class_name = $lhs_template_atomic_type->as;
}
}
} elseif ($lhs_atomic_type instanceof Type\Atomic\TClassString
&& $lhs_atomic_type->as
) {
Expand Down
5 changes: 5 additions & 0 deletions src/Psalm/Type/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -1680,4 +1680,9 @@ public function hasLiteralFloat(): bool
{
return count($this->literal_float_types) > 0;
}

public function getSingleAtomic(): Atomic
{
return reset($this->types);
}
}

0 comments on commit 96ed252

Please sign in to comment.