Magento Development Bad Practices

Mobeen Sarwar
mobeensarwar
Published in
2 min readJan 2, 2022

Magento is one of the most complicated and difficult PHP systems in the market a developer could learn. There are XML handles, UiComponents that are a mashup of random configuration XML, KnockoutJS with HTML templates, requireJS, and jQuery Widgets. There is a lot more that makes it harder to learn and master. Here are some Magento development bad practices that one must avoid while developing a Magento application.

1- Using Object Manager Directly:

Magento highly discourages using the object manager directly but this is the most common bad practice that new developers used to fall in. Developers tend to go for the easy/short/not recommended solution. Here is a little example that I found, while reviewing code.

Object Manager is used in .phtml files and event it’s in a forach loop.

Object Manager in Templates

2- Using Raw SQL Queries:

Raw SQL queries can lead to potential security vulnerabilities and database portability issues. This is also a very common practice that instead of using data models, newbies tend to use Raw SQL Queries.

An example of Raw Query used in a template

3- Using PHP Superglobal Global Variables:

There is also a tendency to use PHP Super Global variables by beginners.

$GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_SESSION, $_REQUEST

Instead, use the Magento\Framework\HTTP\PhpEnvironment\Request wrapper class to safely access these values.

4- Unnecessary Use of around Plugin:

Around plugin is the most sensitive/complex and least used plugin. Avoid using around method plugins when they are not required because they increase stack traces and affect performance. The only use case for around method plugins is when the execution of all further plugins and original methods need termination. Access to method parameters was the primary purpose for using around plugins. Since 2.2, after method plugins give you access to method parameters.

public function aroundSave(Product $subject, callable $proceed)
{
$someValue = $this->doSmthBeforeProductIsSaved();
$returnValue = null;

if ($this->canCallProceedCallable($someValue)) {
$returnValue = $proceed();
}

if ($returnValue) {
$this->postProductToFacebook();
}

return $returnValue;
}

5- Misuse of Preference:

For newbies, it looks easy to write a preference instead of writing a plugin. So, they just copy/paste the original class to their module, change the desired functionality, and add preference to the original class. This is a funny thing but it’s also a common practice from naive ones.

Thanks for reading the blog.

--

--