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 |
||
| 25 | class YamlConfig |
||
| 26 | { |
||
| 27 | protected $processedConfig; |
||
| 28 | protected $loadedConfig; |
||
| 29 | protected $compiledConfig; |
||
| 30 | protected $parameters = array(); |
||
| 31 | protected $taskFactory; |
||
| 32 | protected $transporterFactory; |
||
| 33 | protected $strategyFactory; |
||
| 34 | protected $file; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor. |
||
| 38 | * |
||
| 39 | * @param string $file |
||
| 40 | * @param TaskFactory $taskFactory |
||
| 41 | * @param TransporterFactory $transporterFactory |
||
| 42 | * @param StrategyFactory $strategyFactory |
||
| 43 | * @throws \InvalidArgumentException |
||
| 44 | */ |
||
| 45 | 3 | public function __construct($file, TaskFactory $taskFactory, TransporterFactory $transporterFactory, StrategyFactory $strategyFactory) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | 1 | public function getConfig() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getFile() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | 1 | protected function process() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | 1 | protected function load() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | 1 | protected function compile() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $config |
||
| 121 | * @param array $parameters |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 1 | protected function replaceParameters(array $config, array $parameters) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Expand target groups |
||
| 141 | * |
||
| 142 | * @param array $config |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | 1 | protected function expandTasksTargetGroups(array $config) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Expand target groups for a certain section |
||
| 164 | * |
||
| 165 | * @param array $config |
||
| 166 | * @param array $section |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 1 | protected function expandTasksTargetGroupsForSection(array $config, array $section = null) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function flatten() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $name |
||
| 214 | * @param mixed $value |
||
| 215 | */ |
||
| 216 | public function setParameter($name, $value) |
||
| 230 | } |
||
| 231 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.