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 PhpCodeSniffer 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 PhpCodeSniffer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class PhpCodeSniffer extends Plugin implements ZeroConfigPluginInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $suffixes; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $standard; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $tabWidth; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $encoding; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $allowedErrors; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $allowedWarnings; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $severity = null; |
||
| 53 | /** |
||
| 54 | * @var null|int |
||
| 55 | */ |
||
| 56 | protected $errorSeverity = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var null|int |
||
| 60 | */ |
||
| 61 | protected $warningSeverity = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public static function pluginName() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function __construct(Builder $builder, Build $build, array $options = []) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | public static function canExecuteOnStage($stage, Build $build) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Runs PHP Code Sniffer in a specified directory, to a specified standard. |
||
| 143 | */ |
||
| 144 | public function execute() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Process options and produce an arguments string for PHPCS. |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | protected function getFlags() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Process the PHPCS output report. |
||
| 235 | * |
||
| 236 | * @param $output |
||
| 237 | * @return array |
||
| 238 | * @throws \Exception |
||
| 239 | */ |
||
| 240 | protected function processReport($output) |
||
| 269 | } |
||
| 270 |
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.