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

Keep traversing type when we can contain lowercase/upercase strings #3792

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
10 changes: 9 additions & 1 deletion src/Type/VerbosityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,18 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
$moreVerboseCallback = static function (Type $type, callable $traverse) use (&$moreVerbose, &$veryVerbose): Type {
if ($type->isCallable()->yes()) {
$moreVerbose = true;
return $type;

// Keep checking if we need to be very verbose.
return $traverse($type);
}
if ($type->isConstantValue()->yes() && $type->isNull()->no()) {
$moreVerbose = true;

// For ConstantArrayType we need to keep checking if we need to be very verbose.
if (!$type->isArray()->no()) {
return $traverse($type);
}

return $type;
}
if (
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,21 @@ public function testReportWrongType(
$this->analyse([__DIR__ . '/data/wrong-var-native-type.php'], $expectedErrors);
}

public function testBug12457(): void
{
$this->checkTypeAgainstNativeType = true;
$this->checkTypeAgainstPhpDocType = true;
$this->strictWideningCheck = true;
$this->analyse([__DIR__ . '/data/bug-12457.php'], [
[
'PHPDoc tag @var with type array{numeric-string} is not subtype of type array{lowercase-string&numeric-string&uppercase-string}.',
13,
],
[
'PHPDoc tag @var with type callable(): string is not subtype of type callable(): numeric-string&lowercase-string&uppercase-string.',
22,
],
]);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-12457.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug12457;

class HelloWorld
{
/**
* @param array{numeric-string&uppercase-string&lowercase-string} $a
*/
public function sayHello(array $a): void
{
/** @var array{numeric-string} $b */
$b = $a;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need more tests for different variants of this problem?

like

Suggested change
}
}
/**
* @param array{numeric-string&uppercase-string&lowercase-string} $a
*/
public function sayUpper(array $a): void
{
/** @var array{uppercase-string} $b */
$b = $a;
}
/**
* @param array{numeric-string&uppercase-string&lowercase-string} $a
*/
public function sayLower(array $a): void
{
/** @var array{lowercase-string} $b */
$b = $a;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also variants with non-empty-string, non-falsey-string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so since it's not related to numeric-string directly.

The issue was because it was a ConstantArray with a lowercase-string or upercase-string inside.
Because of the early return $type we didn't process the content of the array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I added a test for the callable case
ad97094


/**
* @param callable(): numeric-string&uppercase-string&lowercase-string $a
*/
public function sayHello2(callable $a): void
{
/** @var callable(): string $b */
$b = $a;
}
}
Loading