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

Similar to Code Quality with Magento 2 (20)

ODP
Beginners - Get Started With Unit Testing in .NET
Baskar K
 
PPTX
Introduction to Integration Tests in Magento / Adobe Commerce
Bartosz Górski
 
PPTX
Introduction to Integration Tests in Magento / Adobe Commerce
Bartosz Górski
 
PPTX
agile with scrum methodology
rahul reddy
 
PPTX
Software Development : Jeremy Gleason Iscope Digital
Iscope Digital
 
PDF
Case Study - AMR Test Automation
iFocusSystec
 
PPTX
V model Over view (Software Engineering)
Badar Rameez. CH.
 
PPTX
V model Over View (Software Engineering)
Badar Rameez. CH.
 
PPTX
Magento 2 Best Practice Workfow // David Lambauer // Meet Magento 2017 // Lei...
AOE
 
PPTX
Madison PHP - Getting Started with Magento 2
Mathew Beane
 
ODP
(Re)inventing software development productivity
Peter Hendriks
 
PPTX
WDS trainer presentation - MLOps.pptx
Arthur240715
 
PPTX
Training: MVVM Pattern
Betclic Everest Group Tech Team
 
PPTX
Meet Magento Spain 2019 - Our Experience with Magento Cloud
Lyzun Oleksandr
 
PPTX
Expert guidance on migrating from magento 1 to magento 2
James Cowie
 
PPTX
Udvid din test portefølje med coded ui test og cloud load test
Peter Lindberg
 
PDF
Blending Realized Code
Pathfinder Solutions
 
ODP
Moodle Development Best Pracitces
Justin Filip
 
PDF
MidoNet roadmap
Jean-Francois Joly
 
PDF
MidoNet Vision & Roadmap
MidoNet
 
Beginners - Get Started With Unit Testing in .NET
Baskar K
 
Introduction to Integration Tests in Magento / Adobe Commerce
Bartosz Górski
 
Introduction to Integration Tests in Magento / Adobe Commerce
Bartosz Górski
 
agile with scrum methodology
rahul reddy
 
Software Development : Jeremy Gleason Iscope Digital
Iscope Digital
 
Case Study - AMR Test Automation
iFocusSystec
 
V model Over view (Software Engineering)
Badar Rameez. CH.
 
V model Over View (Software Engineering)
Badar Rameez. CH.
 
Magento 2 Best Practice Workfow // David Lambauer // Meet Magento 2017 // Lei...
AOE
 
Madison PHP - Getting Started with Magento 2
Mathew Beane
 
(Re)inventing software development productivity
Peter Hendriks
 
WDS trainer presentation - MLOps.pptx
Arthur240715
 
Training: MVVM Pattern
Betclic Everest Group Tech Team
 
Meet Magento Spain 2019 - Our Experience with Magento Cloud
Lyzun Oleksandr
 
Expert guidance on migrating from magento 1 to magento 2
James Cowie
 
Udvid din test portefølje med coded ui test og cloud load test
Peter Lindberg
 
Blending Realized Code
Pathfinder Solutions
 
Moodle Development Best Pracitces
Justin Filip
 
MidoNet roadmap
Jean-Francois Joly
 
MidoNet Vision & Roadmap
MidoNet
 

More from Andreas von Studnitz (6)

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

Recently uploaded (20)

PPTX
ipv6 very very very very vvoverview.pptx
eyala75
 
PDF
How to Fix Error Code 16 in Adobe Photoshop A Step-by-Step Guide.pdf
Becky Lean
 
PPTX
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
PPTX
Presentation on Social Media1111111.pptx
tanamlimbu
 
PPT
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PDF
Technical Guide to Build a Successful Shopify Marketplace from Scratch.pdf
CartCoders
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
Internet_of_Things_Presentation_KaifRahaman.pptx
kaifrahaman27593
 
PDF
123546568reb2024-Linux-remote-logging.pdf
lafinedelcinghiale
 
PPT
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
PPTX
Cost_of_Quality_Presentation_Software_Engineering.pptx
farispalayi
 
PDF
Pas45789-Energs-Efficient-Craigg1ing.pdf
lafinedelcinghiale
 
PPTX
英国学位证(RCM毕业证书)皇家音乐学院毕业证书如何办理
Taqyea
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
PPTX
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
PPTX
一比一原版(LaTech毕业证)路易斯安那理工大学毕业证如何办理
Taqyea
 
PPTX
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
ipv6 very very very very vvoverview.pptx
eyala75
 
How to Fix Error Code 16 in Adobe Photoshop A Step-by-Step Guide.pdf
Becky Lean
 
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
Presentation on Social Media1111111.pptx
tanamlimbu
 
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
Technical Guide to Build a Successful Shopify Marketplace from Scratch.pdf
CartCoders
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
Internet_of_Things_Presentation_KaifRahaman.pptx
kaifrahaman27593
 
123546568reb2024-Linux-remote-logging.pdf
lafinedelcinghiale
 
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
Cost_of_Quality_Presentation_Software_Engineering.pptx
farispalayi
 
Pas45789-Energs-Efficient-Craigg1ing.pdf
lafinedelcinghiale
 
英国学位证(RCM毕业证书)皇家音乐学院毕业证书如何办理
Taqyea
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
一比一原版(LaTech毕业证)路易斯安那理工大学毕业证如何办理
Taqyea
 
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
Ad

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