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

[RFC][make:entity] Add an option to generate fluent or not fluent setters #297

Open
maks-rafalko opened this issue Oct 19, 2018 · 5 comments

Comments

@maks-rafalko
Copy link

Hi everyone!

The only one mention about fluent setters I found is here: #104, but nothing has been implemented as far as I see from the source code.

There is a flag that checks whether to generate fluent setters or not, here

private function makeMethodFluent(Builder\Method $methodBuilder)
{
if (!$this->fluentMutators) {
return;
}

but it's impossible to override the default value (which is true - setters are fluent), because manipulator class is instantiated always with true:

$manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite);

Note the last parameter

public function __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true)

Proposal

What do you think about adding an option that will allow overriding this flag? For example:

bin/console make:entity --fluent-setters=false

or something like that.

If this would be useful from your point of view, I would be able to send a PR.

@javiereguiluz
Copy link
Member

@borNfreee thanks for your proposal. At first sight I'd say we shouldn't make this configurable ... because the current behaviour is configurable (more precisely, it's flexible).

If you generate fluent setters, you can decide if you prefer to use them fluently or not. Both ways work. But if you don't make them fluent or make it configurable ... user must check which is the format to use, etc.

@maks-rafalko
Copy link
Author

maks-rafalko commented Oct 19, 2018

But if you don't make them fluent or make it configurable ... user must check which is the format to use, etc.

Could you please explain what do you mean here?

Some background of why I think there should be an option:

  1. We are using api-platform and Sonata Admin, and to take everything from this 2 libraries, unfortunately, we have to use getters/setters.

  2. When we generate entities, we have to go through each method and replace return type, and remove return $this line

  3. Developer can miss one of the methods, ending up with 2 types of setters - one is fluent, another is not (just a human mistake, that's easy)

  4. When you use a fluent setter (alone, not as a chain), for example, $user->setName('John');, my IDE with PhpInspection EA Extended plugin complains that the returned value is not used and is being ignored, underlining it with a red line which is quite annoying.

  5. https://ocramius.github.io/blog/fluent-interfaces-are-evil/

@Nyholm
Copy link
Member

Nyholm commented May 27, 2019

If this should be implemented, then we maybe should support it to be configurable with the bundle's configuration.

@weaverryan
Copy link
Member

Yea, I'd be fine with this - as configuration in the bundle itself (not as an interactive question)... which I think would work better for your use-case anyways (one consistent way of generating entities through a project, every time).

Someone just needs to add the config and make the generate respect it - it's a "fairly" easy change :).

Cheers!

@Nyholm
Copy link
Member

Nyholm commented Jun 16, 2019

If someone tries to implement this: remember that some generated code rely on these fluid interfaces.

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

No branches or pull requests

4 participants