Skip to content

[OffsetAccessAssignmentToScalarRule] Initial implementation#1307

Closed
iluuu1994 wants to merge 1 commit into
phpstan:masterfrom
iluuu1994:feature/array-access-assignment-to-scalar
Closed

[OffsetAccessAssignmentToScalarRule] Initial implementation#1307
iluuu1994 wants to merge 1 commit into
phpstan:masterfrom
iluuu1994:feature/array-access-assignment-to-scalar

Conversation

@iluuu1994

@iluuu1994 iluuu1994 commented Jul 29, 2018

Copy link
Copy Markdown
Contributor

Closes #724

  • Rename to OffsetAccessAssignmentToScalarRule

@iluuu1994 iluuu1994 force-pushed the feature/array-access-assignment-to-scalar branch from 13caabc to d54ddd4 Compare July 29, 2018 10:54
@iluuu1994

Copy link
Copy Markdown
Contributor Author

There's more that we should cover for string types:

https://3v4l.org/qJG1U
https://3v4l.org/mDX6U

But we'll do that in a separate rule. I'll create a new issue for that.

@iluuu1994 iluuu1994 force-pushed the feature/array-access-assignment-to-scalar branch from d54ddd4 to 5ce3111 Compare July 29, 2018 18:31
@iluuu1994 iluuu1994 changed the title [ArrayAccessAssignmentToScalarRule] Initial implementation [OffsetAccessAssignmentToScalarRule] Initial implementation Jul 29, 2018
@iluuu1994 iluuu1994 force-pushed the feature/array-access-assignment-to-scalar branch from 5ce3111 to 4e0e08f Compare July 29, 2018 18:32
@ondrejmirtes

Copy link
Copy Markdown
Member

Hi, this is already (at least partially) covered by returning ErrorType from Type:: setOffsetValueType. So I suggest creating a rule that observes ArrayDimFetch in an assign operation and reports an error in case of ErrorType. This rule should pass these existing tests so it's possible it will be required to go through all the method implementations and improve them to return ErrorType in the right situations.

This kind of rule also has to respect levels - level 3 for always incorrect values and level 6 for partially incorrect values (unions where at least one type is fine).

@iluuu1994

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes

This kind of rule also has to respect levels - level 3 for always incorrect values and level 6 for partially incorrect values (unions where at least one type is fine).

This PR already supports reportMaybes. Is that what you're asking for?

@ondrejmirtes

Copy link
Copy Markdown
Member

@iluuu1994 It would be nicer to employ RuleLevelHelper::findTypeToCheck, see how it's used elsewhere. It will also solve complaining about null which should be done only on level 7.

@iluuu1994 iluuu1994 force-pushed the feature/array-access-assignment-to-scalar branch from 4e0e08f to 459044e Compare August 10, 2018 20:18
@iluuu1994

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes One issue:

Currently, AssignOp are not added as assign expressions. This is because then expressions like $foo += 10; would not trigger an undefined variable $foo error. Unfortunately, for this rule this means that isInExpressionAssign will report false which will cause a false negative for this rule.

Modifying the NodeScopeResolver here:

https://github.com/phpstan/phpstan/blob/master/src/Analyser/NodeScopeResolver.php#L819

by adding || $node instanceof Expr\AssignOp will solve my issue but cause the other one described above.

Any idea on how to fix this?

@iluuu1994

Copy link
Copy Markdown
Contributor Author

Also, the rule is very redundant. The issue is that I need to filter the maybeTypes for expr and dim. Any tips on how to improve that?

@ondrejmirtes

ondrejmirtes commented Aug 10, 2018 via email

Copy link
Copy Markdown
Member

@ondrejmirtes

ondrejmirtes commented Aug 10, 2018 via email

Copy link
Copy Markdown
Member

@iluuu1994

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes I don't see my PR in the dropdown 🤔

@ondrejmirtes

ondrejmirtes commented Aug 10, 2018 via email

Copy link
Copy Markdown
Member

@iluuu1994

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes The reason the build is failing is because of this. The tests already exist.

This one fails which is correct:

$value = 'Foo';
$value['foo'] = null;

This one should fail, but it doesn't:

$value = 'Foo';
$value['foo'] += null;

That is because $value['foo'] in $value['foo'] += null; is not recognized as an assignment expression which means the rule will ignore it (since we check isInExpressionAssign).

@ondrejmirtes

ondrejmirtes commented Aug 10, 2018 via email

Copy link
Copy Markdown
Member

@iluuu1994

Copy link
Copy Markdown
Contributor Author

@ondrejmirtes Thanks 🙂

@ondrejmirtes ondrejmirtes force-pushed the master branch 2 times, most recently from be23b86 to c6a8cd1 Compare October 20, 2018 20:02
@ondrejmirtes ondrejmirtes force-pushed the master branch 3 times, most recently from 5904f1c to 99bb3a9 Compare October 30, 2018 10:54
@ondrejmirtes ondrejmirtes added this to the 0.11 milestone Nov 4, 2018
@ondrejmirtes

Copy link
Copy Markdown
Member

I've taken over this PR and created 56be6db, thanks :)

@iluuu1994 iluuu1994 deleted the feature/array-access-assignment-to-scalar branch November 7, 2018 14:05
@iluuu1994

Copy link
Copy Markdown
Contributor Author

Cool 🙂

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.

2 participants