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 |
||
| 28 | View Code Duplication | abstract class BaseResourceController extends ResourceController |
|
| 29 | { |
||
| 30 | /** |
||
| 31 | * @return \Symfony\Component\Security\Core\SecurityContextInterface |
||
| 32 | */ |
||
| 33 | /*public function getSecurity() |
||
| 34 | { |
||
| 35 | return $this->container->get('security.context'); |
||
| 36 | }*/ |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return object |
||
| 40 | */ |
||
| 41 | public function getTemplate() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return NotFoundHttpException |
||
| 48 | */ |
||
| 49 | public function abort() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Converts string 'Chamilo\CoreBundle\Controller\Admin\QuestionManager' into |
||
| 56 | * 'admin/question_manager' |
||
| 57 | */ |
||
| 58 | public function getTemplatePath() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Transforms 'QuestionManagerController' to 'question_manager.controller' |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getControllerAlias() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Translator shortcut |
||
| 90 | * @param string $variable |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function trans($variable) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the class name label |
||
| 100 | * @example RoleController -> Role |
||
| 101 | * |
||
| 102 | * @return string the class name label |
||
| 103 | */ |
||
| 104 | public function getClassNameLabel() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return MenuFactoryInterface |
||
| 111 | */ |
||
| 112 | public function getMenuFactory() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $action |
||
| 119 | * @return MenuItemInterface |
||
| 120 | */ |
||
| 121 | protected function getBreadcrumbs($action) |
||
| 127 | |||
| 128 | /** Main home URL |
||
| 129 | * @return MenuItemInterface |
||
| 130 | */ |
||
| 131 | protected function getHomeBreadCrumb() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param $action |
||
| 153 | * @param MenuItemInterface $menu |
||
| 154 | * @return MenuItemInterface |
||
| 155 | */ |
||
| 156 | public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param array $breadCrumbList |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | protected function parseLegacyBreadCrumb($breadCrumbList = array()) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Renders the current controller template |
||
| 208 | * @param string $name |
||
| 209 | * @param array $elements |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | public function renderTemplate($name, $elements = array()) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritdoc |
||
| 226 | **/ |
||
| 227 | public function isGranted($attributes, $object = null) |
||
| 231 | } |
||
| 232 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: