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

Use a more specific type for getSqlStatements() #11195

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

greg0ire
Copy link
Member

It is strictly beneficial for the Psalm baseline.

@mpdude
Copy link
Contributor

mpdude commented Jan 29, 2024

$i = -1;
foreach (array_reverse($classNames) as $className) {
$affected = false;
$class = $em->getClassMetadata($className);
$updateSql = 'UPDATE ' . $quoteStrategy->getTableName($class, $platform) . ' SET ';
foreach ($updateItems as $updateItem) {
$field = $updateItem->pathExpression->field;
if (
(isset($class->fieldMappings[$field]) && ! isset($class->fieldMappings[$field]['inherited'])) ||
(isset($class->associationMappings[$field]) && ! isset($class->associationMappings[$field]['inherited']))
) {
$newValue = $updateItem->newValue;
if (! $affected) {
$affected = true;
++$i;
} else {
$updateSql .= ', ';
}
$updateSql .= $sqlWalker->walkUpdateItem($updateItem);
if ($newValue instanceof AST\InputParameter) {
$this->sqlParameters[$i][] = $newValue->name;

Not sure this is guaranteed to be a list... It might increment the counter in line 108, but not meet the condition in 116.

Copy link
Contributor

@mpdude mpdude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, see above

@greg0ire
Copy link
Member Author

greg0ire commented Jan 29, 2024

I wish there was a way to express the fact that the keys don't matter other than mixed[], because then it seems that we neglected to specify the type of they keys, when what we really mean is: "the keys don't matter, don't rely on them".

Also, see how the returned property is currently annotated (although yes, it might very well be a lie depending on what you mean by list):

/** @var list<string>|string */
protected $sqlStatements;

@SenseException
Copy link
Member

SenseException commented Jan 29, 2024

I interpreted list<> rather as "the keys have no meaning" than "doesn't matter" (or maybe you meant the same thing). Because a tool like PHPStan defines a list also as sequential integer keys I'd usually see gaps in a sequence as array<int, string>.

Even if the keys don't matter, they can be (re-)used by a dev. I wonder if list<string> could break someone's PHPStan workflow of custom code build on ORM.

Does array_is_list() return true when used on $this->sqlStatements in some cases? I'd expect that there can happen some false cases.

@mpdude
Copy link
Contributor

mpdude commented Jan 29, 2024

To my understanding, the difference between array<T> and list<T> is that a list has keys that are the sequential integers 0, 1, 2, ...?

It is strictly beneficial for the Psalm baseline.
@greg0ire
Copy link
Member Author

greg0ire commented Jan 29, 2024

"the keys have no meaning" is indeed what I meant, I think. In this case, it's possible there are gaps, but we should not be careful to preserve them, I don't think they have a meaning.

To my understanding, the difference between array and list is that a list has keys that are the sequential integers 0, 1, 2, ...?

Yes, technically, but if we were using Python, the data structure we would be using here would be list. We wouldn't be using a dictionary, right? If it's not a list, that's an unwanted albeit unimportant side effect.

Anyway, I've amended my commit to make sure list is technically true, thus removing even more baseline blocks.

@greg0ire greg0ire requested a review from mpdude January 29, 2024 21:27
@greg0ire greg0ire added this to the 2.18.0 milestone Jan 30, 2024
@greg0ire greg0ire merged commit 8845b6d into doctrine:2.18.x Jan 30, 2024
58 checks passed
@greg0ire greg0ire deleted the more-specific-type branch January 30, 2024 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants