magento 2 development - yireo · pdf filean overview of magento 2 development compared to...
TRANSCRIPT
Yireo Educational Whitepaper - Magento 2 Development
Magento 2 DevelopmentYireo Educational Whitepaper
An overview of Magento 2 developmentCompared to Magento 1, the structure of classes within Magento 2 is completely different: Instead
of a Mage factory that binds parts together, modules now use Dependency Injection to import
other objects into their own classes. While events and observers are quite the same, the new
functionality of Plugins allows you to override original class methods, instead of rewriting the
entire class like you needed to under Magento 1. Composer offers a modular installation
mechanism, solving the problem of requirements and module dependencies, but also opening up
any code for existing composer-based projects like Monolog and Guzzle.
Magento 2 development is different, exciting and offers many more opportunities. It is important
to learn things properly, because when things get overwhelming the results might become messy.
Modern concepts with a Magento flavorMagento 2 implements Dependency Injection, but Magento would not be Magento if things were
not highly configurable: DI classes are not inserted by their actual class name, but by the interface
they implement. Adding to this, Magento offers XML configuration options (preferences) to replace
the default implementation with your own, as long as the same interfaces (or contracts) are
followed. Also, some classes generate other classes, leading to concepts like factories, plugins,
proxies and virtual types.
Getting the tools rightBecause Magento 2 is different, getting your tools right is an important first step: It is best to start
developing using PhpStorm, with its coding standards properly configured and XSD locations setup
to correctly interpret Magento 2 XML files. On the server level, composer and magerun2 are
configured, while your entire project lives in git. Not following the new standards will make
development hard and cumbersome.
Yireo - Opening Up Technology - Copyright 2016 Yireo.com - page 1 of 3
Yireo Educational Whitepaper - Magento 2 Development
Code smells with Magento 2While Magento 2 offers a great new framework, that incorporates praised concepts like
Dependency Injection, this does not mean things are perfect. For instance, after a while it becomes
easy to insert dependencies into your own class, simply by inserting classname or interfaces into
your constructor. However, having a list of dozens of dependencies will make your class hard to
read and understand. Most likely, a constructor with too many dependencies is a code smell that
suggests the creation of more classes that each do a single thing.
The same counts for calling upon the Object Manager directly: Sometimes it is easy to simply call
upon the Object Manager to fetch you the right object. While in test classes this might be legit, the
usage of the Object Manager in classes is not to be preferred.
Creating code in Magento 2 does not guarantee clean code. Developers should be aware of this
and should work on refactoring and unit testing to improve their code. Concepts like SOLID, DRY
and object calisthenics help developers improve their work.
Unit testingIf better code is the end goal, unit testing is definitely a reliable way to get there. While it is yet
unsure how Magento itself will try to enforce tests, creating tests is a new way of living. By creating
a test method for actual class methods, you will be forced to refactor code, create additional
methods and classes, think about concepts like Single Responsibility etcetera. While 100% code
coverage is disputable, the exercise of creating unit testing (no matter in what degree) is healthy.
Yireo - Opening Up Technology - Copyright 2016 Yireo.com - page 2 of 3
Yireo Educational Whitepaper - Magento 2 Development
Tips for writing Magento 2 code• Develop only Developer Mode and only run production sites in Production Mode.
• Make sure to configure PhpStorm to the correct XSD files, so that all of your XML code is
instantly validated.
• Enable the Magento Developer module for better debugging (magento module:enable
Magento_Developer).
• Try to limit the number of objects you are pulling via DI constructor injection. Having 20
dependencies in your own class is definitely a code smell. Optionally use separate helpers
to defer Dependency Injection.
• Try to create interfaces for all of your classes.
• When injecting other objects into your own object using DI, prefer the usage of interfaces
instead of objects.
• Never ever call upon the Object Manager directly. Instead use a factory for this.
• Try to stay away from factories. Your code is more versatile if you don’t need them.
• Create separate packages for your business logic, free of Magento dependencies and try to
write them as if you were writing them for Laravel or Symfony.
• Don’t forget your towel.
Magento 2 Development TrainingYireo organizes classroom-based trainings, 1-on-1 trainings and customized workshops for
Magento 2 Development. Do you want to get started quickly with Magento 2 coding? Or do you
want your team of developers to learn the best practices properly? We are are to help you out.
Contact us via mail ([email protected]) so we can go through the options?
https://www.yireo.com/education/magento-education
About YireoYireo focuses on education and extension development for both Magento and Joomla. As a
knowledge provider pur sang, we seek to help you wherever we can.
Yireo - Opening Up Technology - Copyright 2016 Yireo.com - page 3 of 3