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 |
||
14 | class PackageXmlParser implements Parser |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var \SplFileObject The package.xml file |
||
19 | */ |
||
20 | protected $file = null; |
||
21 | |||
22 | /** |
||
23 | * @var array Map of package content types to path prefixes |
||
24 | */ |
||
25 | protected $targets = array(); |
||
26 | |||
27 | /** |
||
28 | * @param string $packageXmlFile |
||
29 | */ |
||
30 | 5 | public function __construct($packageXmlFile) |
|
34 | |||
35 | /** |
||
36 | * @return array |
||
37 | * @throws \ErrorException |
||
38 | */ |
||
39 | 4 | View Code Duplication | public function getMappings() |
48 | |||
49 | /** |
||
50 | * @throws \RuntimeException |
||
51 | * @return array |
||
52 | */ |
||
53 | 3 | protected function parseMappings() |
|
54 | { |
||
55 | 3 | $map = array(); |
|
56 | |||
57 | /** @var $package \SimpleXMLElement */ |
||
58 | 3 | $package = simplexml_load_file($this->file->getPathname()); |
|
59 | 3 | if (isset($package)) { |
|
60 | 3 | foreach ($package->xpath('//contents/target') as $target) { |
|
61 | try { |
||
62 | 3 | $basePath = $this->getTargetPath($target); |
|
63 | |||
64 | 3 | foreach ($target->children() as $child) { |
|
65 | 3 | foreach ($this->getElementPaths($child) as $elementPath) { |
|
66 | 3 | if (pathinfo($elementPath, PATHINFO_EXTENSION) == 'txt') { |
|
67 | continue; |
||
68 | } |
||
69 | 3 | $relativePath = $basePath . '/' . $elementPath; |
|
70 | //remove the any trailing './' or '.' from the targets base-path. |
||
71 | 3 | if (strpos($relativePath, './') === 0) { |
|
72 | 3 | $relativePath = substr($relativePath, 2); |
|
73 | 3 | } |
|
74 | 3 | $map[] = array($relativePath, $relativePath); |
|
75 | 3 | } |
|
76 | 3 | } |
|
77 | |||
78 | 3 | } catch (\RuntimeException $e) { |
|
79 | // Skip invalid targets |
||
80 | 2 | continue; |
|
81 | } |
||
82 | 3 | } |
|
83 | 3 | } |
|
84 | 3 | return $map; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param \SimpleXMLElement $target |
||
89 | * @return string |
||
90 | * @throws \RuntimeException |
||
91 | */ |
||
92 | 3 | protected function getTargetPath(\SimpleXMLElement $target) |
|
101 | |||
102 | /** |
||
103 | * @return array |
||
104 | */ |
||
105 | 3 | protected function getTargetsDefinitions() |
|
118 | |||
119 | /** |
||
120 | * @param \SimpleXMLElement $element |
||
121 | * @return array |
||
122 | * @throws \RuntimeException |
||
123 | */ |
||
124 | 3 | protected function getElementPaths(\SimpleXMLElement $element) |
|
153 | } |
||
154 |
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.