| Conditions | 11 |
| Paths | 43 |
| Total Lines | 126 |
| Code Lines | 59 |
| Lines | 19 |
| Ratio | 15.08 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 131 | public function create() |
||
| 132 | { |
||
| 133 | $root_id = $this->getRootID(); |
||
| 134 | $items = $this->getItems(); |
||
| 135 | $nesting = $this->getNesting(); |
||
| 136 | |||
| 137 | if (empty($items)) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | View Code Duplication | foreach ($items as $item) { |
|
| 142 | if (null !== $item->getParentId()) { |
||
| 143 | $children[$item->getParentId()][] = $item; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | // loop will be false if the root has no children (i.e., an empty menu!) |
||
| 148 | $loop = !empty($children[$root_id]); |
||
| 149 | |||
| 150 | // initializing $parent as the root |
||
| 151 | $parent = $root_id; |
||
| 152 | $parent_stack = array(); |
||
| 153 | |||
| 154 | $html = []; |
||
| 155 | |||
| 156 | // HTML wrapper for the menu (open) |
||
| 157 | $html[] = '<ul>'; |
||
| 158 | |||
| 159 | $html[] = !empty($before['first_root_li']) ? str_repeat("\t", $nesting + 1).$before['first_root_li'] : ''; |
||
| 160 | |||
| 161 | // loop |
||
| 162 | while ($loop && (($item = each($children[$parent])) || ($parent > $root_id))) { |
||
| 163 | View Code Duplication | if (is_object($item['value'])) { |
|
| 164 | /** |
||
| 165 | * @var MenuItem $obj |
||
| 166 | */ |
||
| 167 | $obj = $item['value']; |
||
| 168 | $item = [ |
||
| 169 | 'id' => $obj->getId(), |
||
| 170 | 'parent_id' => $obj->getParentId(), |
||
| 171 | 'title' => $obj->getTitle(), |
||
| 172 | 'slug' => $obj->getSlug(), |
||
| 173 | 'caption' => $obj->getCaption(), |
||
| 174 | 'position' => $obj->getPosition(), |
||
| 175 | ]; |
||
| 176 | } |
||
| 177 | |||
| 178 | // HTML for menu item containing children (close) |
||
| 179 | if ($item === false) { |
||
| 180 | $parent = array_pop($parent_stack); |
||
| 181 | $html[] = str_repeat("\t", (count($parent_stack) + 1) * 2 + $nesting).'</ul>'; |
||
| 182 | $html[] = str_repeat("\t", (count($parent_stack) + 1) * 2 - 1 + $nesting).'</li>'; |
||
| 183 | } |
||
| 184 | |||
| 185 | // HTML for menu item containing children (open) |
||
| 186 | elseif (!empty($children[$item['id']])) { |
||
| 187 | $tab = str_repeat("\t", (count($parent_stack) + 1) * 2 - 1 + $nesting); |
||
| 188 | |||
| 189 | /* |
||
| 190 | * <li> with <ul> |
||
| 191 | */ |
||
| 192 | $html[] = sprintf( |
||
| 193 | '%1$s'.'<li>%2$s - <a'.'%3$s'.' href="'.'%4$s'.'">%5$s</a> – pozycja: %6$s'. |
||
| 194 | ' <a href="/admin/appearance/menu/edit/%2$s" class="btn btn-primary btn-xs">Edytuj</a>'. |
||
| 195 | ' <a href="/admin/appearance/menu/del/%2$s" class="btn btn-danger btn-xs">Usuń</a>', |
||
| 196 | # %1$s tabulation |
||
| 197 | $tab, |
||
| 198 | |||
| 199 | $item['id'], |
||
| 200 | |||
| 201 | # %2$s a title="" |
||
| 202 | $this->isAttribute('title', $item['caption']), |
||
| 203 | |||
| 204 | # %3$s a href="" |
||
| 205 | $item['slug'], |
||
| 206 | |||
| 207 | # %4$s text inside item |
||
| 208 | $item['title'], |
||
| 209 | |||
| 210 | $item['position'] |
||
| 211 | ); |
||
| 212 | |||
| 213 | /* |
||
| 214 | * sub <ul> in <li> |
||
| 215 | */ |
||
| 216 | $html[] = sprintf( |
||
| 217 | '%1$s'.'<ul>', |
||
| 218 | # %1$s tabulation |
||
| 219 | $tab."\t" |
||
| 220 | ); |
||
| 221 | |||
| 222 | $parent_stack[] = $item['parent_id']; |
||
| 223 | $parent = $item['id']; |
||
| 224 | } |
||
| 225 | |||
| 226 | // HTML for menu item with no children (aka "leaf") |
||
| 227 | else { |
||
| 228 | $html[] = sprintf( |
||
| 229 | '%1$s'.'<li>%2$s - <a'.'%3$s'.' href="'.'%4$s'.'">%5$s</a> – pozycja: %6$s'. |
||
| 230 | ' <a href="/admin/appearance/menu/edit/%2$s" class="btn btn-primary btn-xs">Edytuj</a>'. |
||
| 231 | ' <a href="/admin/appearance/menu/del/%2$s" class="btn btn-danger btn-xs">Edytuj</a>', |
||
| 232 | |||
| 233 | # %1$s tabulation |
||
| 234 | str_repeat("\t", (count($parent_stack) + 1) * 2 - 1 + $nesting), |
||
| 235 | |||
| 236 | $item['id'], |
||
| 237 | |||
| 238 | # %2$s a title="" |
||
| 239 | $this->isAttribute('title', $item['caption']), |
||
| 240 | |||
| 241 | # %3$s a href="" |
||
| 242 | $item['slug'], |
||
| 243 | |||
| 244 | # %4$s text inside item |
||
| 245 | $item['title'], |
||
| 246 | |||
| 247 | $item['position'] |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // HTML wrapper for the menu (close) |
||
| 253 | $html[] = str_repeat("\t", $nesting).'</ul>'; |
||
| 254 | |||
| 255 | return implode("\n", array_filter($html))."\n"; |
||
| 256 | } |
||
| 257 | } |
||
| 258 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: