Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Pure.php
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.
*
Copy link
Contributor

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):

Suggested change
*
*
* Target audience:
* - Implementors: static analysis tools.
* - Users: packages which want to safeguard "pure" functions via static analysis tools.

Phrasing may need work, but hopefully you get my drift.

* 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
Copy link
Member

Choose a reason for hiding this comment

The 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.
Maybe we should repeat the "must not" on every line rather than just once?

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds good. It also.allows one to mix requirements like

  • MUST NOT do x
  • SHALL do y
  • but MUST do that

* 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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The 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

  1. footnote

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {}