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:
Complex classes like ConfigBridge often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConfigBridge, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | final class ConfigBridge |
||
26 | { |
||
27 | const CS_FIXER_MIN_VERSION = '1.6.1'; |
||
28 | |||
29 | /** |
||
30 | * @var OutputInterface |
||
31 | */ |
||
32 | private $output; |
||
33 | |||
34 | /** |
||
35 | * @var FixerFactory |
||
36 | */ |
||
37 | private $fixerFactory = null; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $styleCIConfigDir; |
||
43 | |||
44 | /** |
||
45 | * @var array|null |
||
46 | */ |
||
47 | private $styleCIConfig = null; |
||
48 | |||
49 | /** |
||
50 | * @var string|array |
||
51 | */ |
||
52 | private $finderDirs; |
||
53 | |||
54 | /** |
||
55 | * @param string|null $styleCIConfigDir StyleCI config directory. Called script dir as default. |
||
56 | * @param string|array|null $finderDirs A directory path or an array of directories for Finder. Called script dir as default. |
||
|
|||
57 | */ |
||
58 | public function __construct($styleCIConfigDir = null, $finderDirs = null) |
||
99 | |||
100 | /** |
||
101 | * @param string $styleCIConfigDir |
||
102 | * @param string|array $finderDirs A directory path or an array of directories for Finder |
||
103 | * |
||
104 | * @return Config |
||
105 | */ |
||
106 | public static function create($styleCIConfigDir = null, $finderDirs = null) |
||
137 | |||
138 | /** |
||
139 | * @return Finder|\Symfony\CS\Finder\DefaultFinder |
||
140 | */ |
||
141 | public function getFinder() |
||
169 | |||
170 | /** |
||
171 | * @return string[] |
||
172 | */ |
||
173 | public function getFixers() |
||
194 | |||
195 | /** |
||
196 | * Returns fixers converted to rules for PHP-CS-Fixer 2.x. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | public function getRules() |
||
223 | |||
224 | /** |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function getRisky() |
||
231 | |||
232 | /** |
||
233 | * @return string[] |
||
234 | */ |
||
235 | private function getPresetFixers() |
||
242 | |||
243 | /** |
||
244 | * Adds both aliases and real fixers if set. PHP-CS-Fixer would not take care if not existing. |
||
245 | * Better compatibility between PHP-CS-Fixer 1.x and 2.x. |
||
246 | * |
||
247 | * @param string[] $fixers |
||
248 | * |
||
249 | * @return string[] |
||
250 | */ |
||
251 | private function resolveAliases(array $fixers) |
||
264 | |||
265 | /** |
||
266 | * @param string $name |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | private function isFixerAvailable($name) |
||
279 | |||
280 | private function parseStyleCIConfig() |
||
288 | } |
||
289 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.