Skip to content

[Php71] Skip array dim fetch in BinaryOpBetweenNumberAndStringRector - #8862

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-9942-binary-op-phpdoc-array
Oct 27, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-9942-binary-op-phpdoc-array

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9942

BinaryOpBetweenNumberAndStringRector rewrote $where['adaid'] != '' to $where['adaid'] != 0 when $where carried a @param array{adaid?: int} phpdoc shape.

The number-side gate uses getNativeType(), but for an array dim fetch on a known string key of a constant-array shape the resolver falls back to the phpdoc type, so a phpdoc int leaks in as if it were a runtime guarantee. The element type is never guaranteed at runtime, so the rule should leave such code alone.

Bail out when either operand is an ArrayDimFetch. Added a skip fixture mirroring the reported case.

@TomasVotruba
TomasVotruba merged commit fa42c1e into main Oct 27, 2026
50 checks passed
@TomasVotruba
TomasVotruba deleted the fix-9942-binary-op-phpdoc-array branch October 27, 2026 21:12
@staabm

staabm commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

btw, I wonder why rector sees a different native type than phpstan does:

https://phpstan.org/r/a9dfae41-e50b-4052-b86d-c613788b71da

@TomasVotruba

Copy link
Copy Markdown
Member Author

Maybe trust docblock types option? But that would break many fixtures where docblocks are ignored

@staabm

staabm commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

treat phpdoc option does not influence this example: https://phpstan.org/r/c118560b-908a-4684-98b2-e2165000d2d5

I guess there is some "shortcut" or other workaround in rector which prevents that rector sees the same type than phpstan does.

but maybe thats something in the process of beeing resolved, since ondrej seem to strip most workarrounds in rector.

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

BinaryOpBetweenNumberAndStringRector should not trust phpdoc

2 participants