| Conditions | 1 |
| Paths | 1 |
| Total Lines | 87 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 52 | public function build(ManiaLink $manialink, Frame $mainFrame, $name, $sizeX, $sizeY) |
||
| 53 | { |
||
| 54 | $titleHeight = 5.5; |
||
| 55 | $closeButtonWidth = 9.5; |
||
| 56 | $titlebarColor = "000e"; |
||
| 57 | $titleTextColor = "eff"; |
||
| 58 | |||
| 59 | // Creating sub frame to keep all the pieces together. |
||
| 60 | $frame = new Frame(); |
||
| 61 | $frame->setPosition(-2, ($titleHeight) + 2); |
||
| 62 | $mainFrame->addChild($frame); |
||
| 63 | |||
| 64 | // Size of the actual window. |
||
| 65 | $sizeX += 4; |
||
| 66 | $sizeY += $titleHeight + 2; |
||
| 67 | |||
| 68 | // Title bar & title. |
||
| 69 | $titleLabel = new Label(); |
||
| 70 | $titleLabel->setPosition(3, -$titleHeight / 3 - 1) |
||
| 71 | ->setAlign(Label::LEFT, Label::CENTER2) |
||
| 72 | ->setTextId($name) |
||
| 73 | ->setTextColor($titleTextColor) |
||
| 74 | ->setTextSize(2) |
||
| 75 | ->setTranslate(true) |
||
| 76 | ->setTextFont('RajdhaniMono') |
||
| 77 | ->setId("TitleText"); |
||
| 78 | $frame->addChild($titleLabel); |
||
| 79 | |||
| 80 | $titleBar = new Quad(); |
||
| 81 | $titleBar->setSize($sizeX, 0.33) |
||
| 82 | ->setPosition(0, -$titleHeight) |
||
| 83 | ->setBackgroundColor('fff'); |
||
| 84 | $frame->addChild($titleBar); |
||
| 85 | |||
| 86 | $titleBar = new Quad(); |
||
| 87 | $titleBar->setSize($sizeX / 4, 0.5) |
||
| 88 | ->setPosition(0, -$titleHeight) |
||
| 89 | ->setBackgroundColor('fff'); |
||
| 90 | $frame->addChild($titleBar); |
||
| 91 | |||
| 92 | $titleBar = new Quad('Title'); |
||
| 93 | $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight) |
||
| 94 | ->setBackgroundColor($titlebarColor) |
||
| 95 | ->setScriptEvents(true); |
||
| 96 | $frame->addChild($titleBar); |
||
| 97 | |||
| 98 | $closeButton = new Label('Close'); |
||
| 99 | $closeButton->setSize($closeButtonWidth, $titleHeight) |
||
| 100 | ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2) |
||
| 101 | ->setAlign(Label::CENTER, Label::CENTER2) |
||
| 102 | ->setText("✖") |
||
| 103 | ->setTextColor('fff') |
||
| 104 | ->setTextSize(2) |
||
| 105 | ->setTextFont('OswaldMono') |
||
| 106 | ->setScriptEvents(true) |
||
| 107 | ->setAreaColor($titlebarColor) |
||
| 108 | ->setAreaFocusColor('f22'); |
||
| 109 | $frame->addChild($closeButton); |
||
| 110 | |||
| 111 | |||
| 112 | //body |
||
| 113 | $body = new Quad(); |
||
| 114 | $body->setSize($sizeX, $sizeY - $titleHeight) |
||
| 115 | ->setPosition(0, -$titleHeight) |
||
| 116 | ->setBackgroundColor("222") |
||
| 117 | ->setOpacity(0.8); |
||
| 118 | $frame->addChild($body); |
||
| 119 | |||
| 120 | $body = new Quad(); |
||
| 121 | $body->setSize($sizeX, $sizeY - $titleHeight) |
||
|
|
|||
| 122 | ->setPosition(0, -$titleHeight) |
||
| 123 | ->setStyles('Bgs1', 'BgDialogBlur') |
||
| 124 | ->setId('WindowBg') |
||
| 125 | ->setScriptEvents(true); |
||
| 126 | $frame->addChild($body); |
||
| 127 | |||
| 128 | $body = new Quad(); |
||
| 129 | $body->setSize($sizeX + 10, $sizeY + 10) |
||
| 130 | ->setPosition(-5, 5) |
||
| 131 | ->setStyles('Bgs1InRace', 'BgButtonShadow'); |
||
| 132 | $frame->addChild($body); |
||
| 133 | |||
| 134 | // Add maniascript for window handling. |
||
| 135 | $manialink->addChild($this->windowManiaScriptFactory->createScript([''])); |
||
| 136 | |||
| 137 | return $closeButton; |
||
| 138 | } |
||
| 139 | |||
| 148 |
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: