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 |
||
| 20 | class ContainText extends Property { |
||
| 21 | /** |
||
| 22 | * @inheritdoc |
||
| 23 | */ |
||
| 24 | 285 | public function name() { |
|
| 27 | |||
| 28 | /** |
||
| 29 | * @inheritdoc |
||
| 30 | */ |
||
| 31 | 51 | public function check_arguments(array $arguments) { |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @inheritdoc |
||
| 45 | */ |
||
| 46 | 13 | public function compile(Query $query, Rule $rule) { |
|
| 47 | 13 | $builder = $query->builder(); |
|
| 48 | 13 | $b = $builder->expr(); |
|
| 49 | 13 | $mode = $rule->mode(); |
|
| 50 | 13 | $checked_on = $rule->checked_on(); |
|
| 51 | 13 | $regexp = $rule->argument(0); |
|
| 52 | 13 | if ($mode == Rule::MODE_CANNOT || $mode == Rule::MODE_ONLY_CAN) { |
|
| 53 | return $builder |
||
| 54 | 9 | ->select |
|
| 55 | 9 | ( "e.id as entity_id" |
|
| 56 | 9 | , "e.file" |
|
| 57 | 9 | , "src.source" |
|
| 58 | 9 | , "src.line" |
|
| 59 | 9 | ) |
|
| 60 | 9 | ->from($query->entity_table(), "e") |
|
| 61 | ->innerJoin |
||
| 62 | 9 | ( "e", $query->source_file_table(), "src" |
|
| 63 | 9 | , $b->andX |
|
| 64 | 9 | ( $b->gte("src.line", "e.start_line") |
|
| 65 | 9 | , $b->lte("src.line", "e.end_line") |
|
| 66 | 9 | , $b->eq("src.name", "e.file") |
|
| 67 | 9 | , "src.source REGEXP ?" |
|
| 68 | 9 | ) |
|
| 69 | 9 | ) |
|
| 70 | ->where |
||
| 71 | 9 | ( $query->compile_var("e", $checked_on) |
|
| 72 | 9 | ) |
|
| 73 | 9 | ->setParameter(0, $regexp) |
|
| 74 | 9 | ->execute(); |
|
| 75 | } |
||
| 76 | 4 | if ($mode == Rule::MODE_MUST) { |
|
| 77 | return $builder |
||
| 78 | 4 | ->select |
|
| 79 | 4 | ( "e.id as entity_id" |
|
| 80 | 4 | , "e.file" |
|
| 81 | 4 | , "e.start_line as line" |
|
| 82 | 4 | , "src.source" |
|
| 83 | 4 | ) |
|
| 84 | 4 | ->from($query->entity_table(), "e") |
|
| 85 | ->innerJoin |
||
| 86 | 4 | ( "e", $query->source_file_table(), "src" |
|
| 87 | 4 | , $b->andX |
|
| 88 | 4 | ( $b->eq("src.name", "e.file") |
|
| 89 | 4 | , $b->eq("src.line", "e.start_line") |
|
| 90 | 4 | ) |
|
| 91 | 4 | ) |
|
| 92 | ->leftJoin |
||
| 93 | 4 | ( "e", $query->source_file_table(), "match" |
|
| 94 | 4 | , $b->andX |
|
| 95 | 4 | ( $b->eq("match.name", "e.file") |
|
| 96 | 4 | , $b->eq("match.line", "e.start_line") |
|
| 97 | 4 | , "match.source REGEXP ?" |
|
| 98 | 4 | ) |
|
| 99 | 4 | ) |
|
| 100 | ->where |
||
| 101 | 4 | ( $query->compile_var("e", $checked_on) |
|
| 102 | 4 | , "match.line IS NULL" |
|
| 103 | 4 | ) |
|
| 104 | 4 | ->setParameter(0, $regexp) |
|
| 105 | 4 | ->execute(); |
|
| 106 | } |
||
| 107 | throw new \LogicException("Unknown rule mode: '$mode'"); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @inheritdoc |
||
| 112 | */ |
||
| 113 | 2 | public function pprint(Rule $rule) { |
|
| 116 | } |
||
| 117 |
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.