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 |
||
| 15 | class Result { |
||
| 16 | /** |
||
| 17 | * @var Def\Ruleset |
||
| 18 | */ |
||
| 19 | protected $ruleset; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Violation[] |
||
| 23 | */ |
||
| 24 | protected $violations; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $by_rule_cache; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $by_filename_cache; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Def\Ruleset $ruleset |
||
| 38 | * @param Violations[] $violations |
||
| 39 | */ |
||
| 40 | 4 | public function __construct(Def\Ruleset $ruleset, array $violations) { |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return Def\Ruleset |
||
| 51 | */ |
||
| 52 | public function ruleset() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Def\Rules\Rule $rule |
||
| 58 | * @return Violation[] |
||
| 59 | */ |
||
| 60 | 2 | View Code Duplication | public function violations_of(Rule $rule) { |
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $filename |
||
| 79 | * @return Violation[] |
||
| 80 | */ |
||
| 81 | 2 | View Code Duplication | public function violations_in($filename) { |
| 97 | } |
||
| 98 |
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.