0% found this document useful (0 votes)
73 views12 pages

Magento2 (Best Practices) - Final1

The document is a comprehensive guide on best practices for Magento 2.x development, detailing essential do's and don'ts for coding standards. Key recommendations include avoiding the use of Object Manager, properly configuring code sniffer, and adhering to naming conventions and data type declarations. It serves as a resource for developers to enhance code quality and maintainability in Magento projects.

Uploaded by

Mhammad Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views12 pages

Magento2 (Best Practices) - Final1

The document is a comprehensive guide on best practices for Magento 2.x development, detailing essential do's and don'ts for coding standards. Key recommendations include avoiding the use of Object Manager, properly configuring code sniffer, and adhering to naming conventions and data type declarations. It serves as a resource for developers to enhance code quality and maintainability in Magento projects.

Uploaded by

Mhammad Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MAGENTO 2.

BEST PRACTICES
A COMPLETE GUIDE

Contributors:
Badar Anwar
Hassan Raza
Muhammad Ashir
Shajie Shaukat
Afzel Arshad

© RLT Square
5, L Block, Johar Town, Lahore, Pakistan
Phone +92 (42) 32297200
Email: [email protected]

Never Use Object Manager.


Contents

Do’s .......................................................................................................3
1) Block Comments..........................................................................4
2) Configure Code Sniffer Properly .............................................5
3) Throw Back Exceptions ............................................................6
4) Declare Data Type with Variable Declaration: ...................6
5) XML Start Tag ...............................................................................7
6) Anchor Link in escapeHtml Text ............................................7
7) Magento2 Coding Standard Plugin .........................................8
8) Naming Convention & Indentation .........................................8
9) Declare Strict Types ...................................................................9
Don’ts .................................................................................................10
1) Never Use Object Manager & Core Registry ..................... 11
2) Don’t Use Underscore with Variable Declaration ............11
3) Don’t Use Deprecated Class and Functions ...................... 12
4) Don't Use Global Variables Directly in Your Code...........12

Never Use Object Manager.


Do’s

Never Use Object Manager.


1) Block Comments

✓ Used to add comments in PHP files.

INSTRUCTIONS

1) Press (from Keyboard)

2) Then Select PHPDoc Blocks

✓ Example:

Never Use Object Manager.


2) Configure Code Sniffer Properly
INSTRUCTIONS

Open PHPStorm & Press


1) (from Keyboard)

2) Then Select PHP

3) Select Quality Tools

4) Choose PHPCodeSniffer

Set the path of squizlabs/PHP_CodeSniffer


5) /bin/phpcs

6) Validate it press Apply and ok

7) Open PHPStorm Settings Select Editor

8) Then Select Inspections

9) Select PHP

10) Choose Quality Tools

11) Select php_codesniffer Validation

Options on the right side check all the


12) options

Set path of vendor/magento/magento-coding-


13) standard/magento2 and press ok

Select magento2 from coding standard


14) dropdown options

15) Press Apply & ok

Options on the right side check all the


16) Then
options

Never Use Object Manager.


3) Throw Back Exceptions
✓ Used to handle exceptions
✓ There are two ways to handle exceptions:
1) try & catch
2) use throwback exception classes

✓ Example:

4) Declare Data Type with Variable


Declaration:
✓ Always use data type while declaring a variable

✓ Example:

protected/private/public string $name;

Never Use Object Manager.


5) XML Start Tag
✓ Always use XML start tag in every XML file

✓ Example:

<?xml version=”1.0”?>

6) Anchor Link in escapeHtml Text


✓ Use escapeHtml to secure template files

✓ Example:
✓ $block->escapeHtml()
✓ $block->escapeQuote()
✓ $block->escapeUrl()

Never Use Object Manager.


7) Magento2 Coding Standard Plugin
✓ Configure Magento2 coding standard plugin in PHP Storm

8) Naming Convention & Indentation


➢ Naming convention:

▪ Always use camel case naming convention


▪ Lowercase for xml files

Never Use Object Manager.


➢ Indentation:

▪ Tab size: 4 spaces


▪ Indent size: 4 spaces
▪ Continuation indent: 4 spaces

INSTRUCTIONS

1) Press (from Keyboard)

9) Declare Strict Types


✓ Always declare strict types at the start of each PHP file (i.e. declare(strict_types=1);)

Never Use Object Manager.


Don’ts

Never Use Object Manager.


1) Never Use Object Manager & Core
Registry
➢ Use of ObjectManager is an extreme bad practice for a professional developer

✓ Example:

2) Don’t Use Underscore with


Variable Declaration
Magento2 follows the PSR-2 coding standard, which states:

✓ Property names SHOULD NOT be prefixed with a single underscore to indicate


protected or private visibility.
✓ Method names SHOULD NOT be prefixed with a single underscore to indicate
protected or private visibility.

✓ Example:

Never Use Object Manager.


3) Don’t Use Deprecated Class and
Functions

✓ Example:

4) Don't Use Global Variables


Directly in Your Code

✓ Example:

Never Use Object Manager.

You might also like