Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class ArticleList implements SubscriberInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $pluginPath; |
||
| 24 | /** |
||
| 25 | * @var \Shopware_Components_Snippet_Manager |
||
| 26 | */ |
||
| 27 | private $snippetManager; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param $pluginPath |
||
| 31 | * @param \Shopware_Components_Snippet_Manager $snippetManager |
||
| 32 | */ |
||
| 33 | public function __construct($pluginPath, \Shopware_Components_Snippet_Manager $snippetManager) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public static function getSubscribedEvents() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * If the 'connect' filter is checked, only show products imported from connect |
||
| 52 | * |
||
| 53 | * @param \Enlight_Event_EventArgs $args |
||
| 54 | */ |
||
| 55 | public function onFilterArticle(\Enlight_Event_EventArgs $args) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Extends the product list in the backend in order to have a special hint for connect products |
||
| 76 | * |
||
| 77 | * @event Enlight_Controller_Action_PostDispatch_Backend_ArticleList |
||
| 78 | * @param \Enlight_Event_EventArgs $args |
||
| 79 | */ |
||
| 80 | public function extendBackendArticleList(\Enlight_Event_EventArgs $args) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Helper method which adds an additional 'connect' field to article objects in order to indicate |
||
| 112 | * if they are connect articles or not |
||
| 113 | * |
||
| 114 | * @param $data |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | private function markConnectProducts($data) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Adds connect field to the ExtJS model for article_list |
||
| 156 | * That was changed in SW5, because the model is dynamically created |
||
| 157 | * |
||
| 158 | * @param $data |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | private function addConnectColumn($data) |
||
| 175 | } |
||
| 176 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.