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 |
||
17 | abstract class TextToSpeechAbstract |
||
18 | { |
||
19 | const VOTE_FEMALE = 'female'; |
||
20 | const VOTE_MALE = 'male'; |
||
21 | |||
22 | protected $driver = null; |
||
23 | |||
24 | protected $vote = null; |
||
25 | |||
26 | protected $speaker = ''; |
||
27 | |||
28 | /** @var string */ |
||
29 | protected $cacheDir = null; |
||
30 | |||
31 | protected $isCached = false; |
||
32 | |||
33 | protected $results = []; |
||
34 | |||
35 | protected $debug = false; |
||
36 | |||
37 | /** |
||
38 | * @var string[] |
||
39 | */ |
||
40 | protected $lines = []; |
||
41 | |||
42 | /** |
||
43 | * @var LoggerInterface |
||
44 | */ |
||
45 | protected $logger; |
||
46 | |||
47 | /** |
||
48 | * @param LoggerInterface $cache |
||
|
|||
49 | * @param string[] $options |
||
50 | */ |
||
51 | public function __construct(LoggerInterface $logger = null, array $options = []) |
||
69 | |||
70 | /** |
||
71 | * @param string $text |
||
72 | * @return $this |
||
73 | */ |
||
74 | View Code Duplication | public function addText($text) |
|
84 | |||
85 | /** |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function process() |
||
94 | |||
95 | public function convert() |
||
140 | |||
141 | public function getResult() |
||
145 | |||
146 | private function calcCacheKey($text) |
||
150 | |||
151 | /** |
||
152 | * Синтезирует фразу $text |
||
153 | * |
||
154 | * @param string $text Текст для преоброзование |
||
155 | * @param string $outFile Имя файла с полным путём |
||
156 | * |
||
157 | * @return bool true если успешно |
||
158 | */ |
||
159 | abstract protected function synthesize($text, $outFile); |
||
160 | } |
||
161 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.