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 |
||
15 | class PageContentReader extends FileContentReader implements ContentReader |
||
16 | { |
||
17 | /** |
||
18 | * @var \Knp\Bundle\MarkdownBundle\MarkdownParserInterface |
||
19 | */ |
||
20 | private $parser; |
||
21 | |||
22 | public function __construct($contentDir, $contentPath, MarkdownParserInterface $parser) |
||
27 | |||
28 | /** |
||
29 | * @param string $path |
||
30 | * |
||
31 | * @return Page |
||
32 | */ |
||
33 | public function getPage($path) |
||
37 | |||
38 | /** |
||
39 | * @param string $path |
||
40 | * @param bool $fetchSubNav |
||
41 | * |
||
42 | * @return Page |
||
43 | */ |
||
44 | protected function buildPage($path, $fetchSubNav = true) |
||
63 | |||
64 | /** |
||
65 | * @param Content $c |
||
66 | * @param string $path |
||
67 | * |
||
68 | * @return Page |
||
69 | */ |
||
70 | View Code Duplication | protected function transformContent(Content $c, $path) |
|
81 | |||
82 | protected function fixLinks($html, $page) |
||
92 | |||
93 | /** |
||
94 | * @param $path |
||
95 | * |
||
96 | * @return string |
||
97 | * @see http://www.php.net/manual/en/function.realpath.php#81935 |
||
98 | */ |
||
99 | protected function normalizePath($path) |
||
109 | |||
110 | protected function getSubnav(\SplFileInfo $file) |
||
119 | } |
||
120 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.