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 |
||
| 21 | abstract class AbstractBundle extends Bundle |
||
| 22 | { |
||
| 23 | const STATE_DISABLED = 2; |
||
| 24 | const STATE_ACTIVE = 3; |
||
| 25 | const STATE_MISSING = 6; |
||
| 26 | |||
| 27 | protected $state; |
||
| 28 | |||
| 29 | protected $booted = false; |
||
| 30 | |||
| 31 | private $basePath; |
||
| 32 | |||
| 33 | public function isBooted() |
||
| 37 | |||
| 38 | public function setState($state) |
||
| 39 | { |
||
| 40 | View Code Duplication | if (!in_array($state, [self::STATE_ACTIVE, self::STATE_DISABLED, self::STATE_MISSING])) { |
|
|
|
|||
| 41 | throw new \InvalidArgumentException(sprintf('Invalid state %s', $state)); |
||
| 42 | } |
||
| 43 | |||
| 44 | $this->state = $state; |
||
| 45 | |||
| 46 | return $this; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getState() |
||
| 53 | |||
| 54 | public function getInstallerClass() |
||
| 61 | |||
| 62 | public function getRoutingConfig() |
||
| 66 | |||
| 67 | public function getTranslationDomain() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Gets the translation domain path |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getLocalePath() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Gets the views path |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getViewsPath() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the config path. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getConfigPath() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the assetpath relative to /web e.g. /modules/acmefoo |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getRelativeAssetPath() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getBasePath() |
||
| 124 | |||
| 125 | public function getNameType() |
||
| 129 | |||
| 130 | protected function hasCommands() |
||
| 134 | |||
| 135 | public function getContainerExtension() |
||
| 165 | |||
| 166 | public function registerCommands(Application $application) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get container. |
||
| 175 | * |
||
| 176 | * @return ContainerInterface |
||
| 177 | */ |
||
| 178 | public function getContainer() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return MetaData |
||
| 185 | */ |
||
| 186 | public function getMetaData() |
||
| 207 | } |
||
| 208 |
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.