-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fig\Attributes; | ||
|
||
/** | ||
* Indicates that a function or method is a "pure" function. | ||
* | ||
* A pure function is a function or method that has the following properties: | ||
* | ||
* 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 MUST NOT: | ||
* | ||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to reread this part before I noticed that the Must not was a list of prohibited operations for pure functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds good. It also.allows one to mix requirements like
|
||
* 1. Use or modify any global variable. | ||
* 2. Perform any IO operations, even read-only ones. | ||
* 3. Modify any property of an object provided to it as an argument, even transitively. | ||
* 4. Read from an object property on the same object unless that property is `readonly`. | ||
* 5. Call any function that is not also marked "pure." (It may invoke a callable passed to it as an explicit argument.) | ||
* | ||
* Many functions in the PHP standard library are already pure. Implementers MUST treat those as acceptable to call | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this more explicit somehow? Like a reference of functions? Right now it sounds like, you need to figure out yourself if functions are pure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The list is... not small. 90% of the string and array functions would fall into this category. Listing them here would be a challenge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 Options IMO:
|
||
* from a pure function. | ||
* | ||
* Implementers MAY choose to implement optimizations based on the information that a function is pure, such as caching. | ||
* | ||
* If this attribute is placed on a method in an interface, it means that all implementations of that method MUST | ||
* themselves be marked pure. Implementers MUST enforce this rule. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see a reference to where the information came.from so that people realize that this is not something we invented ourselves but is based on other peoples know-how. In this case a link to the Wikipedia page seems to be a good idea. Using either a1 or an {@ see} Annotation (oh the irony 😂) Footnotes
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to add the requirement of references to the meta-document.... |
||
#[\Attribute(\Attribute::TARGET_FUNCTION | \Attribute::TARGET_METHOD)] | ||
class Pure {} |
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.
Suggestion (also to consider for the template):
Phrasing may need work, but hopefully you get my drift.