If you sell your extension or share it with others, think about writing code that is human readable.
- don't make method too complex
- add DOC blocks to your methods *
- use proper variable names, like
$productIds instead of $ids
- same for methods,
public function myOnProductSaveMethod() {...} says ... nothing , but tryDisableInternetOnProductSave() will give a hint want's planned
- use type hints where it makes sense
someMethod(Varien_Data_Db_Collection $collection)
- avoid magic numbers and strings **
- if you use models set
$_eventPrefix property (and $_eventObject) to make them better accessible to observers
- if you add system config fields
- set default values in
config.xml
- add
<validate> nodes to fields in system.xml
- add ACL resources to
adminhtml.xml
- do not add useless/advertising first-level menu entries in admin backend - neither in topbar nor in config sections
- add ACL resources for all controller actions (massactions too!)
- ensure your queries work with DB table prefixes
- think about (no) backward comptibility (this is really opinion based)
- do not support
Mysql4 classes
- do not use deprecated methods
- ensure your extionsion works as expected in every case - add UnitTests (PhpUnit for example)
- in addition to David Manners ... add a
composer.json too to make deployment easier
- since PHP5.6 is EOL, write your code for PHP7. Use
declare(strict_types=1); and define your in- and output types
- Magento2: check your code with static code analyze tools like phpstan. Support for magic methods here. (latest commit works with 2.3, before for 2.1/2.2 - taht require phpstan 0.8.5)
* DOC blocks:
If you check your Magento-1 code with PHP_CodeSniffer for PSR2 standard or PHPMD you maybe want to add this lines (where it makes sense) ...
- to classes
@phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
@phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore - inherited properties
@phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
@SuppressWarnings(PHPMD.CamelCaseClassName)
@SuppressWarnings(PHPMD.CamelCasePropertyName) - inherited properties
- to methods
@SuppressWarnings(PHPMD.CamelCaseMethodName) - inherited methods
@SuppressWarnings(PHPMD.StaticAccess) - if you use Mage:: or other static calls
** Often used:
- admin store ID
0 > Mage_Core_Model_App::ADMIN_STORE_ID
- product
status
1 > Mage_Catalog_Model_Product_Status::STATUS_ENABLED
2 > Mage_Catalog_Model_Product_Status::STATUS_DISABLED (not 0 as maybe expected)
- product
type
simple > Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
bundle > Mage_Catalog_Model_Product_Type::TYPE_BUNDLE
configurable > Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
grouped > Mage_Catalog_Model_Product_Type::TYPE_GROUPED
virtual > Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL
- product
visibity
1 > Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE
2 > Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
3 > Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
4 > Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH
Same for SQL order ASC vs Zend_Db_Select::SQL_ASC (for example).
Saying "it's not nessessary cause it will never ever change"? For example entity ID for catalog_product attributes changed somewhere between Magento 1.5 and 1.9 from 10 to 4, so this could break your extension:
$collection->addFieldToFilter('entity_type_id', 10)
Using this instead adds one query, but you'll be safe ...
$entityTypeId = Mage::getModel('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)
->getEntityTypeId();
$collection->addFieldToFilter('entity_type_id', $entityTypeId)