Skip to content

DateTimeInterface::format() with Pure attribute #1749

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

integer
Copy link

@integer integer commented May 30, 2025

No description provided.

@LolGleb
Copy link
Contributor

LolGleb commented Jun 18, 2025

Thank you for your PR! After reviewing it, I believe that adding the #[Pure] attribute to the DateTimeInterface::format() method is not appropriate. Here’s why:

This method depends not only on the input argument, but also on the internal state of the object.

For example, calling the method with the same $format value can yield different results depending on the instance’s internal state:

$date1 = new DateTime('2023-02-03');
$date2 = new DateTime('2024-04-05');
echo $date1->format('Y-m-d'); // "2023-02-03"
echo $date2->format('Y-m-d'); // "2024-04-05"

This violates one of the key conditions of purity: the output of the method must be consistent given the same input arguments.

If you think I am wrong, please point out where I am mistaken.

@LolGleb LolGleb self-assigned this Jun 18, 2025
@LolGleb
Copy link
Contributor

LolGleb commented Jun 20, 2025

It turns out that for conditionally pure methods, where the method itself does not cause side effects, but its result may depend on the state of the object, you need to use the #[Pure(true)] attribute. This is exactly our case. Could you please make the changes in the PR?

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