Skip to content

replace VarExportFunctionDynamicReturnTypeExtension with conditional-type stub#1244

Closed
staabm wants to merge 3 commits into
phpstan:1.6.xfrom
staabm:conditional-export-stub
Closed

replace VarExportFunctionDynamicReturnTypeExtension with conditional-type stub#1244
staabm wants to merge 3 commits into
phpstan:1.6.xfrom
staabm:conditional-export-stub

Conversation

@staabm

@staabm staabm commented Apr 25, 2022

Copy link
Copy Markdown
Contributor

with this PR I want to try to replace a existing phpstan extension with stubs and conditional types.
I did so with some very basic examples, like var_export etc. as a proof-of-concept.

It does not work yet though.

Comment thread stubs/core.stub Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this stub is taken 1:1 from psalm, to verify phpstan is kind of compatible with the psalm notations

Comment thread stubs/core.stub Outdated

@staabm staabm Apr 25, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

when running tests on phpstan-src@1.6.x it seems to make a difference whether the @return expression here is sorrounded by braces.. I think this is not expected

see

grafik

vs. (when running without braces)

grafik

(both results are not correct though ;))

@staabm staabm Apr 26, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

after reading the 1.6.x release blog post, I conclude the paranthesis are necessary.

@staabm staabm force-pushed the conditional-export-stub branch from 9fbc45e to ce13ef2 Compare April 27, 2022 10:53

@staabm staabm left a comment

Copy link
Copy Markdown
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 know why yet, but the result of the test actually look like

9) PHPStan\Analyser\NodeScopeResolverTest::testFileAsserts with data set "C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser/data/pr-1244.php:24" ('type', 'C:\dvl\Workspace\phpstan-src-...44.php', PHPStan\Type\Constant\ConstantStringType Object (...), PHPStan\Type\ConditionalTypeForParameter Object (...), 24)
Expected type string|true, got type ($return is true ? string : true) in C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser/data/pr-1244.php on line 24.
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'string|true'
+'($return is true ? string : true)'

C:\dvl\Workspace\phpstan-src-staabm\src\Testing\TypeInferenceTestCase.php:90
C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser\NodeScopeResolverTest.php:919

10) PHPStan\Analyser\NodeScopeResolverTest::testFileAsserts with data set "C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser/data/pr-1244.php:25" ('type', 'C:\dvl\Workspace\phpstan-src-...44.php', PHPStan\Type\Constant\ConstantStringType Object (...), PHPStan\Type\ConditionalTypeForParameter Object (...), 25)
Expected type true, got type ($return is true ? string : true) in C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Analyser/data/pr-1244.php on line 25.
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'true'
+'($return is true ? string : true)'

I tried moving the test into a NodeScopeResolverTest but this did not make a difference compared to LegacyNodeScopeResolverTest.

it seems the return-type is not finally resolved.
not sure whether I am missing something in the stub-file, or whether I am hitting a bug within conditional-types, which make phpstan not resolve the ConditionalTypeForParameter.

any hint would be appreciated

Comment thread stubs/core.stub Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

psalm supports a @return ($return is true ? string : void) type, while it seems in phpstan atm we need to explicitly use null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is because the function map specifies var_export to return string|null which isn't compatible with string|void.

@rvanvelzen

Copy link
Copy Markdown
Contributor

I'll try to look into this tomorrow.

@rvanvelzen

Copy link
Copy Markdown
Contributor

So the big problem is that default values aren't exposed for native functions, so $return can't actually be resolved. I've created #1261 to remedy that, but that will only work for PHP>=8 because there are no default values available for native functions before that.

@staabm

staabm commented Apr 28, 2022

Copy link
Copy Markdown
Contributor Author

thank you for your support Richard <3.

@ondrejmirtes

Copy link
Copy Markdown
Member

Finished and merged: 09635a1

Thank you!

@staabm staabm deleted the conditional-export-stub branch May 11, 2022 14:17
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.

4 participants