-
Notifications
You must be signed in to change notification settings - Fork 579
Implement @pure-unless-parameter-passed #6018
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
Open
zonuexe
wants to merge
15
commits into
phpstan:2.2.x
Choose a base branch
from
zonuexe:feature/pure-unless-parameter-passed
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,151
−19
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8aae47a
Parse @pure-unless-parameter-passed PHPDoc tag
zonuexe 68fdb67
Thread @pure-unless-parameter-passed through the reflection layer
zonuexe 1f0edd1
Wire @pure-unless-parameter-passed into the analyser
zonuexe 7c8046e
Mark by-ref out-parameter builtins as @pure-unless-parameter-passed
zonuexe 9e0daad
Add tests for @pure-unless-parameter-passed
zonuexe 3559a76
Cover the pre-PHP 8 parameter names for the by-ref out-parameter buil…
zonuexe 4f51502
Honor @pure-unless-parameter-passed on constructor calls, unpacked an…
zonuexe 1cde339
Parse @pure-unless-parameter-passed via phpdoc-parser 2.3.3
zonuexe 02f5a45
Report certain impurity when a @pure-unless-parameter-passed argument…
zonuexe 4902e4b
Recognize @phpstan-pure-unless-callable-is-impure as a known tag
zonuexe 472e24d
Report @pure-unless-parameter-passed on a non-optional parameter
zonuexe 0766a41
Expose @pure-unless-parameter-passed parameters as a function-level map
zonuexe 850d542
Test @pure-unless-parameter-passed with a variadic parameter
zonuexe d78c3df
Mark preg_filter() as @pure-unless-parameter-passed
zonuexe 915ecf0
Combine the callable and parameter-passed purity verdicts
zonuexe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add
?
searching with
'&[^']+=thruresources/functionMap.phpcould reveal even moreThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These optional by-ref parameters do have a side effect on the caller variable scope, but these functions have side effects as their primary purpose, so their purity does not change depending on whether the parameter is passed. In some cases it can be worth treating a function as
#[\NoDiscard], but these do not seem to be such cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a broader sweep of all optional by-ref out-params in
functionMap.php.preg_filteris a clean match (same signature as the already-coveredpreg_replace, pure regex substitution with an optionalcount), so I added it. The remaining plausible candidates (grapheme_extract,Spoofchecker::areConfusable/isSuspicious,IntlTimeZone::getCanonicalID) are borderline — their purity depends on ICU/locale process globals — so I left them out for now; happy to revisit if you think they qualify.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, here is the full breakdown of the borderline candidates I left out, in case any are worth a follow-up.
FITS but deferred (medium confidence — purity depends on ICU/locale process globals):
grapheme_extractnextGRAPHEME_EXTR_WORD/SENTENCEuse ICU break-iterators bound to the process-global default locale (Locale::setDefault()), so identical args can differ across calls.Spoofchecker::areConfusableerrorSpoofchecker::isSuspiciouserrorIntlTimeZone::getCanonicalIDisSystemIDLooks pure at first glance, but classified impure (clear side effect / non-determinism):
is_callablecallable_name"Class::method"string can trigger autoloading, independent of the by-ref param.mb_ereg/mb_eregiregistersmb_regex_encoding().idn_to_ascii/idn_to_utf8idna_infoIntlDateFormatter::parse/::localtimeparse_posdate_default_timezone_get()when the formatter has no timezone.xdiff_string_patcherroryaml_parsendocscallbacksarray takes arbitrary user callables (same class aspreg_replace_callback); that would needpureUnlessCallableIsImpureParametersoncallbacks, not this mechanism.ldap_parse_exop,ldap_control_paged_result_responseretdata/retoid,cookie/estimatedldap_control_paged_result_responseis also removed in PHP 8.0).Totals across the 79 optional/required by-ref out-param entries: 12 required-by-ref (skipped), 11 already handled, 51 impure, 5 fit (of which
preg_filteris added here).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for looking into it. I agree these are mostly borderline.
one stood out for me:
preg_replace_callback,preg_replace_callback_arrayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call.
preg_replace_callbacknow carries both flags — the two verdicts were checked in sequence, so a pure callback short-circuited before the$countcheck; I combined them withTrinaryLogic::and()(in both the call andnewpaths) and flagged its$count.preg_replace_callback_arrayI left out: its callbacks live inside anarray<string, callable>parameter, which thepureUnlessCallableIsImpureParametersmechanism (single callable param) cannot inspect, so flagging only its$countwould wrongly treat it as pure when a callback in the array is impure. That one needs a separate mechanism.