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 |
||
26 | final class ConfigBridge |
||
27 | { |
||
28 | const CS_FIXER_MIN_VERSION = '1.6.1'; |
||
29 | |||
30 | /** |
||
31 | * @var OutputInterface |
||
32 | */ |
||
33 | private $output; |
||
34 | |||
35 | /** |
||
36 | * @var FixerFactory |
||
37 | */ |
||
38 | private $fixerFactory = null; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $styleCIConfigDir; |
||
44 | |||
45 | /** |
||
46 | * @var array|null |
||
47 | */ |
||
48 | private $styleCIConfig = null; |
||
49 | |||
50 | /** |
||
51 | * @var string|array |
||
52 | */ |
||
53 | private $finderDirs; |
||
54 | |||
55 | /** |
||
56 | * @param string|null $styleCIConfigDir StyleCI config directory. Called script dir as default. |
||
57 | * @param string|array|null $finderDirs A directory path or an array of directories for Finder. Called script dir as default. |
||
|
|||
58 | */ |
||
59 | public function __construct($styleCIConfigDir = null, $finderDirs = null) |
||
97 | |||
98 | /** |
||
99 | * @param string $styleCIConfigDir |
||
100 | * @param string|array $finderDirs A directory path or an array of directories for Finder |
||
101 | * |
||
102 | * @return Config |
||
103 | */ |
||
104 | public static function create($styleCIConfigDir = null, $finderDirs = null) |
||
130 | |||
131 | /** |
||
132 | * @return Finder |
||
133 | */ |
||
134 | public function getFinder() |
||
157 | |||
158 | /** |
||
159 | * @return int |
||
160 | * |
||
161 | * @deprecated since 1.1, to be removed in 2.0 |
||
162 | */ |
||
163 | public function getLevel() |
||
179 | |||
180 | /** |
||
181 | * @return string[] |
||
182 | */ |
||
183 | public function getFixers() |
||
204 | |||
205 | /** |
||
206 | * Returns fixers converted to rules for PHP-CS-Fixer 2.x. |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public function getRules() |
||
233 | |||
234 | /** |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function getRisky() |
||
241 | |||
242 | /** |
||
243 | * @return string[] |
||
244 | */ |
||
245 | private function getPresetFixers() |
||
252 | |||
253 | /** |
||
254 | * Adds both aliases and real fixers if set. PHP-CS-Fixer would not take care if not existing. |
||
255 | * Better compatibility between PHP-CS-Fixer 1.x and 2.x. |
||
256 | * |
||
257 | * @param string[] $fixers |
||
258 | * |
||
259 | * @return string[] |
||
260 | */ |
||
261 | private function resolveAliases(array $fixers) |
||
274 | |||
275 | /** |
||
276 | * @param string $name |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | private function isFixerAvailable($name) |
||
289 | |||
290 | private function parseStyleCIConfig() |
||
298 | } |
||
299 |
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.