Skip to content
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

Tap fluent chainable #53753

Draft
wants to merge 8 commits into
base: 11.x
Choose a base branch
from
Draft

Conversation

shaedrich
Copy link
Contributor

Refactor chainable/fluent class methods to use tap() where possible

This doesn't require new tests as the existing tests not failing should prove that there are no side-effects outside of the changed methods

Rules

  • Only methods that return $this and one additional line will be reduced to a one-liner (since additional lines could not be transformed into an arrow function. Using normal functions would nullify the line reduction benefit. Only using the last line before the return $this would look inconsistent)
    • These are mostly setters
    public function setA($a)
    {
    - $this->a = $a;
    - 
    - return $this;
    + return tap($this, fn () => $this->a = $a);
    }
  • When the last operation before the return $this in a method is a method call on $this, the higher-order tap is used instead
  • In two cases, this could be further optimizes by passing a first-class callable as the second argument

Since arrow functions need to return something but unset() is a keyword that returns void and not a function
Since tap tries to call the methods technically from outside
@shaedrich
Copy link
Contributor Author

fyi: The two remaining failed tests failed without a conclusive error message

@crynobone
Copy link
Member

crynobone commented Dec 4, 2024

The tests are failing due to a segmentation fault. This is not easy to debug but could be due to performance or failure but PHPUnit are no longer able to handle printing the correct output due to the complexity of new code.

Converted to draft since tests are failing.

@devfrey
Copy link
Contributor

devfrey commented Dec 4, 2024

Reducing the number of lines does not make code better. I find this more complex to read than before. Changes like these also make it harder for newcomers to contribute. Not to mention the (albeit minor) impact on performance.

@timacdonald
Copy link
Member

timacdonald commented Dec 4, 2024

I love tap. Big tap fan. Huge, even. Maybe the biggest. tap all day, every day.

But tap in a pure setter always feels off to me.

@crynobone crynobone marked this pull request as draft December 5, 2024 00:26
@Jacobs63
Copy link
Contributor

Jacobs63 commented Dec 5, 2024

This really adds no value.

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.

5 participants