Foreword
The best names are those that reveal the intent.
At date, Magento 2 codebase is still full of legacy code, thus don’t always take it as a good reference.
Observers, plugins, service classes
When naming observers, plugins, and service classes, choose a name that reveal intent.
Here follow some examples.
Observer
Bad
// events.xml
<event name="store_save_after">
<observer name="store-save-observer" instance="…/Observer/StoreSaveObserver" />
</event>
Good
// events.xml
<event name="store_save_after">
<observer name="duplicate-store" instance="…/Observer/DuplicateStore" />
</event>
Plugin
Bad
<!-- di.xml -->
<type name="Magento\Theme\Block\Html\Topmenu">
<plugin name="catalogTopmenu" type="...\Plugin\Topmenu" />
</type>
// .../Plugin/Topmenu.php
class Topmenu
{
// ...
public function beforeGetHtml(/* ... */)
{
// ...
}
}
Good
<!-- di.xml -->
<type name="Magento\Theme\Block\Html\Topmenu">
<plugin name="build-category-tree" type="...\Plugin\BuildCategoryTree" />
</type>
// .../Plugin/BuildCategoryTree.php
class BuildCategoryTree
{
// ...
public function beforeGetHtml(/* ... */)
{
// ...
}
}
Service class
Bad
class ProductTypeList
{
public function getProductTypes()
{
// ...
}
}
Good
class GetProductTypes
{
public function execute()
{
// ...
}
}
Commands and events
A command is something that has to be done,
thus better use an imperative verb (e.g.: doSomething
).
An event is something that’s happened in the past,
thus better use a past tense verb (e.g.: somethingHappened
).
If you look at how events are named in Magento 2,
they often break this rule (e.g.: See Vinai’s comment below.store_save_after
is used instead of store_saved
).
Off-topic note
Commits are commands, that’s why commit messages should be written using imperative verbs.
Enjoy!
Photo credits: Lesly Juarez - Creative Commons license
Posted with : magento2, best-practices, observers, plugins, service-classes