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 phpstan-src build errors #375

Merged
merged 3 commits into from
Dec 22, 2023
Merged

Fix phpstan-src build errors #375

merged 3 commits into from
Dec 22, 2023

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Dec 21, 2023

before this PR

There was 1 failure:

1) PHPStan\Type\Symfony\ExtensionTest::testFileAsserts with data set "/home/runner/work/phpstan-src/phpstan-src/extension/tests/Type/Symfony/data/cache.php:12" ('type', '/home/runner/work/phpstan-src...he.php', PHPStan\Type\Constant\ConstantStringType Object (...), PHPStan\Type\Constant\ConstantStringType Object (...), 12)
Expected type string, got type '' in /home/runner/work/phpstan-src/phpstan-src/extension/tests/Type/Symfony/data/cache.php on line 12.
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'string'
+''''

@ondrejmirtes
Copy link
Member

This is not the right fix. The failing test actually shows a real issue. PHPStan dynamic return type extension needs to be written instead.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

And please rebase this on top of 1.3.x.

}

$callbackReturnType = $scope->getType($methodCall->getArgs()[1]->value);
if ($callbackReturnType instanceof ClosureType) {
Copy link
Member

Choose a reason for hiding this comment

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

CacheInterface accepts any callable, instanceof *Type is always wrong.

Type has getCallableParametersAcceptors, check isCallable()->yes() and then call this method.

@staabm staabm changed the base branch from 1.4.x to 1.3.x December 22, 2023 09:07
composer.json Outdated
@@ -15,7 +15,7 @@
"require": {
"php": "^7.2 || ^8.0",
"ext-simplexml": "*",
"phpstan/phpstan": "^1.10.36"
"phpstan/phpstan": "^1.10.49"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since the behaviour we fix with this PR only affects 1.10.49+
refs phpstan/phpstan-src#2818

Copy link
Member

Choose a reason for hiding this comment

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

I disagree with this change. The dynamic return type extension is valid and works for older PHPStan versions too.

composer.json Outdated
@@ -15,7 +15,7 @@
"require": {
"php": "^7.2 || ^8.0",
"ext-simplexml": "*",
"phpstan/phpstan": "^1.10.36"
"phpstan/phpstan": "^1.10.49"
Copy link
Member

Choose a reason for hiding this comment

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

I disagree with this change. The dynamic return type extension is valid and works for older PHPStan versions too.

// generalize template parameters
if ($returnType->isConstantScalarValue()->yes()) {
return $returnType->generalize(GeneralizePrecision::lessSpecific());
}
Copy link
Member

Choose a reason for hiding this comment

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

Please test a scenario where this is not a scalar. The $returnType should still be used.

Copy link
Member

Choose a reason for hiding this comment

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

I'd rather return $returnType here too.

function testNonScalarCacheCallable(\Symfony\Contracts\Cache\CacheInterface $cache, callable $fn): void {
$result = $cache->get('foo', $fn);

assertType('string', $result);
Copy link
Member

Choose a reason for hiding this comment

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

I feel like non-empty-string should be generalized to string. Which means we'll likely have to always generalize by GeneralizePrecision::templateArgument().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wasn't aware GeneralizePrecision::templateArgument() is a thing 😅

@ondrejmirtes ondrejmirtes merged commit 34b3c43 into phpstan:1.3.x Dec 22, 2023
31 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the patch-1 branch December 22, 2023 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants