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

Allow to get defined variables from scope that are maybe defined #11772

Closed
ruudk opened this issue Oct 1, 2024 · 5 comments · Fixed by phpstan/phpstan-src#3521
Closed

Allow to get defined variables from scope that are maybe defined #11772

ruudk opened this issue Oct 1, 2024 · 5 comments · Fixed by phpstan/phpstan-src#3521

Comments

@ruudk
Copy link
Contributor

ruudk commented Oct 1, 2024

Feature request

Currently, Scope::getDefinedVariables() only returns certain variables.

How to retrieve variables that are maybe defined?

https://github.com/phpstan/phpstan-src/blob/f38addda2b151b6e41a746a37659c0bbe9e2293b/src/Analyser/MutatingScope.php#L557-L572

Did PHPStan help you today? Did it make you happy in any way?

No response

@ruudk
Copy link
Contributor Author

ruudk commented Oct 1, 2024

Had to create this ugly thing to be able to retrieve them:

$builder = ConstantArrayTypeBuilder::createEmpty();
foreach ($variables as $variable) {
    $builder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable));
}

foreach ($scope->debug() as $key => $value) {
    if ( ! str_starts_with($key, '$')) {
        continue;
    }

    if ( ! str_ends_with($key, ' (Maybe)')) {
        continue;
    }

    $variable = substr($key, 1, -8);
    $builder->setOffsetValueType(new ConstantStringType($variable), $scope->getVariableType($variable), true);
}

return $builder->getArray();

@ondrejmirtes
Copy link
Member

Oh, that's a creative hack 😂

I'd accept a PR that adds Scope::getMaybeDefinedVariables().

@ruudk
Copy link
Contributor Author

ruudk commented Oct 1, 2024

Done in phpstan/phpstan-src#3521

ruudk added a commit to ruudk/phpstan-src that referenced this issue Oct 1, 2024
@ondrejmirtes
Copy link
Member

Implemented phpstan/phpstan-src#3521

Copy link

github-actions bot commented Nov 3, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants