Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix callable/lowercase strings coercion #11091

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,21 @@ public static function isContainedBy(
return false;
}

if ($input_type_part instanceof TCallableString
&& (get_class($container_type_part) === TSingleLetter::class
|| get_class($container_type_part) === TNonEmptyString::class
if ($input_type_part instanceof TCallableString) {
if (get_class($container_type_part) === TNonEmptyString::class
|| get_class($container_type_part) === TNonFalsyString::class
|| get_class($container_type_part) === TLowercaseString::class)
) {
return true;
) {
return true;
}

if (get_class($container_type_part) === TLowercaseString::class
|| get_class($container_type_part) === TSingleLetter::class
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
return false;
}
}

if (($container_type_part instanceof TLowercaseString
Expand Down
8 changes: 8 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ function foo(string $s) : void {
'assertions' => [],
'ignored_issues' => ['MixedAssignment', 'MixedArgument'],
],
'noRedundantErrorForCallableStrToLower' => [
'code' => <<<'PHP'
<?php
/** @var callable-string */
$function = "strlen";
strtolower($function);
PHP,
],
'objectLikeArrayAssignmentInConditional' => [
'code' => '<?php
$a = [];
Expand Down
42 changes: 38 additions & 4 deletions tests/TypeComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\Internal\Type\Comparator\TypeComparisonResult;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
Expand Down Expand Up @@ -129,6 +130,43 @@ public function testTypeDoesNotAcceptType(string $parent_type_string, string $ch
);
}

/** @dataProvider getCoercibleComparisons */
public function testTypeIsCoercible(string $parent_type_string, string $child_type_string): void
{
$parent_type = Type::parseString($parent_type_string);
$child_type = Type::parseString($child_type_string);

$result = new TypeComparisonResult();

$contained = UnionTypeComparator::isContainedBy(
$this->project_analyzer->getCodebase(),
$child_type,
$parent_type,
false,
false,
$result,
);

$this->assertFalse($contained, 'Type ' . $parent_type_string . ' should not contain ' . $child_type_string);
$this->assertTrue(
$result->type_coerced,
'Type ' . $parent_type_string . ' should be coercible into ' . $child_type_string,
);
}

/** @return iterable<string, list{string, string}> */
public function getCoercibleComparisons(): iterable
{
yield 'callableStringIntoLowercaseString' => [
'lowercase-string',
'callable-string',
];
yield 'lowercaseStringIntoCallableString' => [
'callable-string',
'lowercase-string',
];
}

/**
* @return array<array{string, string}>
*/
Expand All @@ -155,10 +193,6 @@ public function getSuccessfulComparisons(): array
'array{foo?: string}&array<string, mixed>',
'array<never, never>',
],
'Lowercase-stringAndCallable-string' => [
'lowercase-string',
'callable-string',
],
'callableUnionAcceptsCallableUnion' => [
'(callable(int,string[]): void)|(callable(int): void)',
'(callable(int): void)|(callable(int,string[]): void)',
Expand Down
Loading