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() |
|
| 79 | { |
||
| 80 | 1 | if (null === $this->processedConfig) { |
|
| 81 | 1 | $this->load(); |
|
| 82 | |||
| 83 | 1 | $configuration = new DeployConfiguration($this->taskFactory, $this->transporterFactory, $this->strategyFactory); |
|
| 84 | 1 | $processor = new Processor(); |
|
| 85 | |||
| 86 | 1 | $this->processedConfig = $processor->processConfiguration($configuration, $this->loadedConfig); |
|
|
|
|||
| 87 | 1 | } |
|
| 88 | |||
| 89 | 1 | return $this->processedConfig; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | 1 | protected function load() |
|
| 96 | { |
||
| 97 | 1 | if (null === $this->loadedConfig) { |
|
| 98 | 1 | $this->loadedConfig = Yaml::parse($this->file); |
|
| 99 | 1 | } |
|
| 100 | |||
| 101 | 1 | return $this->loadedConfig; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | 1 | protected function compile() |
|
| 108 | { |
||
| 109 | 1 | if (null === $this->compiledConfig) { |
|
| 110 | 1 | $this->process(); |
|
| 111 | |||
| 112 | 1 | $this->compiledConfig = $this->replaceParameters($this->processedConfig, $this->parameters); |
|
| 113 | 1 | $this->compiledConfig = $this->expandTasksTargetGroups($this->compiledConfig); |
|
| 114 | 1 | } |
|
| 115 | |||
| 116 | 1 | return $this->compiledConfig; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $config |
||
| 121 | * @param array $parameters |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 1 | protected function replaceParameters(array $config, array $parameters) |
|
| 125 | { |
||
| 126 | 1 | foreach ($config as &$value) { |
|
| 127 | 1 | if (is_array($value)) { |
|
| 128 | 1 | $value = $this->replaceParameters($value, $parameters); |
|
| 129 | 1 | } elseif (is_string($value)) { |
|
| 130 | 1 | foreach ($parameters as $key => $val) { |
|
| 131 | $value = str_replace(sprintf('{{%s}}', $key), $val, $value); |
||
| 132 | 1 | } |
|
| 133 | 1 | } |
|
| 134 | 1 | } |
|
| 135 | |||
| 136 | 1 | return $config; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Expand target groups |
||
| 141 | * |
||
| 142 | * @param array $config |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | 1 | protected function expandTasksTargetGroups(array $config) |
|
| 146 | { |
||
| 147 | $sections = array( |
||
| 148 | 1 | &$config['build']['tasks'], |
|
| 149 | 1 | &$config['deploy']['before'], |
|
| 150 | 1 | &$config['deploy']['after'], |
|
| 151 | 1 | &$config['deploy']['final'], |
|
| 152 | 1 | &$config['undeploy']['tasks'], |
|
| 153 | 1 | ); |
|
| 154 | |||
| 155 | 1 | foreach ($sections as &$section) { |
|
| 156 | 1 | $section = $this->expandTasksTargetGroupsForSection($config, $section); |
|
| 157 | 1 | } |
|
| 158 | |||
| 159 | 1 | return $config; |
|
| 160 | } |
||
| 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) |
|
| 170 | { |
||
| 171 | 1 | if (null === $section) { |
|
| 172 | 1 | return $section; |
|
| 173 | } |
||
| 174 | |||
| 175 | 1 | $groups = array(); |
|
| 176 | 1 | View Code Duplication | foreach ($config['targets'] as $targetName => $targetConfig) { |
| 177 | 1 | foreach ($targetConfig['groups'] as $group) { |
|
| 178 | $groups[$group][] = $targetName; |
||
| 179 | 1 | } |
|
| 180 | 1 | } |
|
| 181 | |||
| 182 | 1 | foreach ($section as &$taskConfig) { |
|
| 183 | $targets = $taskConfig['targets']; |
||
| 184 | $expandedTargets = array(); |
||
| 185 | |||
| 186 | foreach ($targets as $target) { |
||
| 187 | if (isset($groups[$target])) { |
||
| 188 | foreach ($groups[$target] as $groupTarget) { |
||
| 189 | $expandedTargets[] = $groupTarget; |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | $expandedTargets[] = $target; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | $taskConfig['targets'] = $expandedTargets; |
||
| 197 | 1 | } |
|
| 198 | |||
| 199 | 1 | return $section; |
|
| 200 | } |
||
| 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) |
||
| 217 | { |
||
| 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.