use the empty string instead of null as an array offset#12160
use the empty string instead of null as an array offset#12160xabbuh wants to merge 1 commit intodoctrine:3.5.xfrom
Conversation
0e2692f to
3aa7791
Compare
|
What prompted you to contribute this? Was it static analysis or was it tests? If it's static analysis and there are no unit tests covering theses cases, maybe we should use assertions instead? |
|
Sorry I forgot to mention. This is a deprecation that will be introduced with PHP 8.5. We caught this in the Symfony CI. So I enabled a PHP 8.5 job on my fork and these were the places that were caught by the tests. |
|
Oh ok so there are actual situations where this can happen 🤯 |
| { | ||
| if (isset($this->class->fieldMappings[$this->class->versionField]->inherited)) { | ||
| $definingClassName = $this->class->fieldMappings[$this->class->versionField]->inherited; | ||
| if (isset($this->class->fieldMappings[$this->class->versionField ?? '']->inherited)) { |
There was a problem hiding this comment.
If we take this one as an example, does it happen in a realistic scenario, or would it make sense to trigger a deprecation ourselves in the next major release?
There was a problem hiding this comment.
I have no idea. 🤷 But it's actually triggered a lot of times in the tests (see https://github.com/xabbuh/orm/actions/runs/17612071318/job/50036129395#step:6:84).
src/Query/SqlWalker.php
Outdated
| $sqlParts[] = $col . ' AS ' . $columnAlias; | ||
|
|
||
| $this->scalarResultAliasMap[$resultAlias][] = $columnAlias; | ||
| $this->scalarResultAliasMap[$resultAlias ?? ''][] = $columnAlias; |
There was a problem hiding this comment.
should it even be added in scalarResultAliasMap if there is no result alias ?
There was a problem hiding this comment.
I am not sure here about the implications if we don't add it. In other places we use the $scalarResultCounter property but I have no idea if that would work as expected here.
There was a problem hiding this comment.
after re-reading how the map is accessed in walkResultVariable() it's probably safe to assume that adding an entry here in case of $resultAlias being null doesn't make much sense
| // Get any aliases that are available for select expressions. | ||
| foreach ($AST->selectClause->selectExpressions as $selectExpression) { | ||
| $selectAliasToExpressionMap[$selectExpression->fieldIdentificationVariable] = $selectExpression->expression; | ||
| $selectAliasToExpressionMap[$selectExpression->fieldIdentificationVariable ?? ''] = $selectExpression->expression; |
There was a problem hiding this comment.
shouldn't we rather skip expressions that have no identification variable (instead of keeping only the last one as they will anyway override each other in that map) ?
There was a problem hiding this comment.
That's true, it didn't make a difference before as we never read from the map below when $orderByItem->expression was not a string. So we can just abstain from adding it.
| foreach ($orderByClause->orderByItems as $orderByItem) { | ||
| if (is_string($orderByItem->expression) && isset($selectAliasToExpressionMap[$orderByItem->expression])) { | ||
| $orderByItem->expression = $selectAliasToExpressionMap[$orderByItem->expression]; | ||
| if (is_string($orderByItem->expression) && isset($selectAliasToExpressionMap[$orderByItem->expression ?? ''])) { |
There was a problem hiding this comment.
this is useless, as is_string($orderByItem->expression) already ensures it is not null (same on next line)
| { | ||
| if (isset($this->class->fieldMappings[$this->class->versionField]->inherited)) { | ||
| $definingClassName = $this->class->fieldMappings[$this->class->versionField]->inherited; | ||
| if (isset($this->class->fieldMappings[$this->class->versionField ?? '']->inherited)) { |
There was a problem hiding this comment.
I think it will be clearer to handle the (common) case of classes without a version field explicitly instead of attempting an array check with the empty string.
| if (isset($this->class->fieldMappings[$this->class->versionField ?? '']->inherited)) { | |
| if ($this->class->versionField !== null && isset($this->class->fieldMappings[$this->class->versionField]->inherited)) { |
This PR was merged into the 6.4 branch. Discussion ---------- Fix ord()-related PHP 8.5 deprecations | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT The remaining deprecations will be fixed by doctrine/orm#12160 Commits ------- 5314fd5 Fix ord()-related PHP 8.5 deprecations
530099d to
3bb9931
Compare
|
thanks for the reviews, comments addressed, this is ready from my side |
|
I thought it might be great if PHPStan could help us in that regard. phpstan/phpstan#13592 |
src/Query/SqlWalker.php
Outdated
| $sqlParts[] = $col . ' AS ' . $columnAlias; | ||
|
|
||
| $this->scalarResultAliasMap[$resultAlias][] = $columnAlias; | ||
| if (is_string($resultAlias)) { |
There was a problem hiding this comment.
Let's make it explicit that this is actually a null-check.
| if (is_string($resultAlias)) { | |
| if ($resultAlias !== null) { |
src/Query/SqlWalker.php
Outdated
| $sqlParts[] = $col . ' AS ' . $columnAlias; | ||
|
|
||
| $this->scalarResultAliasMap[$resultAlias][] = $columnAlias; | ||
| if (is_string($resultAlias)) { |
cb1637c
|
#12191 handles this |
No description provided.