SlideShare a Scribd company logo
Code Quality with Magento 2
April 30th 2020 – Virtual Meet Magento Meetup #2
Andreas von Studnitz – integer_net – @avstudnitz
Co-Founder and Managing Director @ integer_net
Magento Developer, Trainer, Consultant
since 2008
Twitter: @avstudnitz
Andreas von Studnitz
Andreas von Studnitz – integer_net – @avstudnitz
2014
Image
Quality
Andreas von Studnitz – integer_net – @avstudnitz
2020
Code
Quality
Andreas von Studnitz – integer_net – @avstudnitz
Agenda
Magento-Specific Code Quality
Questions and Answers
General Code Quality
Tools for better Code Quality
Motivation
Andreas von Studnitz – integer_net – @avstudnitz
For developers
◦ Code is easy to read
◦ Easy to understand
◦ Easy to adapt
◦ Easy to extend
What is code quality good for?
For customers
• Fewer Bugs
• Reliability
• Security
• Updatability
• Less expensive long-term
General Code Quality
(independent of M2)
Andreas von Studnitz – integer_net – @avstudnitz
Soft Factors
• Code Reviews
• Working Hours
• Pair Programming
• Code Reviews
Andreas von Studnitz – integer_net – @avstudnitz
Keep it simple
• Minimal amount of LoC for demand – „only no code is
good code“
• Use existing functions: Generate price rules instead of
overwriting price logic
• Not going to break on an update
• Not going to show wrong VAT with tax included
• Not going to break on a credit memo
• Avoid unnecessary abstraction
“
Andreas von Studnitz – integer_net – @avstudnitz
“Debugging is twice as hard as
writing the code in the first place.
Therefore, if you write the code as
cleverly as possible, you are, by
definition, not smart enough to
debug it.”
- Brian Kernighan
Andreas von Studnitz – integer_net – @avstudnitz
Literature
Magento-specific
Code Quality
Andreas von Studnitz – integer_net – @avstudnitz
Updatability
• No core hacks, no hacks of external modules
• No changes of PHP classes
 Instead: Observer, Plugin or Preference
• No changes of template files
 Instead: Make a theme specific copy, or better use Layout XML
• No changes of language files
 Instead: Custom translation files, possibly in own language pack
Andreas von Studnitz – integer_net – @avstudnitz
From MVC to MVVM
• Model – View – ViewModel
• ViewModels provide data for the View
• PHP Classes which don’t need to inherit or implement
anything
• Read more: https://www.integer-net.com/view-models-
in-magento-1-and-2/
(by Fabian Schmengler)
Andreas von Studnitz – integer_net – @avstudnitz
An Example of View Models
module-catalog/view/frontend/layout/catalog_product_view.xml
module-catalog/view/frontend/templates/product/breadcrumbs.phtml
Andreas von Studnitz – integer_net – @avstudnitz
Configuration as Code
• Store configuration in the config.php and/or env.php
• bin/magento config:set --lock-config
• bin/magento config:set --lock-env
• Since Magento 2.2.4
• Can create websites and stores
• Can set store specific values
Andreas von Studnitz – integer_net – @avstudnitz
Example
Andreas von Studnitz – integer_net – @avstudnitz
Configuration: Suggestions
• config.php is for sharing
• Should contain everything you want to have control over
• Don’t add everything – some things are better kept in config.xml
• env.php is local only
• Add your base URLs
• Adjust security settings
• Disable services like Google Analytics
• Adjust payment methods and other sensitive data by environment
Andreas von Studnitz – integer_net – @avstudnitz
Setup Patches
• More “Configuration as Code” – don’t use the Magento
Admin for crucial changes
• Successor of Setup Scripts (since Magento 2.3)
• Attributes
• Content (CMS, categories, products, …)
• Accompanied by Declarative Schema for adjusting the
database schema
Andreas von Studnitz – integer_net – @avstudnitz
A Setup Patch
Tools for better Code
Quality with Magento 2
Andreas von Studnitz – integer_net – @avstudnitz
Automated Testing
• Huge improvement of M2 over M1
• In M1 automated testing not cost-effective in most mid-size cases
• In M2 automated testing is now cost effective in most mid-size
cases
• Different types of testing:
• Unit Tests
• Integration Tests
• Frontend Tests (MFTF)
Andreas von Studnitz – integer_net – @avstudnitz
Integration Tests
class NewProductsCategoryTest extends MagentoTestFrameworkTestCaseAbstractControlle
{
public static function loadFixture()
{
include __DIR__ . '/../_files/categories.php';
include __DIR__ . '/../_files/products.php';
}
public function testCategoryDisplaysCorrectProducts()
{
$this->dispatch('catalog/category/view/id/3');
$this->assertContains('Simple Product 1', $this->getResponse()->getBody());
$this->assertNotContains('Simple Product 2', $this->getResponse()->getBody());
}
}
• Hint: use https://github.com/tddwizard/magento2-fixtures
Andreas von Studnitz – integer_net – @avstudnitz
Static Tests
• Check the code with tools like PHP Codesniffer and PHP
Mess Detector
• Automate with GrumPhp: Code will be checked on
commit
• Only a few steps to setup
• Example:
Andreas von Studnitz – integer_net – @avstudnitz
Static Tests
• Check the code with tools like PHP Codesniffer and PHP
Mess Detector
• Automate with GrumPhp: Code will be checked on
commit
• Only a few steps to setup
• Example:
Andreas von Studnitz – integer_net – @avstudnitz
Static Tests
• Check the code with tools like PHP Codesniffer and PHP
Mess Detector
• Automate with GrumPhp: Code will be checked on
commit
• Only a few steps to setup
• Example:
#MM18DE
Installation of GrumPhp (1)
#MM18DE
Installation of GrumPhp (2)
grumphp.yml in project root directory
Andreas von Studnitz – integer_net – @avstudnitz
„The Obvious“
• Git for source code management and versioning
• Composer for managing dependencies
• PhpStorm
• See warnings and errors directly
• Easily integrate tools like Code Sniffer and Mess Detector
• Built-in commands for code formatting and small refactorings
Thank you!
Any Questions?
avs@integer-net.de
@avstudnitz
@integer_net
Presentation Template by SlidesCarnival

More Related Content

PPTX
PDF
Keeping business logic out of your UIs
PDF
Background threads, async communication and vaadin
PDF
Picking the right architecture and sticking to it
PPTX
zaid ppt.pptx
PPTX
MidwestPHP - Getting Started with Magento 2
PDF
Magento Worst Practice (Meet Magento Poland 2016)
PPTX
Building an OpenMRS Distribution - Lessons from KenyaEMR
Keeping business logic out of your UIs
Background threads, async communication and vaadin
Picking the right architecture and sticking to it
zaid ppt.pptx
MidwestPHP - Getting Started with Magento 2
Magento Worst Practice (Meet Magento Poland 2016)
Building an OpenMRS Distribution - Lessons from KenyaEMR

Similar to Code Quality with Magento 2 (20)

ODP
Beginners - Get Started With Unit Testing in .NET
PPTX
Introduction to Integration Tests in Magento / Adobe Commerce
PPTX
Introduction to Integration Tests in Magento / Adobe Commerce
PPTX
agile with scrum methodology
PPTX
Software Development : Jeremy Gleason Iscope Digital
PDF
Case Study - AMR Test Automation
PPTX
V model Over view (Software Engineering)
PPTX
V model Over View (Software Engineering)
PPTX
Magento 2 Best Practice Workfow // David Lambauer // Meet Magento 2017 // Lei...
PPTX
Madison PHP - Getting Started with Magento 2
ODP
(Re)inventing software development productivity
PPTX
WDS trainer presentation - MLOps.pptx
PPTX
Training: MVVM Pattern
PPTX
Meet Magento Spain 2019 - Our Experience with Magento Cloud
PPTX
Expert guidance on migrating from magento 1 to magento 2
PPTX
Udvid din test portefølje med coded ui test og cloud load test
PDF
Blending Realized Code
ODP
Moodle Development Best Pracitces
PDF
MidoNet roadmap
PDF
MidoNet Vision & Roadmap
Beginners - Get Started With Unit Testing in .NET
Introduction to Integration Tests in Magento / Adobe Commerce
Introduction to Integration Tests in Magento / Adobe Commerce
agile with scrum methodology
Software Development : Jeremy Gleason Iscope Digital
Case Study - AMR Test Automation
V model Over view (Software Engineering)
V model Over View (Software Engineering)
Magento 2 Best Practice Workfow // David Lambauer // Meet Magento 2017 // Lei...
Madison PHP - Getting Started with Magento 2
(Re)inventing software development productivity
WDS trainer presentation - MLOps.pptx
Training: MVVM Pattern
Meet Magento Spain 2019 - Our Experience with Magento Cloud
Expert guidance on migrating from magento 1 to magento 2
Udvid din test portefølje med coded ui test og cloud load test
Blending Realized Code
Moodle Development Best Pracitces
MidoNet roadmap
MidoNet Vision & Roadmap
Ad

More from Andreas von Studnitz (6)

PDF
Successful projects with Hyvä - The impact of Developer Happiness
PDF
Vorstellung Hyvä: Modernes Frontend mit Magento 2
PPTX
Dependency Injection Extended: the way to advanced Magento 2 development
PDF
Magento Audit - anonymisiert (German)
PDF
Was kann denn schon passieren? Sicherheit in Magento-Shops
PDF
What could possibly go wrong? Security in Magento Shops
Successful projects with Hyvä - The impact of Developer Happiness
Vorstellung Hyvä: Modernes Frontend mit Magento 2
Dependency Injection Extended: the way to advanced Magento 2 development
Magento Audit - anonymisiert (German)
Was kann denn schon passieren? Sicherheit in Magento-Shops
What could possibly go wrong? Security in Magento Shops
Ad

Recently uploaded (20)

PPT
tcp ip networks nd ip layering assotred slides
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
E -tech empowerment technologies PowerPoint
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
Introduction to the IoT system, how the IoT system works
PDF
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPT
Ethics in Information System - Management Information System
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
Sims 4 Historia para lo sims 4 para jugar
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
DOCX
Unit-3 cyber security network security of internet system
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
tcp ip networks nd ip layering assotred slides
PptxGenJS_Demo_Chart_20250317130215833.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
E -tech empowerment technologies PowerPoint
Slides PPTX World Game (s) Eco Economic Epochs.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Introuction about WHO-FIC in ICD-10.pptx
Introduction to the IoT system, how the IoT system works
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Ethics in Information System - Management Information System
522797556-Unit-2-Temperature-measurement-1-1.pptx
Sims 4 Historia para lo sims 4 para jugar
introduction about ICD -10 & ICD-11 ppt.pptx
artificialintelligenceai1-copy-210604123353.pptx
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Unit-3 cyber security network security of internet system
Design_with_Watersergyerge45hrbgre4top (1).ppt
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf

Code Quality with Magento 2

  • 1. Code Quality with Magento 2 April 30th 2020 – Virtual Meet Magento Meetup #2 Andreas von Studnitz – integer_net – @avstudnitz
  • 2. Co-Founder and Managing Director @ integer_net Magento Developer, Trainer, Consultant since 2008 Twitter: @avstudnitz Andreas von Studnitz
  • 3. Andreas von Studnitz – integer_net – @avstudnitz 2014 Image Quality
  • 4. Andreas von Studnitz – integer_net – @avstudnitz 2020 Code Quality
  • 5. Andreas von Studnitz – integer_net – @avstudnitz Agenda Magento-Specific Code Quality Questions and Answers General Code Quality Tools for better Code Quality Motivation
  • 6. Andreas von Studnitz – integer_net – @avstudnitz For developers ◦ Code is easy to read ◦ Easy to understand ◦ Easy to adapt ◦ Easy to extend What is code quality good for? For customers • Fewer Bugs • Reliability • Security • Updatability • Less expensive long-term
  • 8. Andreas von Studnitz – integer_net – @avstudnitz Soft Factors • Code Reviews • Working Hours • Pair Programming • Code Reviews
  • 9. Andreas von Studnitz – integer_net – @avstudnitz Keep it simple • Minimal amount of LoC for demand – „only no code is good code“ • Use existing functions: Generate price rules instead of overwriting price logic • Not going to break on an update • Not going to show wrong VAT with tax included • Not going to break on a credit memo • Avoid unnecessary abstraction
  • 10. “ Andreas von Studnitz – integer_net – @avstudnitz “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan
  • 11. Andreas von Studnitz – integer_net – @avstudnitz Literature
  • 13. Andreas von Studnitz – integer_net – @avstudnitz Updatability • No core hacks, no hacks of external modules • No changes of PHP classes  Instead: Observer, Plugin or Preference • No changes of template files  Instead: Make a theme specific copy, or better use Layout XML • No changes of language files  Instead: Custom translation files, possibly in own language pack
  • 14. Andreas von Studnitz – integer_net – @avstudnitz From MVC to MVVM • Model – View – ViewModel • ViewModels provide data for the View • PHP Classes which don’t need to inherit or implement anything • Read more: https://www.integer-net.com/view-models- in-magento-1-and-2/ (by Fabian Schmengler)
  • 15. Andreas von Studnitz – integer_net – @avstudnitz An Example of View Models module-catalog/view/frontend/layout/catalog_product_view.xml module-catalog/view/frontend/templates/product/breadcrumbs.phtml
  • 16. Andreas von Studnitz – integer_net – @avstudnitz Configuration as Code • Store configuration in the config.php and/or env.php • bin/magento config:set --lock-config • bin/magento config:set --lock-env • Since Magento 2.2.4 • Can create websites and stores • Can set store specific values
  • 17. Andreas von Studnitz – integer_net – @avstudnitz Example
  • 18. Andreas von Studnitz – integer_net – @avstudnitz Configuration: Suggestions • config.php is for sharing • Should contain everything you want to have control over • Don’t add everything – some things are better kept in config.xml • env.php is local only • Add your base URLs • Adjust security settings • Disable services like Google Analytics • Adjust payment methods and other sensitive data by environment
  • 19. Andreas von Studnitz – integer_net – @avstudnitz Setup Patches • More “Configuration as Code” – don’t use the Magento Admin for crucial changes • Successor of Setup Scripts (since Magento 2.3) • Attributes • Content (CMS, categories, products, …) • Accompanied by Declarative Schema for adjusting the database schema
  • 20. Andreas von Studnitz – integer_net – @avstudnitz A Setup Patch
  • 21. Tools for better Code Quality with Magento 2
  • 22. Andreas von Studnitz – integer_net – @avstudnitz Automated Testing • Huge improvement of M2 over M1 • In M1 automated testing not cost-effective in most mid-size cases • In M2 automated testing is now cost effective in most mid-size cases • Different types of testing: • Unit Tests • Integration Tests • Frontend Tests (MFTF)
  • 23. Andreas von Studnitz – integer_net – @avstudnitz Integration Tests class NewProductsCategoryTest extends MagentoTestFrameworkTestCaseAbstractControlle { public static function loadFixture() { include __DIR__ . '/../_files/categories.php'; include __DIR__ . '/../_files/products.php'; } public function testCategoryDisplaysCorrectProducts() { $this->dispatch('catalog/category/view/id/3'); $this->assertContains('Simple Product 1', $this->getResponse()->getBody()); $this->assertNotContains('Simple Product 2', $this->getResponse()->getBody()); } } • Hint: use https://github.com/tddwizard/magento2-fixtures
  • 24. Andreas von Studnitz – integer_net – @avstudnitz Static Tests • Check the code with tools like PHP Codesniffer and PHP Mess Detector • Automate with GrumPhp: Code will be checked on commit • Only a few steps to setup • Example:
  • 25. Andreas von Studnitz – integer_net – @avstudnitz Static Tests • Check the code with tools like PHP Codesniffer and PHP Mess Detector • Automate with GrumPhp: Code will be checked on commit • Only a few steps to setup • Example:
  • 26. Andreas von Studnitz – integer_net – @avstudnitz Static Tests • Check the code with tools like PHP Codesniffer and PHP Mess Detector • Automate with GrumPhp: Code will be checked on commit • Only a few steps to setup • Example:
  • 28. #MM18DE Installation of GrumPhp (2) grumphp.yml in project root directory
  • 29. Andreas von Studnitz – integer_net – @avstudnitz „The Obvious“ • Git for source code management and versioning • Composer for managing dependencies • PhpStorm • See warnings and errors directly • Easily integrate tools like Code Sniffer and Mess Detector • Built-in commands for code formatting and small refactorings