| Conditions | 1 |
| Paths | 1 |
| Total Lines | 99 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 2 |
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 35 | public function __construct( |
||
| 36 | Group $group, |
||
| 37 | ManiaScriptFactory $windowManiaScriptFactory, |
||
| 38 | Translations $translationHelper, |
||
| 39 | $name, |
||
| 40 | $sizeX, |
||
| 41 | $sizeY, |
||
| 42 | $posX = null, |
||
| 43 | $posY = null |
||
| 44 | ) |
||
| 45 | { |
||
| 46 | parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY); |
||
| 47 | |||
| 48 | $this->translationHelper = $translationHelper; |
||
| 49 | |||
| 50 | $titleHeight = 5.5; |
||
| 51 | $closeButtonWidth = 9.5; |
||
| 52 | $titlebarColor = "3afe"; |
||
| 53 | |||
| 54 | // Manialink containing everything |
||
| 55 | $this->manialink = new \FML\ManiaLink(); |
||
| 56 | $this->manialink->setId($this->getId()) |
||
| 57 | ->setName($name) |
||
| 58 | ->setVersion(\FML\ManiaLink::VERSION_3); |
||
| 59 | |||
| 60 | $this->dictionary = new Dico(); |
||
| 61 | $this->manialink->setDico($this->dictionary); |
||
| 62 | |||
| 63 | $windowFrame = new Frame('Window'); |
||
| 64 | $windowFrame->setPosition($posX, $posY); |
||
| 65 | $this->manialink->addChild($windowFrame); |
||
| 66 | |||
| 67 | // Frame to handle the content of the window. |
||
| 68 | $this->contentFrame = new Frame(); |
||
| 69 | $this->contentFrame->setPosition(2, -$titleHeight - 2); |
||
| 70 | $this->contentFrame->setSize($sizeX - 4, $sizeY - $titleHeight - 4); |
||
| 71 | $windowFrame->addChild($this->contentFrame); |
||
| 72 | |||
| 73 | // Title bar & title. |
||
| 74 | $titleLabel = new Label(); |
||
| 75 | $titleLabel->setPosition(3, -$titleHeight / 3 - 1) |
||
| 76 | ->setAlign(Label::LEFT, Label::CENTER2) |
||
| 77 | ->setTextId($name) |
||
| 78 | ->setTextColor('fff') |
||
| 79 | ->setTextSize(2) |
||
| 80 | ->setTranslate(true) |
||
| 81 | ->setTextFont('RajdhaniMono') |
||
| 82 | ->setId("TitleText"); |
||
| 83 | $windowFrame->addChild($titleLabel); |
||
| 84 | |||
| 85 | $titleBar = new Quad(); |
||
| 86 | $titleBar->setSize($sizeX, 0.33) |
||
| 87 | ->setPosition(0, -$titleHeight) |
||
| 88 | ->setBackgroundColor('fff'); |
||
| 89 | $windowFrame->addChild($titleBar); |
||
| 90 | |||
| 91 | $titleBar = new Quad(); |
||
| 92 | $titleBar->setSize($sizeX / 4, 0.5) |
||
| 93 | ->setPosition(0, -$titleHeight) |
||
| 94 | ->setBackgroundColor('fff'); |
||
| 95 | $windowFrame->addChild($titleBar); |
||
| 96 | |||
| 97 | $titleBar = new Quad('Title'); |
||
| 98 | $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight) |
||
| 99 | ->setBackgroundColor($titlebarColor) |
||
| 100 | ->setScriptEvents(true); |
||
| 101 | $windowFrame->addChild($titleBar); |
||
| 102 | |||
| 103 | $this->closeButton = new Label('Close'); |
||
| 104 | $this->closeButton->setSize($closeButtonWidth, $titleHeight) |
||
| 105 | ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2) |
||
| 106 | ->setAlign(Label::CENTER, Label::CENTER2) |
||
| 107 | ->setText("✖") |
||
| 108 | ->setTextColor('fff') |
||
| 109 | ->setTextSize(2) |
||
| 110 | ->setTextFont('OswaldMono') |
||
| 111 | ->setScriptEvents(true) |
||
| 112 | ->setAreaColor('d00') |
||
| 113 | ->setAreaFocusColor('f22'); |
||
| 114 | $windowFrame->addChild($this->closeButton); |
||
| 115 | |||
| 116 | //body |
||
| 117 | $body = new Quad_Bgs1(); |
||
| 118 | $body->setSize($sizeX, $sizeY - $titleHeight) |
||
|
|
|||
| 119 | ->setPosition(0, -$titleHeight) |
||
| 120 | ->setSubStyle(Quad_Bgs1::SUBSTYLE_BgWindow3) |
||
| 121 | ->setId('WindowBg') |
||
| 122 | ->setScriptEvents(true); |
||
| 123 | $windowFrame->addChild($body); |
||
| 124 | |||
| 125 | $body = new Quad_Bgs1InRace(); |
||
| 126 | $body->setSize($sizeX + 10, $sizeY + 10) |
||
| 127 | ->setPosition(-5, 5) |
||
| 128 | ->setSubStyle(Quad_Bgs1InRace::SUBSTYLE_BgButtonShadow); |
||
| 129 | $windowFrame->addChild($body); |
||
| 130 | |||
| 131 | // Add maniascript for window handling. |
||
| 132 | $this->manialink->addChild($windowManiaScriptFactory->createScript([''])); |
||
| 133 | } |
||
| 134 | |||
| 325 |
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: