Share
Embed
Magento 2: PHP Development Best Practices
A talk about some of the most relevant features of the Magento 2 architecture and how they relate to PHP development best-practices, with a focus on do's and dont's for those who are looking to get a jump-start on Magento2 development.
Gabriel Somoza
September 27, 2017
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
68 slides
-
https://somoza.be 1. Understand how Magento 2 (M2) meets mainstream PHP
2. Get up to speed with some of the jargon around M2 development 3. Share 8 quick tips about how to build an open-source M2 component Goals for This Talk -
https://somoza.be 1. Magento Developers or modern PHP Developers 2. Magento
2 Developers 1. Distributing M2 components 2. Contributing to M2 3. Building internal components Who is this talk (primarily) for? -
https://somoza.be 1 Coding Standards M2 DOCS: Best Practices for Extension
Developers http://bit.ly/m2-best-practices SOLID Do not modify core Use the M2 framework Security Observers Reusable -
https://somoza.be 1 Coding Standards PSRs 1 through 4 Code Demarcation
Standard DocBlock Standard JavaScript Coding Standard JavaScript DocBlock Standard jQuery Widget Standard LESS Coding Standard HTML Style Guide -
https://somoza.be 1 Coding Standards Must for core contributions Just a
suggestion for everyone else, but please take it Kind-of-a-must for Magento Marketplace (see Extension Quality Program) -
https://somoza.be 9 Understand the OM Dependency Injection/Inversion Lifecycle Management Compilation
Scopes Special Patterns Translations Magento’s Container: the Object Manager (OM) -
https://somoza.be 9 Understand the OM 2pm - Manipulating Magento: make
it do what you want (Yoke Puts) -
https://somoza.be 3 Build Bridges The Stockbase M2 Module not the
topic only for examples -
https://somoza.be 3 Build Bridges Sample Component Structure • Api •
Block • Controller • etc • frontend • events.xml • backend • di.xml • module.xml • config.xml • Helper • view • composer.json • README.md • registration.php PSR-4 Namespace: Vendor\Package -
https://somoza.be 3 Build Bridges { "name": “stockbase/magento2-module", "type": "magento2-module", "license":
[ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { “Stockbase\\Module\\": “." } } "require": { "php": “~7.0.0|~7.1.0|~7.2.0”, “magento/module-catalog": “~100.1.0”, // snip, ship ... "dividebv/phpdivideiq": “^1.5” } } Example composer.json ✓ Package name -
https://somoza.be 3 Build Bridges { "name": “stockbase/magento2-module", "type": "magento2-module", "license":
[ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { “Stockbase\\Module\\": “." } } "require": { "php": “~7.0.0|~7.1.0|~7.2.0”, “magento/module-catalog": “~100.1.0”, // snip, ship ... "dividebv/phpdivideiq": “^1.5” } } Example composer.json ✓ Package name ✓ Component Type (module, theme, language) -
https://somoza.be 3 Build Bridges { "name": “stockbase/magento2-module", "type": "magento2-module", "license":
[ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { “Stockbase\\Module\\": “." } } "require": { "php": “~7.0.0|~7.1.0|~7.2.0”, “magento/module-catalog": “~100.1.0”, // snip, ship ... "dividebv/phpdivideiq": “^1.5” } } Example composer.json ✓ Package name ✓ Component Type (module, theme, language) -
https://somoza.be 3 Build Bridges { "name": “stockbase/magento2-module", "type": "magento2-module", "license":
[ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { “Stockbase\\Module\\": “." } } "require": { "php": “~7.0.0|~7.1.0|~7.2.0”, “magento/module-catalog": “~100.1.0”, // snip, ship ... "dividebv/phpdivideiq": “^1.5” } } Example composer.json ✓ Package name ✓ Magento 2 Dependencies ✓ Component Type (module, theme, language) -
https://somoza.be 3 Build Bridges { "name": “stockbase/magento2-module", "type": "magento2-module", "license":
[ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { “Stockbase\\Module\\": “." } } "require": { "php": “~7.0.0|~7.1.0|~7.2.0”, “magento/module-catalog": “~100.1.0”, // snip, ship ... "dividebv/phpdivideiq": “^1.5” } } Example composer.json ✓ External libraries ✓ Package name ✓ Magento 2 Dependencies ✓ Component Type (module, theme, language) -
https://somoza.be 3 Build Bridges Domain Layer Bridge Layer Magento 2
Your M2 Component Your Generic Domain or Web API Library Output Input XSDs APIs Events Provide APIs Events Consume 3rd Party Components Core Components Preferences Extend -
https://somoza.be 3 Build Bridges Domain Layer Bridge Layer Magento 2
dividebv/phpdivideiq stockbase/magento2- module Response Request Events Provide APIs Events Consume 3rd Party Components Core Components Preferences Extend -
https://somoza.be 3 Build Bridges ✓ Use Private Packagist (commercial) or
Satis for private packages -
https://somoza.be 4 Defensive Components Hide Information Default to “Private” Visibility
/ Access Modifiers Photo by Dale Gillard / CC BY public $bar; protected $foo; private $baz; public function foo() {} protected function bar() {} private function baz() {} -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY Avoid Getter & Setter Swarm Private Access Modifiers -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY “Setters don’t mean anything, anyway Marco Pivetta (@ocramius) “Getter and setter methods are evil Allen Hollub, 2003 Private Access Modifiers -
https://somoza.be 4 Defensive Components class MyAbandonedCartEmailSender { /** * Gathers
data and sends email * @return void */ public function process(CartInterface $cart) {} // Returns the email transport public function getEmailTransport(): EmailTransportInterface {} // Sets the email transport public function setEmailTransport( EmailTransportInterface $transport ) {} protected function calculateCartPriceForEmail(CartInterface $cart) {} } Photo by Dale Gillard / CC BY EVIL Private Access Modifiers Example -
https://somoza.be 4 Defensive Components # etc/di.xml <preference for=“Vendor\Package\Api\AbandonedCartEmailSenderInterface” type=“Vendor\Package\Service\MyEmailSender” />
interface Api/AbandonedCartEmailSenderInterface { /** * Gathers data and sends email * @return void */ public function process(CartInterface $cart); } Photo by Dale Gillard / CC BY Private Access Modifiers ✓ Tip for OSS: abstract to a meaningful interface -
https://somoza.be 4 Defensive Components # etc/di.xml <preference for=“Vendor\Package\Api\AbandonedCartEmailSenderInterface” type=“Vendor\Package\Service\MyEmailSender” />
interface Api/AbandonedCartEmailSenderInterface { /** * Gathers data and sends email * @return void */ public function process(CartInterface $cart); } Photo by Dale Gillard / CC BY Private Access Modifiers ✓ Convention: public component interfaces go in an “Api” folder -
https://somoza.be 4 Defensive Components interface Api/AbandonedCartEmailSenderInterface { /** * Gathers
data and sends email * @return void */ public function process(CartInterface $cart); } Photo by Dale Gillard / CC BY Private Access Modifiers ✓ We also got rid of getters / setters with little meaning or value -
https://somoza.be 4 Defensive Components interface AbandonedCartEmailSenderInterface { /** * Gathers
data and sends email * @return void */ public function process(CartInterface $cart); } Photo by Dale Gillard / CC BY Private Access Modifiers ✓ Rename public methods to something meaningful -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY Private Access Modifiers interface AbandonedCartEmailSenderInterface { /** * Gathers data and sends email * @return void */ public function sendForCart(CartInterface $cart); } ✓ Rename public methods to something meaningful -
https://somoza.be 4 Defensive Components interface AbandonedCartEmailSenderInterface { /** * Gathers
data and sends email * @return void */ public function sendForCart(CartInterface $cart); } Photo by Dale Gillard / CC BY ✓ Suggestion: rename interfaces so they reflect their value, meaning or role to their users Private Access Modifiers -
https://somoza.be 4 Defensive Components interface SendsAbandonedCartEmails { /** * Gathers
data and sends email * @return void */ public function sendForCart(CartInterface $cart); } Photo by Dale Gillard / CC BY ✓ Suggestion: rename interfaces so they reflect their value, meaning or role to their users Private Access Modifiers -
https://somoza.be 4 Defensive Components class MyEmailSender implements Api\SendsAbandonedCartEmails { private
$transport; public function __construct( EmailTransportInterface $transport ) { $this->transport = $transport; } public function sendForCart(CartInterface $cart) { // ... $this->transport->send(/* ... */); } private function calculateCartPriceForEmail( CartInterface $cart ) { // ... } } Photo by Dale Gillard / CC BY Private Access Modifiers -
https://somoza.be 4 Defensive Components class MyEmailSender implements Api\SendsAbandonedCartEmails { private
$transport; public function __construct( EmailTransportInterface $transport ) { $this->transport = $transport; } public function sendForCart(CartInterface $cart) { // ... $this->transport->send(/* ... */); } private function calculateCartPriceForEmail( CartInterface $cart ) { // ... } } Photo by Dale Gillard / CC BY ✓ Interface in “Api” folder Private Access Modifiers -
https://somoza.be 4 Defensive Components class MyEmailSender implements Api\SendsAbandonedCartEmails { private
$transport; public function __construct( EmailTransportInterface $transport ) { $this->transport = $transport; } public function sendForCart(CartInterface $cart) { // ... $this->transport->send(/* ... */); } private function calculateCartPriceForEmail( CartInterface $cart ) { // ... } } Photo by Dale Gillard / CC BY ✓ Default to private visibility Private Access Modifiers ✓ Interface in “Api” folder -
https://somoza.be 4 Defensive Components class MyEmailSender implements Api\SendsAbandonedCartEmails { private
$transport; public function __construct( EmailTransportInterface $transport ) { $this->transport = $transport; } public function sendForCart(CartInterface $cart) { // ... $this->transport->send(/* ... */); } private function calculateCartPriceForEmail( CartInterface $cart ) { // ... } } Photo by Dale Gillard / CC BY ✓ Dependencies injected during construction Private Access Modifiers ✓ Interface in “Api” folder ✓ Default to private visibility -
https://somoza.be 4 Defensive Components class MyEmailSender implements Api\SendsAbandonedCartEmails { private
$transport; public function __construct( EmailTransportInterface $transport ) { $this->transport = $transport; } public function sendForCart(CartInterface $cart) { // ... $this->transport->send(/* ... */); } private function calculateCartPriceForEmail( CartInterface $cart ) { // ... } } Photo by Dale Gillard / CC BY ✓ Public API now is meaningful and concise Private Access Modifiers ✓ Interface in “Api” folder ✓ Default to private visibility ✓ Dependencies injected during construction -
https://somoza.be 4 Defensive Components class MyEmailSender implements Api\SendsAbandonedCartEmails { private
$transport; public function __construct( EmailTransportInterface $transport ) { $this->transport = $transport; } public function sendForCart(CartInterface $cart) { // ... $this->transport->send(/* ... */); } private function calculateCartPriceForEmail( CartInterface $cart ) { // ... } } Photo by Dale Gillard / CC BY ✓ We don’t yet know if this would be useful publicly Private Access Modifiers ✓ Interface in “Api” folder ✓ Default to private visibility ✓ Dependencies injected during construction ✓ Public API now is meaningful and concise -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY ✓ Prevent unnecessary BC breaks ✓ Encourage more expressive APIs ✓ Design better abstractions ✓ More simple tests Some benefits: Private Access Modifiers -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY ๏ Will not play nice with several OM features like Proxies / Plugins ๏ Might not be compatible with some Magento-specific debugging / profiling tools WARNING: “final” classes and functions The OM uses inheritance for many of its features. Private Access Modifiers -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY Photo by Dale Gillard / CC BY Fail Fast(er) composer require webmozart/assert -
https://somoza.be 4 Defensive Components Fail Faster use Webmozart\Assert\Assert; Fail Fast(er)
composer require webmozart/assert -
https://somoza.be 4 Defensive Components use Webmozart\Assert\Assert; class Employee { public
function __construct($id) { Assert::integer($id); Assert::greaterThan($id, 0); } } Fail Fast(er) composer require webmozart/assert Fail Faster -
https://somoza.be 4 Defensive Components use Webmozart\Assert\Assert; class Employee { public
function __construct($id) { Assert::integer($id); Assert::greaterThan($id, 0); } } new Employee('foobar'); // => InvalidArgumentException: // Expected an integer. Got: string new Employee(-10); // => InvalidArgumentException: // Expected a value greater than 0. Got: -10 Fail Fast(er) composer require webmozart/assert Fail Faster -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY …and don’t forget to CATCH exceptions /** * Sends a test email */ public function execute() { try { $emailAddress = $this->getEmailParam(); $orders = $this->getOrders(); $this->mailSender->send($emailAddress, $orders); } catch (\InvalidArgumentException $e) { $this->logger->logException($e); $this->messageManager->addError($e->getMessage()); } } Fail Faster -
https://somoza.be 4 Defensive Components Photo by Dale Gillard / CC
BY Extremely Defensive PHP - Marko Pivetta Defensive Programming - Wikipedia More on Defensive Programming -
https://somoza.be 5 Automated Tests 1. Unit test: generously, or use
TDD 2. Integration test: interactions (where unit tests are not enough) and data flows 3. Functional / UI test: only “happy” / critical paths 4. Follow core testing standards Unit Functional UI by Mike Cohn -
https://somoza.be 5 Automated Tests ✓ Exclude them from “production” releases:
# .gitattributes /Test export-ignore ✓ Distribute tests in your component’s source (free extra docs!) -
https://somoza.be 5 Automated Tests ✓ Show badges for build status
and code quality shields.io -
https://somoza.be 5 Automated Tests Awesome FREE Tools ๏ Travis CI
๏ ContinuousPHP ๏ Jenkins ๏ A few other Builds ๏ Scrutinizer ๏ Code Climate ๏ Codacy ๏ Your own Code Quality ๏ GrumPHP ๏ CodeSniffer ๏ Mage EQP ๏ PHP MD ๏ Many more Standards Next up - Automatically deploying magento2 without losing sleep (Ike Devolder) -
https://somoza.be 6 Documentation What goes in README.md? ✓ Build Status
✓ Description & Features ✓ Link to external docs ✓ Installation ✓ Requirements ✓ License ✓ Contributing Guidelines -
https://somoza.be 6 Documentation ✓ Build Status ✓ Description & Features
✓ Link to external docs ✓ Installation ✓ Requirements ✓ License ✓ Contributing Guidelines What goes in README.md? -
https://somoza.be 6 Documentation ✓ Build Status ✓ Description & Features
✓ Link to external docs ✓ Installation ✓ Requirements ✓ License ✓ Contributing Guidelines What goes in README.md? -
https://somoza.be 7 packagist.org $where .= "id IN ("; foreach($diff as
$id) { $where .= "'$id'"; if($id != end($diff)) { $where .= ','; } } $where .= “)”; $where = 'id IN ('.implode(',', $diff).')'; ≈ -
https://somoza.be 7 packagist.org if (in_array($inputParam, $allowedValues, true)) { // do
something } else { throw new \InvalidArgumentException(sprintf( 'Value "%s" is not allowed.', $value )); } Assert::oneOf($inputParam, $allowedValues); // do something here ≈ -
https://somoza.be 7 packagist.org Photo by Dale Gillard / CC BY
Photo by Dale Gillard / CC BY webmozart/assert ramsey/uuid bookdown/bookdown mockery/mockery league/factory-muffin league/oauth2-client guzzlehttp/guzzle monolog/monolog facebook/graph-sdk jms/serializer phpmd/phpmd phpseclib/phpseclib