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

more precise $argc, $argv global var type #1718

Merged
merged 1 commit into from Sep 20, 2022

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 10, 2022

global $argc, $argv;
\PHPStan\Testing\assertType('mixed', $argc); // should be int<1, max>
\PHPStan\Testing\assertType('mixed', $argv); // should be non-empty-array<int, string>
Copy link
Contributor Author

@staabm staabm Sep 10, 2022

Choose a reason for hiding this comment

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

tried several things, but in the end wasn't able to identify this variable-scope.

therefore left it mixed for now.. I don't think thats a very common thing anyway
(and we can't be 100% sure whether global $argc, $argv actually references the builtin globals or a custom variable)

\PHPStan\Testing\assertType('int<1, max>', $argc);
\PHPStan\Testing\assertType('non-empty-array<int, string>', $argv);
Copy link
Contributor Author

@staabm staabm Sep 10, 2022

Choose a reason for hiding this comment

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

$argc/$argv always count/contain the program name you pass php test.php, so actually you never have a empty type here.

actually this variable would only exist for a script run via the CLI SAPI, but I don't think we can differentiate CLI and WEB sapi.

FWIW, psalm also allows this 2 vars: https://psalm.dev/r/942a865551 and they are in the process of making them more precise in https://github.com/vimeo/psalm/pull/8473/files. I will look into making the super-globals more precise in a separate PR.

@staabm staabm marked this pull request as ready for review Sep 10, 2022
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Please also test if these variables are locally created by the user and contain a different type.

\PHPStan\Testing\assertType('non-empty-array<int, string>', $argv);

function f() {
\PHPStan\Testing\assertType('*ERROR*', $argc);
Copy link
Member

@ondrejmirtes ondrejmirtes Sep 20, 2022

Choose a reason for hiding this comment

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

Please put this function in a use statement.

@ondrejmirtes ondrejmirtes merged commit f74f668 into phpstan:1.8.x Sep 20, 2022
47 checks passed
@ondrejmirtes
Copy link
Member

ondrejmirtes commented Sep 20, 2022

Thank you.

@staabm staabm deleted the cli-globals branch Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants