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 |
||
22 | class SimpleXML implements Loadable { |
||
23 | |||
24 | use Supports\Json; |
||
25 | |||
26 | const XMLNS = 'urn:carica-json-dom.2013'; |
||
27 | |||
28 | /** |
||
29 | * Maximum recursions |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | private $_recursions = 100; |
||
34 | |||
35 | /** |
||
36 | * @return string[] |
||
37 | */ |
||
38 | 4 | public function getSupported() { |
|
41 | |||
42 | /** |
||
43 | * Load the json string into an DOMDocument |
||
44 | * |
||
45 | * @param mixed $source |
||
46 | * @param string $contentType |
||
47 | * @param array|\Traversable|Options $options |
||
48 | * @return Document|Result|NULL |
||
49 | */ |
||
50 | 4 | View Code Duplication | public function load($source, $contentType, $options = []) { |
51 | 4 | if (FALSE !== ($json = $this->getJson($source, $contentType, $options))) { |
|
|
|||
52 | 3 | $document = new Document('1.0', 'UTF-8'); |
|
53 | 3 | $document->appendChild( |
|
54 | 3 | $root = $document->createElementNS(self::XMLNS, 'json:json') |
|
55 | 3 | ); |
|
56 | 3 | $this->transferTo($root, $json); |
|
57 | 3 | return $document; |
|
58 | } |
||
59 | 1 | return NULL; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param \DOMNode|\DOMElement $node |
||
64 | * @param mixed $json |
||
65 | */ |
||
66 | 3 | protected function transferTo(\DOMNode $node, $json) { |
|
87 | |||
88 | /** |
||
89 | * @param \DOMNode $node |
||
90 | * @param string $name |
||
91 | * @param mixed $data |
||
92 | * @return array |
||
93 | */ |
||
94 | 3 | protected function transferChildTo(\DOMNode $node, $name, $data) { |
|
106 | } |
||
107 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.