Skip to content

Fix pure annotations - First pass#605

Merged
ramsey merged 2 commits intoramsey:fix-pure-annotationsfrom
fillerwriter:fix-pure-annotations
Jun 25, 2025
Merged

Fix pure annotations - First pass#605
ramsey merged 2 commits intoramsey:fix-pure-annotationsfrom
fillerwriter:fix-pure-annotations

Conversation

@fillerwriter
Copy link
Copy Markdown
Contributor

Coming from Re-add the @pure annotations PR, this is an attempt to fix the errors raised by the current phpstan settings.

Description

I went through each of the errors raised by phpstan with the following approach.

  • If a method is part of an @immutable class, we can consider it pure, assuming it only affects internal variables.
  • If a potentially pure method is calling a class's method that is only swapped during testing (and not during normal usage), then we can consider the calling method pure.
  • If a class is marked deprecated, don't bother with attempting to mark it pure or immutable.

There's a lot of ignores that have been placed for this PR, so a second set of eyes is welcome and appreciated.

@ramsey, I have this PR set to merge into uuid/fix-pure-annotations, but happy to switch to uuid/main if that makes more sense.

One thing to note that bugs me about the initial state of this PR is phpstan flags about 40 different method.resultUnused issues in the tests folder. Generally, these tests are either for testing exceptions, or as part of a benchmarking suite. In both cases, the code works as intended. However, having to explicitly flag phpstan to ignore these feels like a downgrade to the developer experience here, and might be an additional minor hindrance to external contrib. My view is likely skewed by placing all the exceptions at the same time, but I would be open to a different approach that automatically filters those warnings in the test folder.

How has this been tested?

This has been tested by running composer run-script test locally. No actual code changes were made, this should all be phpstan comments.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

PR checklist

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.

@fillerwriter fillerwriter requested a review from ramsey as a code owner June 24, 2025 19:42
@ramsey
Copy link
Copy Markdown
Owner

ramsey commented Jun 25, 2025

Quick note before I dive into reviewing this...

One thing to note that bugs me about the initial state of this PR is phpstan flags about 40 different method.resultUnused issues in the tests folder.

I know what's going on here, but it's strange to me that PHPStan only started complaining about these when I added the @pure annotations back to the source. Very weird.

Copy link
Copy Markdown
Owner

@ramsey ramsey 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 so much! This looks great!

/** @phpstan-ignore possiblyImpure.methodCall */
$bytes = $this->getBytes($encodedUuid);

/** @phpstan-ignore possiblyImpure.methodCall, possiblyImpure.methodCall */
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It's so wild you have to list the error ID twice, since there are two occurrences of it here. It makes sense, but I wouldn't have guessed it.

@ramsey ramsey merged commit 147b960 into ramsey:fix-pure-annotations Jun 25, 2025
1 check passed
@ramsey ramsey mentioned this pull request Jun 25, 2025
7 tasks
ramsey pushed a commit that referenced this pull request Jun 25, 2025
Coming from #603, this is an attempt
to fix the errors raised by the current phpstan settings.

I went through each of the errors raised by phpstan with the following
approach.

- If a method is part of an `@immutable` class, we can consider it pure,
  assuming it only affects internal variables.
- If a potentially pure method is calling a class's method that is only
  swapped during testing (and not during normal usage), then we can
  consider the calling method pure.
- If a class is marked deprecated, don't bother with attempting to mark
  it pure or immutable.
ramsey pushed a commit that referenced this pull request Jun 25, 2025
Coming from #603, this is an attempt
to fix the errors raised by the current phpstan settings.

I went through each of the errors raised by phpstan with the following
approach.

- If a method is part of an `@immutable` class, we can consider it pure,
  assuming it only affects internal variables.
- If a potentially pure method is calling a class's method that is only
  swapped during testing (and not during normal usage), then we can
  consider the calling method pure.
- If a class is marked deprecated, don't bother with attempting to mark
  it pure or immutable.
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