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 FileContentReader implements ContentReader |
||
14 | { |
||
15 | const PROPERTIES_MATCH = '/@([a-z0-9]+)=([^\n]+)\n/'; |
||
16 | |||
17 | /** |
||
18 | * @var \SplFileInfo |
||
19 | */ |
||
20 | protected $contentDir; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $contentPath; |
||
26 | |||
27 | public function __construct($contentDir, $contentPath) |
||
33 | |||
34 | /** |
||
35 | * {@inheritDocs} |
||
36 | */ |
||
37 | public function getInfo($path) |
||
45 | |||
46 | /** |
||
47 | * @param $path |
||
48 | * |
||
49 | * @return \SplFileInfo |
||
50 | */ |
||
51 | protected function getFilePath($path) |
||
58 | |||
59 | /** |
||
60 | * {@inheritDocs} |
||
61 | */ |
||
62 | View Code Duplication | public function getContent($path) |
|
73 | |||
74 | protected function readProperties($markdown) |
||
83 | |||
84 | protected function removeProperties($markdown) |
||
88 | } |
||
89 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: