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 |
||
13 | class PhpStanLoader implements FileChecker |
||
14 | { |
||
15 | protected $lineRegex = '/^\s+(?<lineNumber>[0-9]+)/'; |
||
16 | |||
17 | protected $file; |
||
18 | protected $relatedRegex = '#(function|method) (?:(?P<class>.*?)::)?(?P<function>.*?)[ \(]#'; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $invalidLines = []; |
||
24 | |||
25 | /** |
||
26 | * @param string $filename the path to the phpstan.txt file |
||
27 | */ |
||
28 | public function __construct($filename) |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | public function parseLines() |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | View Code Duplication | public function getErrorsOnLine($file, $lineNumber) |
|
70 | |||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function handleNotFoundFile() |
||
79 | |||
80 | /** |
||
81 | * @param string $line |
||
82 | * @param string $currentFile |
||
83 | * @return string the currentFileName |
||
84 | */ |
||
85 | protected function checkForFilename($line, $currentFile) |
||
92 | |||
93 | protected function getLineNumber($line, $currentLineNumber) |
||
106 | |||
107 | protected function getMessage($line) |
||
111 | |||
112 | protected function isExtendedMessage($line) |
||
116 | |||
117 | protected function trimLines() |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public static function getDescription() |
||
133 | |||
134 | protected function handleRelatedError($filename, $line, $error) |
||
155 | |||
156 | /** |
||
157 | * @param $filename |
||
158 | * @param $lineNumber |
||
159 | * @param $error |
||
160 | */ |
||
161 | protected function addError($filename, $lineNumber, $error) |
||
168 | |||
169 | /** |
||
170 | * @param $matches |
||
171 | * @return Reflector |
||
172 | */ |
||
173 | protected function getReflector($matches) |
||
186 | |||
187 | private function appendError($filename, $lineNumber, $error) |
||
193 | } |
||
194 |
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.