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 more psalm findings #2338

Merged
merged 6 commits into from
Jul 18, 2021
Merged

Fix more psalm findings #2338

merged 6 commits into from
Jul 18, 2021

Conversation

franmomu
Copy link
Contributor

@franmomu franmomu commented Jul 6, 2021

Q A
Type improvement
BC Break no
Fixed issues

Summary

Now there are 23 issues in psalm level 6, this PR fixes 15 of them and for the rest I'll update #2329 so I think it would be easier to review.

@franmomu franmomu force-pushed the more_psalm_fixes branch from 13f84a8 to 9f9c8ee Compare July 6, 2021 10:58
*/
public function getClassMetadata($className): ClassMetadata
{
$metadata = $this->metadataFactory->getMetadataFor($className);
assert($metadata instanceof ClassMetadata);
Copy link
Contributor Author

@franmomu franmomu Jul 6, 2021

Choose a reason for hiding this comment

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

I added this assert in another PR to make SA tools happy, but since this is a performance sensitive method, I think it's better to just ignore the issue which is related to vimeo/psalm#5788

Copy link
Member

Choose a reason for hiding this comment

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

People are meant to disable assert in production mode anyway :)

Copy link
Member

Choose a reason for hiding this comment

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

But given we need @psalm-suppress for two other things I'm fine with this 👍

@@ -124,7 +124,7 @@ private function createInitializer(
}

/**
* @return string[]
* @return array<int, string>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -742,7 +742,7 @@ public function enableShardingForDbByDocumentName(string $documentName): void
}
}

private function runShardCollectionCommand(string $documentName, ?WriteConcern $writeConcern = null): array
private function runShardCollectionCommand(string $documentName, ?WriteConcern $writeConcern = null): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This value was used prior to #1848, but after that it is not used anymore.

@@ -213,7 +213,7 @@ final class UnitOfWork implements PropertyChangedListener
* At the end of the UnitOfWork all these collections will make new snapshots
* of their data.
*
* @psalm-var array<string, PersistentCollectionInterface<array-key, object>>
* @psalm-var array<string, array<PersistentCollectionInterface<array-key, object>>>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

my fault, it was an array containing another one:

$this->visitedCollections[spl_object_hash($topmostOwner)][] = $value;

@@ -1626,6 +1626,8 @@ public function removeFromIdentityMap(object $document): bool
* @throws InvalidArgumentException If the class does not have an identifier.
*
* @template T of object
*
* @psalm-suppress InvalidReturnStatement, InvalidReturnType because of the inability of defining a generic property map
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these two suppresses are because the same cause than in #2328 (comment)

@@ -1046,7 +1046,7 @@ public function prepareQueryOrNewObj(array $query, bool $isNewObj = false): arra
continue;
}

if (isset($key[0]) && $key[0] === '$' && is_array($value)) {
if (strpos((string) $key, '$') === 0 && is_array($value)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We were getting:

ERROR: InvalidArrayAccess - lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php:1049:23 - Cannot access array value on non-array variable $key of type array-key (see https://psalm.dev/005)
            if (isset($key[0]) && $key[0] === '$' && is_array($value)) {

Copy link
Member

Choose a reason for hiding this comment

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

Current code is less performant, strpos will go through entire string looking for $ while what we had previously were taking a first char and checking whether it's a $. Instead we should ensure that $key is a string and we'll be ok psalm-wise: https://psalm.dev/r/8e2a12f216

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense 👍, I had used strpos to avoid the isset check, but didn't think about not having $ and check the entire string.

Instead we should ensure that $key is a string and we'll be ok psalm-wise: https://psalm.dev/r/8e2a12f216

I've casted it to string since it was also done afterwards.

@franmomu franmomu mentioned this pull request Jul 6, 2021
foreach ($value as $key => $value) {
if (isset($key[0]) && $key[0] === '$') {
foreach ($value as $key => $notUsedValue) {
if (strpos((string) $key, '$') === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

same as above

@malarzm
Copy link
Member

malarzm commented Jul 17, 2021

Future me: please go and do #2329 once we merge this one

@malarzm malarzm merged commit e2c6dc4 into doctrine:2.3.x Jul 18, 2021
@malarzm
Copy link
Member

malarzm commented Jul 18, 2021

Thanks @franmomu!

@franmomu franmomu deleted the more_psalm_fixes branch July 18, 2021 16:48
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