Skip to content

[New Rule] Method chaining in class design MUST be avoided #25

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

Closed
lenaorobei opened this issue Jan 30, 2019 · 7 comments
Closed

[New Rule] Method chaining in class design MUST be avoided #25

lenaorobei opened this issue Jan 30, 2019 · 7 comments
Labels
accepted New rule is accepted new rule New feature implementation technical guidelines The rule is based on Magento Technical Guidelines

Comments

@lenaorobei
Copy link
Contributor

lenaorobei commented Jan 30, 2019

Rule

Method chaining in class design MUST be avoided.

Reason

Source: Magento Technical Guidelines.

According to technical guidelines:

Application SHOULD be structured in compliance with the CQRS principle.

Good explanation of why method chaining contradicts this principal was described by Martin Fowler in the "Domain-Specific-Languages" book:

Command-query separation is an extremely valuable principle in programming, and I strongly encourage teams to use it. One of the consequences of using Method Chaining in internal DSLs is that it usually breaks this principle - each method alters state but returns an object to continue the chain. I have used many decibels disparaging people who don't follow command-query separation, and will do so again.

Suggested Implementation Direction

Detect return $this; in methods.

@lenaorobei lenaorobei added proposal New rule proposal new rule New feature implementation technical guidelines The rule is based on Magento Technical Guidelines labels Jan 30, 2019
@paliarush paliarush added accepted New rule is accepted and removed proposal New rule proposal labels Jan 30, 2019
@lenaorobei lenaorobei added the Progress: good first issue Issues is easy to get started with label Jan 31, 2019
@orlangur
Copy link

@lenaorobei I do understand that such "useful" sniff is quite easy to implement but it is firstly not applicable to Magento codebase and secondly is wrong in general. For example, returning $this is perfectly valid in builders.

Could you please point out where its approval comes from? Like architectural meeting notes or something.

@lenaorobei
Copy link
Contributor Author

@orlangur the fact that the rule is accepted doesn't mean that implementation is accepted. The example I provided is just the suggested direction where to start the implementation. I'll fix the issue content.

Could you please point out where its approval comes from?

We have an agreement with core team that every rule from Magento Technical Guidelines is automatically accepted.

@lenaorobei lenaorobei removed the Progress: good first issue Issues is easy to get started with label Mar 1, 2019
@chaikovskyia
Copy link

@lenaorobei , why the rule is marked as MUST while applying CQRS principles is recommended (SHOULD)? Also does that mean that data storage classes and interfaces will not have setter methods anymore as currently there too many methods that are modifying state of an object?

@lenaorobei
Copy link
Contributor Author

Hello @chaikovskyia. This rule is the paragraph 2.15 of the Technical Guidelines.

In case of any concerns regarding this rule, please crate an issue in the magento/architecture repository. Looks like this rule requires additional explanation and some exceptions.

@lenaorobei
Copy link
Contributor Author

Cannot be implemented with PHP CodeSniffer.

@orlangur
Copy link

orlangur commented Apr 8, 2019

@lenaorobei why we cannot have it with exceptions for particular class types (extending DataObject, for instance)?

@lenaorobei
Copy link
Contributor Author

lenaorobei commented Apr 8, 2019

@orlangur we need to enforce this rule and can discuss use cases and appropriate tool.
We cannot use PHP CodeSniffer for that for sure, because this tool manipulates with file tokens only and does not know about parent classes, etc.

magento-devops-reposync-svc pushed a commit that referenced this issue Aug 24, 2021
…-coding-standard-228

[Imported] Moved abstract unit test to autoload-dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted New rule is accepted new rule New feature implementation technical guidelines The rule is based on Magento Technical Guidelines
Projects
None yet
Development

No branches or pull requests

5 participants