Skip to content

Tell PHPStan which method returns which object - #383

Merged
aristath merged 1 commit into
developfrom
filip/hint-phpstan
Mar 27, 2025
Merged

Tell PHPStan which method returns which object#383
aristath merged 1 commit into
developfrom
filip/hint-phpstan

Conversation

@ilicfilip

Copy link
Copy Markdown
Collaborator

Context

Earlier today we had a fatal PHP error, it was caused by a method being removed on a branch which was merged into develop. A bit later other branch, which used the removed method, was also merged into develop but the method in question was missed (quite easily since we had so many changes).

The problem is that PHP error was triggered in a very specific use case, when plugin was updated, so it wasn't obvious.

But we have tests in place, so how did PHPStan miss it?
Because we use magic methods to get object, so PHPStan doesn't know which object to expect when do \progress_planner()->get_suggested_tasks()->get_tasks_by_status( 'pending' );

This PR adds @method annotations which tell PHPStan which methods to expect.
Since we're close to release I haven't added annotations for all classes, some of them require PHPStan fixes and I don't want to do that a day before the release.

The objects which we use most often are covered, most importantly \Progress_Planner\Suggested_Tasks and PHPStan errors and warnings for it are fixed.

I also tried implementing a more elegant approach, by defining a Dynamic Return Type Extensions, but it consumed too much memory and it was pretty slow.

Quality assurance

  • I have tested this code to the best of my abilities.
  • I have added unit tests to verify the code works as intended.
  • I have checked that the base branch is correctly set.

@ilicfilip
ilicfilip requested a review from aristath March 26, 2025 15:35
@github-actions

github-actions Bot commented Mar 26, 2025

Copy link
Copy Markdown
Contributor

Test merged PR on Playground
Test this pull request on the Playground or download the zip.

@aristath
aristath merged commit 78cc576 into develop Mar 27, 2025
@aristath
aristath deleted the filip/hint-phpstan branch March 27, 2025 06:06
@ilicfilip ilicfilip mentioned this pull request Apr 3, 2025
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