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 makeOffsetRequired with UnionType offset #1187

Closed
wants to merge 5 commits into from

Conversation

rajyan
Copy link
Contributor

@rajyan rajyan commented Apr 8, 2022

fixes phpstan/phpstan#7000

$offsetType with UnionType should be handled separately like in hasOffsetValueType

if ($offsetType instanceof UnionType) {
$results = [];
foreach ($offsetType->getTypes() as $innerType) {
$results[] = $this->hasOffsetValueType($innerType);
}
return TrinaryLogic::extremeIdentity(...$results);
}

@rajyan rajyan changed the title Fix/issue 7000 fix makeOffsetRequired with UnionType offset Apr 8, 2022
@ondrejmirtes
Copy link
Member

Hi, this fix isn't right. When you have a union type, the value is one of the inner types. So checking isset of 'require'|'require-dev' offset doesn't mean that both offsets will surely exist after the check.

In fact this is really hard to fix in typesystem alone. The closest thing we can get after the isset is something like:

ConstantArrayType(both keys still optional)&NonEmptyArrayType

But that still doesn't help us to get rid of the error when asking about $array['require'|'require-dev'] because we don't know which one is set (both are optional).

What we can do to fix this is to let TypeSpecifier specify $composer[$linkType] (ArrayDimFetch) based on the isset so that Scope::isSpecified(ArrayDimFetch) returns true. (This might already be happening, I'm not sure.)

And the right rule needs to pick up this information and not report the error. You also need to write a regression test for the rule instead of NodeScopeResolverTest.

@rajyan
Copy link
Contributor Author

rajyan commented Apr 8, 2022

Hi, this fix isn't right. When you have a union type, the value is one of the inner types.

Oh, you're right.

Thank you for the pointers! I'll continue looking into it.

@ondrejmirtes
Copy link
Member

Feel free to finish the other PR related to Coalesce/Isset_ first, that might have a bigger impact 😊

@rajyan
Copy link
Contributor Author

rajyan commented Apr 9, 2022

Feel free to finish the other PR related to Coalesce/Isset_ first

I’ll finish it today!

@rajyan
Copy link
Contributor Author

rajyan commented Apr 11, 2022

I believe this is now an improvement for markOffsetRequired, but cannot fix the problems you mentioned in
#1187 (comment)
phpstan/phpstan#7000 (comment)
it is hard to solve them by the type system alone (as you mentioned).

The problem (I think) is that type in isset scope should be if ($linkType === 'require') array{require: array<string, string>, require-dev?: array<string, string>}; if ($linkType === 'require-dev') array{require?: array<string, string>, require-dev: array<string, string>}, which is hard to represent in types.
so the type specified here

$type = $this->create(
$var->var,
new HasOffsetType($scope->getType($var->dim)),
$context,
false,
$scope,

should be an conditional type if $var->dim is a UnionType

@rajyan
Copy link
Contributor Author

rajyan commented Apr 11, 2022

I'll read this #1187 (comment) again and look for a fix if possible

@rajyan
Copy link
Contributor Author

rajyan commented Apr 11, 2022

improving type to
ConstantArrayType(both keys still optional)&NonEmptyArrayType
by b8902c3
does fix the phpstan/phpstan#7000
while providing an false negative

foreach ($composer[$foo] as $x) {} // should report error. It can be $linkType = 'require', $foo = 'require-dev'

@rajyan rajyan changed the base branch from 1.5.x to 1.6.x April 24, 2022 15:03
@rajyan
Copy link
Contributor Author

rajyan commented May 5, 2022

I'd close this for now. I think it's not easy enough to solve cleanly, and there are other related issues to solve before this.

phpstan/phpstan#7143
phpstan/phpstan#7144

@rajyan rajyan closed this May 5, 2022
@rajyan rajyan mentioned this pull request May 11, 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