| Conditions | 9 |
| Paths | 25 |
| Total Lines | 120 |
| Code Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 197 | protected function createSubMenu(Manialink $manialink, Frame $frame, ParentItem $parentItem, $displayLevel) |
||
| 198 | { |
||
| 199 | |||
| 200 | if ($displayLevel > 0) { |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | $posX = $displayLevel * (-160.0 / 3); |
||
| 205 | $posY = ($displayLevel * (-100.0 / 3)) * 0.5; |
||
| 206 | $scale = (0.5 / ($displayLevel + 1)) + 0.5; |
||
| 207 | |||
| 208 | $contentFrame = new Frame(); |
||
| 209 | $contentFrame->setScale($scale); |
||
| 210 | $contentFrame->setPosition($posX, $posY); |
||
| 211 | |||
| 212 | |||
| 213 | if ($displayLevel > 0) { |
||
| 214 | $overlay = new Quad(); |
||
| 215 | $overlay->setSize(60, 120); |
||
| 216 | $overlay->setPosition(-30, 0); |
||
| 217 | $overlay->setStyles("Bgs1", "BgDialogBlur"); |
||
| 218 | |||
| 219 | $action = $this->actionFactory->createManialinkAction( |
||
| 220 | $manialink, |
||
| 221 | [$this, 'callbackItemClick'], |
||
| 222 | ['item' => $parentItem] |
||
| 223 | ); |
||
| 224 | |||
| 225 | |||
| 226 | $contentFrame->addChild($overlay); |
||
| 227 | $overlay->setAction($action); |
||
| 228 | } |
||
| 229 | |||
| 230 | /* TITLE */ |
||
| 231 | $titleLabel = $this->uiFactory->createLabel($parentItem->getLabelId(), Label::TYPE_TITLE); |
||
| 232 | $titleLabel->setTextSize(9) |
||
| 233 | ->setSize(60, 12) |
||
| 234 | ->setPosition(0, 0) |
||
| 235 | ->setTranslate(true) |
||
| 236 | ->setTextColor('fff') |
||
| 237 | ->setHorizontalAlign("center"); |
||
| 238 | |||
| 239 | $contentFrame->addChild($titleLabel); |
||
| 240 | |||
| 241 | $titleLine = $this->uiFactory->createLine(-60 * $scale, -12); |
||
| 242 | $titleLine->setLength(120 * $scale); |
||
| 243 | $titleLine->setStroke(0.33)->setColor('fff'); |
||
| 244 | |||
| 245 | $contentFrame->addChild($titleLine); |
||
| 246 | |||
| 247 | $posY = -30; |
||
| 248 | $delay = 0; |
||
| 249 | |||
| 250 | // generate back button |
||
| 251 | $path = explode("/", $parentItem->getPath()); |
||
| 252 | array_shift($path); |
||
| 253 | |||
| 254 | $path = array_reverse($path); |
||
| 255 | array_shift($path); |
||
| 256 | $path = array_reverse($path); |
||
| 257 | |||
| 258 | if ($path && count($path) >= 0) { |
||
| 259 | $rootItem = $this->menuItemProvider->getRootItem(); |
||
| 260 | $backItem = $rootItem->getChild(implode("/", $path)); |
||
| 261 | |||
| 262 | $button = $this->uiFactory->createLabel("expansion_menu.menu_back"); |
||
| 263 | $button->setScale(1.5); |
||
| 264 | $this->animation->addAnimation($button, 'scale="1"', 300, $delay, Animation::ElasticOut); |
||
| 265 | $delay += 50; |
||
| 266 | |||
| 267 | $button->setPosition(0, $posY); |
||
| 268 | $button->setSize(50, 8); |
||
| 269 | $button->setTranslate(true); |
||
| 270 | $button->setTextSize(4); |
||
| 271 | $button->setAlign("center", "center2"); |
||
| 272 | $button->addClass('menuItem'); |
||
| 273 | $button->setTextColor("aaa"); |
||
| 274 | $button->setTextPrefix("⏴ "); |
||
| 275 | $action = $this->actionFactory->createManialinkAction( |
||
| 276 | $manialink, |
||
| 277 | [$this, 'callbackItemClick'], |
||
| 278 | ['item' => $backItem, 'ml' => $manialink] |
||
| 279 | ); |
||
| 280 | $button->setAction($action); |
||
| 281 | $contentFrame->addChild($button); |
||
| 282 | $posY -= 12; |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | foreach ($parentItem->getChilds() as $item) { |
||
| 287 | if ($item->isVisibleFor($manialink->getUserGroup())) { |
||
| 288 | $button = $this->uiFactory->createLabel($item->getLabelId()); |
||
| 289 | $button->setScale(1.5); |
||
| 290 | $this->animation->addAnimation($button, 'scale="1"', 300, $delay, Animation::ElasticOut); |
||
| 291 | $delay += 50; |
||
| 292 | |||
| 293 | $button->setPosition(0, $posY); |
||
| 294 | $button->setSize(50, 8); |
||
| 295 | $button->setTranslate(true); |
||
| 296 | $button->setTextSize(4); |
||
| 297 | $button->setAlign("center", "center2"); |
||
| 298 | $button->addClass('menuItem'); |
||
| 299 | if ($item instanceof $parentItem) { |
||
| 300 | $button->setTextPrefix("⏵ "); |
||
| 301 | } |
||
| 302 | if ($displayLevel == 0) { |
||
| 303 | $action = $this->actionFactory->createManialinkAction( |
||
| 304 | $manialink, |
||
| 305 | [$this, 'callbackItemClick'], |
||
| 306 | ['item' => $item, 'ml' => $manialink] |
||
| 307 | ); |
||
| 308 | $button->setAction($action); |
||
| 309 | } |
||
| 310 | |||
| 311 | $contentFrame->addChild($button); |
||
| 312 | $posY -= 12; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | $frame->addChild($contentFrame); |
||
| 316 | } |
||
| 317 | |||
| 410 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: