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

Rule: Keyword new should not be used #17

Open
jissereitsma opened this issue Apr 29, 2018 · 10 comments
Open

Rule: Keyword new should not be used #17

jissereitsma opened this issue Apr 29, 2018 · 10 comments
Assignees
Labels
experimental Should be implemented with "experimental" severity to try it out

Comments

@jissereitsma
Copy link
Contributor

The new keyword should not be used to instantiate new classes. Constructor DI needs to be used instead.

@jissereitsma
Copy link
Contributor Author

This was coined by @renttek in #8 I personally do not agree with this, as this would limit non-Magento code too much. However, it is open for debate.

@avstudnitz
Copy link
Contributor

I agree that there are some use cases when new is a good approach. It shouldn't be used for Magento classes though.

@fooman
Copy link

fooman commented Apr 30, 2018

It's used heavily for exceptions:

          throw new \Magento\Framework\Exception\LocalizedException(
                __('My Message here')
            );

How about for a data transport object?

    $transport = new \Magento\Framework\DataObject(
        ['test' => 'data']
    );
    $this->eventManager->dispatch(
        'custom_event',
        [
            'transport' => $transport
        ]
    );

@jissereitsma
Copy link
Contributor Author

Completely agree with @fooman. Theoretically, exceptions and DataObject could be generated through factories as well, that are again injected in the constructor. However, this takes the DI pattern so literally, that it starts to be smelly.

@renttek
Copy link

renttek commented Apr 30, 2018

I think the use of new is not bad per se. There are many cases like throwing Exceptions, object creation in Factories or for creating DTOs (like \Magento\Framework\DataObject) where new is absolutely ok.
I many other cases the usage of new creates side effects, which are also can make code more complex to test.
The question is, can/should this be checked?

@navarr
Copy link

navarr commented Apr 30, 2018

Just wanted to add the bit that: Magento architects have said Exceptions should be created with the new keyword. I got in trouble for creating an ExceptionFactory.

@schmengler
Copy link
Collaborator

@navarr thank god, there's still some sanity :D I started a neverending argument when replacing a new DataObject with a domain specific object without using a factory.

While I don't agree with "never use new", I see the need for at least a soft rule that encourage dependency injection, especially for service-like classes.

A random thought: Would it make sense to "not use new to create instances of @api classes"?

@schmengler schmengler added on agenda of hangout This issue will be discussed in the next open hangout and removed debate needed labels May 15, 2018
@schmengler schmengler added experimental Should be implemented with "experimental" severity to try it out and removed on agenda of hangout This issue will be discussed in the next open hangout labels Jun 4, 2018
@schmengler
Copy link
Collaborator

We agreed that the spirit of the rule is good, namely to encourage dependency injection, but it may be too strict. We want to try it out for a while, with a low warning level and of course document when and why this makes sense.

@schmengler
Copy link
Collaborator

Thank you @lenaorobei, I included and documented the rule in #45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental Should be implemented with "experimental" severity to try it out
Projects
None yet
Development

No branches or pull requests

7 participants