Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Tech Guidelines: factories vs new keyword #3982

Merged
merged 3 commits into from
Jul 16, 2019

Conversation

buskamuza
Copy link
Contributor

@buskamuza buskamuza commented Mar 20, 2019

This PR is a:

  • New topic
  • Content update
  • Content fix or rewrite
  • Bug fix or improvement

Summary

Added the rule about using factories instead of new keyword.
See also discussion at magento/magento-coding-standard#64 (comment)

Additional information

List all affected URLs

❗️ older versions of Tech Guidelines should be also updated before merging this PR.

whatsnew
Added '2.2. Object instantiation' section to Technical Guidelines.

@devops-devdocs
Copy link
Collaborator

An admin must run tests on this PR before it can be merged.

@dshevtsov dshevtsov added the Major Update Significant original updates to existing content label Mar 20, 2019
@@ -98,6 +100,11 @@ class Config
{% endcollapsible %}
---

2.2.2. Factories SHOULD be used for object instantiation instead of `new` keyword. An object SHOULD be replaceable for testing or extensibility purposes.
Exception: DTOs. There is no behavior in DTOs, so there is no reason for its replaceability.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest it would be a good idea to either have an explanation for the term DTO here or link through to one.

- added reference to DTO definition
2.2.2. Factories SHOULD be used for object instantiation instead of `new` keyword. An object SHOULD be replaceable for testing or extensibility purposes.
Exception: [DTOs](https://en.wikipedia.org/wiki/Data_transfer_object). There is no behavior in DTOs, so there is no reason for its replaceability.
Tests can create real DTOs for stubs.
Data interfaces, Exceptions and `Zend_Db_Expr` are examples of DTOs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to add code samples where we can't test or extend some class and how using factories will help to solve this issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe having a blog post would be helpful? I'm afraid adding more explanations here would make the document grow and hard to read.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe having a blog post would be helpful? I'm afraid adding more explanations here would make the document grow and hard to read.

Good idea + add link to that post there

@dobooth
Copy link
Contributor

dobooth commented Jun 17, 2019

Hi @buskamuza Any update on this one?

@bdenham bdenham added 2.3.x Magento 2.3 related changes 2.2.x labels Jun 27, 2019
@keharper keharper added the Internal Dev Differentiates work between community and Magento staff label Jun 27, 2019
@buskamuza
Copy link
Contributor Author

This one can be merged. No more feedback received and I addressed existing one.

@dobooth
Copy link
Contributor

dobooth commented Jul 16, 2019

running tests

@dobooth dobooth merged commit 3f9f10c into magento:master Jul 16, 2019
@ghost
Copy link

ghost commented Jul 16, 2019

Hi @buskamuza, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
2.2.x 2.3.x Magento 2.3 related changes Internal Dev Differentiates work between community and Magento staff Major Update Significant original updates to existing content
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants