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 |
||
8 | class SyntaxFormatter implements SyntaxFormatterInterface |
||
9 | { |
||
10 | const DEFAULT_QUOTE_CHAR = '"'; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $identifierQuote = self::DEFAULT_QUOTE_CHAR; |
||
16 | |||
17 | /** |
||
18 | * @param string $char |
||
19 | * |
||
20 | * @return static |
||
21 | */ |
||
22 | public function setIdentifierQuote($char) |
||
27 | |||
28 | /** |
||
29 | * @param string $identifier |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | protected function quoteIdentifier($identifier) |
||
42 | |||
43 | /** |
||
44 | * @param string $syntax |
||
45 | * @param array $params |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function format($syntax, array $params = []) |
||
79 | |||
80 | /** |
||
81 | * @param object $object |
||
82 | * @param string $method |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | private function returnMethodCall($object, $method) |
||
96 | |||
97 | /** |
||
98 | * @param array $array |
||
99 | * @param string $key |
||
100 | * |
||
101 | * @return string mixed |
||
102 | */ |
||
103 | private function returnArrayKey(array $array, $key) |
||
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.