-
Notifications
You must be signed in to change notification settings - Fork 2
Add a Pure attribute for functions and methods. #2
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
Crell
wants to merge
4
commits into
main
Choose a base branch
from
pure-attribute
base: main
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b689d6
Add a Pure attribute for functions and methods.
Crell 0ab2db9
Update Pure attribute to the new template.
Crell 99888eb
Move SHOULD/MUST statements to each bullet point
Crell 1b9b88e
Combine sections now that the SHOULD/MUST is repeated.
Crell 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Fig\Attributes; | ||
|
|
||
| /** | ||
| * Indicates that a function or method is a "pure" function. | ||
| * | ||
| * ## Target audience | ||
| * | ||
| * Implementors | ||
| * - Static analysis tooling | ||
| * - Code compilation and optimization tooling (such as for compiled containers | ||
| * or event dispatchers, etc.) | ||
| * Users | ||
| * - Users who want their static analysis tools to double-check their use of | ||
| * pure functions. | ||
| * | ||
| * ## Purpose | ||
| * | ||
| * A pure function is a function or method that has the following properties, | ||
| * per [Wikipedia](https://en.wikipedia.org/wiki/Pure_function): | ||
| * | ||
| * 1. The function return values are identical for identical arguments | ||
| * (no variation with local static variables, non-local variables, mutable | ||
| * reference arguments or input streams, i.e., referential transparency). | ||
| * 2. The function has no side effects (no mutation of non-local variables, | ||
| * mutable reference arguments or input/output streams). | ||
| * | ||
| * In PHP, that means a pure function: | ||
| * | ||
| * 1. MUST NOT use or modify any global variable. | ||
| * 2. MIST NOT perform any IO operations, even read-only ones. | ||
| * 3. MUST NOT modify any property of an object provided to it as an | ||
| * argument, even transitively. | ||
| * 4. MUST NOT read from an object property on the same object unless | ||
| * that property is `readonly`. | ||
| * 5. MUST NOT accept a parameter by reference. | ||
| * 6. MUST NOT call any function that is not also marked "pure." (It | ||
| * may invoke a callable passed to it as an explicit argument.) | ||
| * | ||
| * ## When should the attribute be applied | ||
| * | ||
| * When a user marks a function or method as pure, it means they are asserting | ||
| * that the function conforms to the rules above. | ||
| * | ||
| * If this attribute is placed on a method in an interface, it means that all | ||
| * implementations of that method MUST themselves be marked pure. | ||
| * | ||
| * Static analysis tools | ||
| * - SHOULD verify that the function indeed meets the rules above, | ||
| * and treat it as an error if not. | ||
| * - MUST treat PHP standard library functions that conform to these | ||
| * rules as pure. As an example, `strtolower()` is pure. `sort()` | ||
| * is not. | ||
| * - MUST flag as an error any function that is marked pure but is | ||
| * demonstrably not. | ||
| * - MUST treat any method implementing an interface method marked | ||
| * pure as pure. | ||
| * | ||
| * Optimization tools and pre-compilation tools: | ||
| * - MAY implement optimizations based on the knowledge that a function | ||
| * is pure. For example, they may safely cache its results indefinitely, | ||
| * or inline its code into another routine. | ||
| * | ||
| */ | ||
Crell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[\Attribute(\Attribute::TARGET_FUNCTION | \Attribute::TARGET_METHOD)] | ||
| class Pure {} | ||
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.