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 |
||
24 | class HtmlMenu extends HtmlSemCollection { |
||
25 | private $_itemHeader; |
||
26 | |||
27 | public function __construct($identifier, $items=array()) { |
||
31 | |||
32 | /** |
||
33 | * Sets the menu type |
||
34 | * @param string $type one of text,item |
||
35 | * @return \Ajax\semantic\html\collections\HtmlMenu |
||
36 | */ |
||
37 | public function setType($type="") { |
||
40 | |||
41 | public function setActiveItem($index) { |
||
48 | |||
49 | private function getItemToInsert($item) { |
||
57 | |||
58 | private function afterInsert($item) { |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * {@inheritDoc} |
||
70 | * |
||
71 | * @see \Ajax\common\html\html5\HtmlCollection::addItem() |
||
72 | */ |
||
73 | public function addItem($item) { |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * {@inheritDoc} |
||
81 | * |
||
82 | * @see \Ajax\common\html\HtmlCollection::insertItem() |
||
83 | */ |
||
84 | public function insertItem($item, $position=0) { |
||
88 | |||
89 | public function generateMenuAsItem($menu, $header=null) { |
||
102 | |||
103 | public function addMenuAsItem($menu, $header=null) { |
||
106 | |||
107 | public function addPopupAsItem($value, $identifier, $content="") { |
||
116 | |||
117 | public function addDropdownAsItem($value, $items=NULL) { |
||
124 | |||
125 | /** |
||
126 | * |
||
127 | * {@inheritDoc} |
||
128 | * |
||
129 | * @see \Ajax\common\html\html5\HtmlCollection::createItem() |
||
130 | */ |
||
131 | protected function createItem($value) { |
||
135 | |||
136 | public function setInverted() { |
||
139 | |||
140 | public function setSecondary() { |
||
143 | |||
144 | public function setVertical() { |
||
147 | |||
148 | public function setPosition($value="right") { |
||
151 | |||
152 | public function setPointing($value=Direction::NONE) { |
||
155 | |||
156 | public function asTab($vertical=false) { |
||
164 | |||
165 | public function asPagination() { |
||
171 | |||
172 | public function setFixed() { |
||
175 | |||
176 | public function setFluid() { |
||
179 | |||
180 | public function setCompact() { |
||
183 | |||
184 | /* |
||
185 | * (non-PHPdoc) |
||
186 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
187 | */ |
||
188 | public function fromDatabaseObject($object, $function) { |
||
195 | |||
196 | /** |
||
197 | * Defines the menu width |
||
198 | * @param int $width |
||
199 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
200 | */ |
||
201 | View Code Duplication | public function setWidth($width) { |
|
208 | |||
209 | public function addImage($identifier, $src="", $alt="") { |
||
212 | |||
213 | public static function vertical($identifier, $items=array()) { |
||
216 | |||
217 | public function getItemHeader() { |
||
220 | } |
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.