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 |
||
27 | class Namer implements SourceAccessorInterface |
||
28 | { |
||
29 | |||
30 | use SourceMagic; |
||
31 | |||
32 | /** |
||
33 | * Working class name |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $className = ''; |
||
37 | protected $info = null; |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | * @var ApiUrl |
||
42 | */ |
||
43 | protected $link = null; |
||
44 | |||
45 | public function __construct($className = null) |
||
51 | |||
52 | /** |
||
53 | * Set wrapper defaults |
||
54 | * @return InlineWrapper |
||
55 | */ |
||
56 | public static function defaults() |
||
60 | |||
61 | public function __get($name) |
||
62 | { |
||
63 | // This is to get class name formatted, without invoking property() |
||
64 | View Code Duplication | if ($name === 'md' || $name === 'html' || $name === 'short') |
|
|
|||
65 | { |
||
66 | if (!$this->info->hasProperty($name)) |
||
67 | { |
||
68 | return (new InlineWrapper($this->_type(), (string) $this->link, $this->getTitle()))->$name; |
||
69 | } |
||
70 | } |
||
71 | return $this->_get($name); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * |
||
76 | * @param string $name |
||
77 | * @return InlineWrapper |
||
78 | */ |
||
79 | public function method($name) |
||
85 | |||
86 | protected function _method($name) |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * @param string $name |
||
94 | * @return InlineWrapper |
||
95 | */ |
||
96 | public function property($name) |
||
113 | |||
114 | protected function _property($name) |
||
118 | |||
119 | public static function __callStatic($name, $arguments) |
||
123 | |||
124 | protected function _type() |
||
128 | |||
129 | public function __toString() |
||
134 | |||
135 | private function getTitle($name = '') |
||
143 | |||
144 | } |
||
145 |
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.