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

Add PHPStan check for iterable types #2437

Merged
merged 5 commits into from
Nov 6, 2022

Conversation

franmomu
Copy link
Contributor

Q A
Type improvement
BC Break no
Fixed issues

Summary

Last PR to add the PHPStan check for iterable types, the remaining ones where the ones in tests.

@franmomu franmomu added this to the 2.4.0 milestone Apr 30, 2022
Copy link
Member

@malarzm malarzm left a comment

Choose a reason for hiding this comment

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

I'm not saying "definitely no", but given few definitions are off and tests are green I'm not sure these changes provide any real value other than satisfying SA tools. Maybe we could stay with level 5 for tests and improve just the main codebase for higher levels? To add another pro some time ago while working on sanity checks I needed to ignore phpstan/psalm errors in tests because I wanted to pass wrong values to check if exceptions are raised properly.

@@ -64,6 +64,8 @@ public function provideAccumulators(): array
}

/**
* @param mixed[] $args
Copy link
Member

Choose a reason for hiding this comment

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

FWIW I'm not really a fan of adding mixed[] all over the place, IMO this is bringing little to no value

@@ -225,6 +225,9 @@ class MyEventListener
/** @psalm-var array<string, list<class-string>> */
public $called = [];

/**
* @param mixed[] $args
Copy link
Member

Choose a reason for hiding this comment

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

Can we somehow note that first element should be an instance of LifecycleEventArgs (and we don't care about the rest)?

@@ -162,6 +162,9 @@ public function getSubscribedEvents(): array
];
}

/**
* @param mixed[] $args
Copy link
Member

Choose a reason for hiding this comment

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

Same question as before with LifecycleEventArgs

@@ -43,6 +43,8 @@ public function testHintIsNotSetByDefault(): void
}

/**
* @param array<array<string, string>> $tags
Copy link
Member

Choose a reason for hiding this comment

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

Shall we introduce a ReadPreferenceTagsShape shape similar to SortShape?

@@ -114,6 +117,9 @@ public function getSubscribedEvents(): array
return $this->events;
}

/**
* @param mixed[] $args
Copy link
Member

Choose a reason for hiding this comment

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

Here goes LifecycleEventArgs question again

@@ -63,6 +63,9 @@ class MODM83EventListener
/** @var array<string, class-string[]> */
public $called = [];

/**
* @param mixed[] $args
Copy link
Member

Choose a reason for hiding this comment

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

I'm just marking all places with LifecycleEventArgs I see in case there'd be a place to improve (next 2 classes could use this as well)

@@ -78,7 +78,7 @@ public function __construct()
* by mapping array indexes (size URL's are required, cropMetadata is not).
* Any invalid elements will be ignored.
*
* @param array|Traversable $embeddedDocuments
* @param array<MODM95TestEmbeddedDocument>|Traversable<MODM95TestEmbeddedDocument> $embeddedDocuments
Copy link
Member

Choose a reason for hiding this comment

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

Both @param and code could be improved to use iterable<MODM95TestEmbeddedDocument> which wasn't available at the time of writing the test

@@ -229,6 +231,10 @@ public function getDocumentsAndExpectedData(): array
];
}

/**
* @param array<string, mixed> $expectedData
* @param array<string, mixed>|null $preparedData
Copy link
Member

Choose a reason for hiding this comment

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

I doubt that null should be allowed for $preparedData as the very first thing we do is iterate over it :)

@@ -249,6 +249,9 @@ public function testComputingChangesetForFileWithoutMetadataThrowsNoError(): voi
}

/**
* @param array<string, string>|null $origData
* @param array<string, string>|null $updateData
Copy link
Member

Choose a reason for hiding this comment

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

There are currently also ints and bools in the array

@malarzm malarzm modified the milestones: 2.4.0, 2.5.0 May 8, 2022
@franmomu franmomu force-pushed the specify_iterator_types_tests branch from c532cf0 to d6a5fa1 Compare November 3, 2022 23:31
@franmomu franmomu changed the base branch from 2.4.x to 2.5.x November 3, 2022 23:33
@franmomu franmomu closed this Nov 3, 2022
@franmomu franmomu reopened this Nov 3, 2022
@franmomu franmomu force-pushed the specify_iterator_types_tests branch 2 times, most recently from 7763fc5 to a035a32 Compare November 3, 2022 23:41
@franmomu
Copy link
Contributor Author

franmomu commented Nov 3, 2022

I'm not saying "definitely no", but given few definitions are off and tests are green I'm not sure these changes provide any real value other than satisfying SA tools. Maybe we could stay with level 5 for tests and improve just the main codebase for higher levels? To add another pro some time ago while working on sanity checks I needed to ignore phpstan/psalm errors in tests because I wanted to pass wrong values to check if exceptions are raised properly.

Thanks @malarzm for the review! I've finally found some time to take a look at this again. I've just updated the code with the comments and make it green improving a bit some of the mixed[] types.

I'll try to add another commit removing some of the phpdoc that don't add value and adding them to the baseline.

@franmomu franmomu force-pushed the specify_iterator_types_tests branch 2 times, most recently from d4679c9 to 4c337ea Compare November 4, 2022 00:14
@malarzm
Copy link
Member

malarzm commented Nov 4, 2022

Awesome! Let me know if you'd need anything :)

@franmomu franmomu force-pushed the specify_iterator_types_tests branch from 4c337ea to babf193 Compare November 5, 2022 12:44
@franmomu
Copy link
Contributor Author

franmomu commented Nov 5, 2022

I think it's ready to review, I understand the mixed[] vs array concern, to me when I see mixed[] it's like the type has been reviewed, but maybe list<mixed> would be better to express that it is list of mixed types and the key doesn't matter.

@franmomu franmomu requested a review from malarzm November 5, 2022 13:08
Copy link
Member

@malarzm malarzm left a comment

Choose a reason for hiding this comment

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

Thank you very much @franmomu for your hard work on this 🍻

@@ -224,6 +225,9 @@ class MyEventListener
/** @psalm-var array<string, list<class-string>> */
public array $called = [];

/**
* @param array{LifecycleEventArgs} $args
Copy link
Member

Choose a reason for hiding this comment

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

Cool this was possible :)

@malarzm malarzm merged commit 0981d3d into doctrine:2.5.x Nov 6, 2022
@franmomu franmomu deleted the specify_iterator_types_tests branch November 7, 2022 06:22
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.

2 participants