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:
Complex classes like HtmlMenu often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlMenu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class HtmlMenu extends HtmlSemCollection { |
||
26 | private $_itemHeader; |
||
27 | |||
28 | public function __construct($identifier, $items=array()) { |
||
32 | |||
33 | /** |
||
34 | * Sets the menu type |
||
35 | * @param string $type one of text,item |
||
36 | * @return \Ajax\semantic\html\collections\HtmlMenu |
||
37 | */ |
||
38 | public function setType($type="") { |
||
41 | |||
42 | public function setActiveItem($index) { |
||
49 | |||
50 | private function getItemToInsert($item) { |
||
58 | |||
59 | private function afterInsert($item) { |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | * {@inheritDoc} |
||
71 | * |
||
72 | * @see \Ajax\common\html\html5\HtmlCollection::addItem() |
||
73 | */ |
||
74 | public function addItem($item) { |
||
78 | |||
79 | /** |
||
80 | * |
||
81 | * {@inheritDoc} |
||
82 | * |
||
83 | * @see \Ajax\common\html\HtmlCollection::insertItem() |
||
84 | */ |
||
85 | public function insertItem($item, $position=0) { |
||
89 | |||
90 | public function generateMenuAsItem($menu, $header=null) { |
||
103 | |||
104 | public function addMenuAsItem($menu, $header=null) { |
||
107 | |||
108 | public function addPopupAsItem($value, $identifier, $content="") { |
||
117 | |||
118 | public function addDropdownAsItem($value, $items=NULL) { |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | * {@inheritDoc} |
||
129 | * |
||
130 | * @see \Ajax\common\html\html5\HtmlCollection::createItem() |
||
131 | */ |
||
132 | protected function createItem($value) { |
||
136 | |||
137 | public function setInverted() { |
||
140 | |||
141 | public function setSecondary() { |
||
144 | |||
145 | public function setVertical() { |
||
148 | |||
149 | public function setPosition($value="right") { |
||
152 | |||
153 | public function setPointing($value=Direction::NONE) { |
||
156 | |||
157 | public function asTab($vertical=false) { |
||
165 | |||
166 | public function asPagination() { |
||
172 | |||
173 | public function setFixed() { |
||
176 | |||
177 | public function setFluid() { |
||
180 | |||
181 | public function setCompact() { |
||
184 | |||
185 | /* |
||
186 | * (non-PHPdoc) |
||
187 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
188 | */ |
||
189 | public function fromDatabaseObject($object, $function) { |
||
196 | |||
197 | /** |
||
198 | * Defines the menu width |
||
199 | * @param int $width |
||
200 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
201 | */ |
||
202 | View Code Duplication | public function setWidth($width) { |
|
209 | |||
210 | public function addImage($identifier, $src="", $alt="") { |
||
213 | |||
214 | public static function vertical($identifier, $items=array()) { |
||
217 | |||
218 | public function getItemHeader() { |
||
221 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.