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 |
||
| 38 | View Code Duplication | class Index extends Action |
|
|
|
|||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Page factory |
||
| 42 | * |
||
| 43 | * @var \Magento\Framework\View\Result\PageFactory |
||
| 44 | */ |
||
| 45 | protected $resultPageFactory; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Result page |
||
| 49 | * |
||
| 50 | * @var \Magento\Backend\Model\View\Result\Page |
||
| 51 | */ |
||
| 52 | protected $resultPage; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor |
||
| 56 | * |
||
| 57 | * @param Context $context |
||
| 58 | * @param PageFactory $resultPageFactory |
||
| 59 | */ |
||
| 60 | public function __construct(Context $context, PageFactory $resultPageFactory) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return if the user has the needed rights to view this page |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | protected function _isAllowed() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return result page |
||
| 78 | * |
||
| 79 | * @return BackendPage|Page |
||
| 80 | */ |
||
| 81 | public function execute() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * instantiate result page object |
||
| 91 | * |
||
| 92 | * @return BackendPage|Page |
||
| 93 | */ |
||
| 94 | public function getResultPage() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * set page data |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | protected function setPageData() |
||
| 114 | } |
||
| 115 |
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.