Completed
Push — dev ( 845ef5...8eacd8 )
by
unknown
02:50
created

Window::removeChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui;
4
5
use eXpansion\Framework\Core\Exceptions\Gui\MissingCloseActionException;
6
use eXpansion\Framework\Core\Helpers\Translations;
7
use eXpansion\Framework\Core\Model\UserGroups\Group;
8
use FML\Controls\Frame;
9
use FML\Controls\Label;
10
use FML\Controls\Quad;
11
use FML\Controls\Quads\Quad_Bgs1;
12
use FML\Controls\Quads\Quad_Bgs1InRace;
13
use FML\Elements\Dico;
14
use FML\Elements\Format;
15
use FML\Types\Container;
16
use FML\Types\Renderable;
17
18
class Window extends Widget implements Container
19
{
20 3
    public function __construct(
21
        Group $group,
22
        ManiaScriptFactory $windowManiaScriptFactory,
23
        Translations $translationHelper,
24
        $name,
25
        $sizeX,
26
        $sizeY,
27
        $posX = null,
28
        $posY = null
29
    )
30
    {
31 3
        parent::__construct($group, $translationHelper, $name, $sizeX, $sizeY, $posX, $posY);
32
33 3
        $this->translationHelper = $translationHelper;
34 3
        $windowFrame = $this->windowFrame;
35
36 3
        $titleHeight = 5.5;
37 3
        $closeButtonWidth = 9.5;
38 3
        $titlebarColor = "3afe";
39
40
        // Frame to handle the content of the window.
41 3
        $this->contentFrame->setPosition(2, -$titleHeight - 2);
42 3
        $this->contentFrame->setSize($sizeX - 4, $sizeY - $titleHeight - 4);
43 3
        $windowFrame->addChild($this->contentFrame);
44
45
        // Title bar & title.
46 3
        $titleLabel = new Label();
47 3
        $titleLabel->setPosition(3, -$titleHeight / 3 - 1)
48 3
            ->setAlign(Label::LEFT, Label::CENTER2)
49 3
            ->setTextId($name)
50 3
            ->setTextColor('fff')
51 3
            ->setTextSize(2)
52 3
            ->setTranslate(true)
53 3
            ->setTextFont('RajdhaniMono')
54 3
            ->setId("TitleText");
55 3
        $windowFrame->addChild($titleLabel);
56
57 3
        $titleBar = new Quad();
58 3
        $titleBar->setSize($sizeX, 0.33)
59 3
            ->setPosition(0, -$titleHeight)
60 3
            ->setBackgroundColor('fff');
61 3
        $windowFrame->addChild($titleBar);
62
63 3
        $titleBar = new Quad();
64 3
        $titleBar->setSize($sizeX / 4, 0.5)
65 3
            ->setPosition(0, -$titleHeight)
66 3
            ->setBackgroundColor('fff');
67 3
        $windowFrame->addChild($titleBar);
68
69 3
        $titleBar = new Quad('Title');
70 3
        $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight)
71 3
            ->setBackgroundColor($titlebarColor)
72 3
            ->setScriptEvents(true);
73 3
        $windowFrame->addChild($titleBar);
74
75 3
        $this->closeButton = new Label('Close');
76 3
        $this->closeButton->setSize($closeButtonWidth, $titleHeight)
77 3
            ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2)
78 3
            ->setAlign(Label::CENTER, Label::CENTER2)
79 3
            ->setText("✖")
80 3
            ->setTextColor('fff')
81 3
            ->setTextSize(2)
82 3
            ->setTextFont('OswaldMono')
83 3
            ->setScriptEvents(true)
84 3
            ->setAreaColor('d00')
85 3
            ->setAreaFocusColor('f22');
86 3
        $windowFrame->addChild($this->closeButton);
87
88
        //body
89 3
        $body = new Quad_Bgs1();
90 3
        $body->setSize($sizeX, $sizeY - $titleHeight)
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FML\Controls\Control as the method setScriptEvents() does only exist in the following sub-classes of FML\Controls\Control: FML\Controls\Audio, FML\Controls\Entry, FML\Controls\FileEntry, FML\Controls\Frame3d, FML\Controls\Label, FML\Controls\Labels\Label_Button, FML\Controls\Labels\Label_Text, FML\Controls\Quad, FML\Controls\Quads\Quad_321Go, FML\Controls\Quads\Quad_BgRaceScore2, FML\Controls\Quads\Quad_Bgs1, FML\Controls\Quads\Quad_Bgs1InRace, FML\Controls\Quads\Quad_BgsButtons, FML\Controls\Quads\Quad_BgsChallengeMedals, FML\Controls\Quads\Quad_BgsPlayerCard, FML\Controls\Quads\Quad_Copilot, FML\Controls\Quads\Quad_Emblems, FML\Controls\Quads\Quad_EnergyBar, FML\Controls\Quads\Quad_Hud3dEchelons, FML\Controls\Quads\Quad_Hud3dIcons, FML\Controls\Quads\Quad_Icons128x128_1, FML\Controls\Quads\Quad_Icons128x128_Blink, FML\Controls\Quads\Quad_Icons128x32_1, FML\Controls\Quads\Quad_Icons64x64_1, FML\Controls\Quads\Quad_Icons64x64_2, FML\Controls\Quads\Quad_ManiaPlanetLogos, FML\Controls\Quads\Quad_ManiaPlanetMainMenu, FML\Controls\Quads\Quad_ManiaplanetSystem, FML\Controls\Quads\Quad_MedalsBig, FML\Controls\Quads\Quad_TitleLogos, FML\Controls\Quads\Quad_...structionBullet_Buttons, FML\Controls\Quads\Quad_UIConstruction_Buttons, FML\Controls\Quads\Quad_UIConstruction_Buttons2, FML\Controls\Quads\Quad_UiSMSpectatorScoreBig, FML\Controls\TextEdit, FML\Controls\Video. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
91 3
            ->setPosition(0, -$titleHeight)
92 3
            ->setSubStyle(Quad_Bgs1::SUBSTYLE_BgWindow3)
93 3
            ->setId('WindowBg')
94 3
            ->setScriptEvents(true);
95 3
        $windowFrame->addChild($body);
96
97 3
        $body = new Quad_Bgs1InRace();
98 3
        $body->setSize($sizeX + 10, $sizeY + 10)
99 3
            ->setPosition(-5, 5)
100 3
            ->setSubStyle(Quad_Bgs1InRace::SUBSTYLE_BgButtonShadow);
101 3
        $windowFrame->addChild($body);
102
103
        // Add maniascript for window handling.
104 3
        $this->manialink->addChild($windowManiaScriptFactory->createScript(['']));
105 3
    }
106
107
    /**
108
     * Set action to close the window.
109
     *
110
     * @param $actionId
111
     */
112 1
    public function setCloseAction($actionId)
113
    {
114 1
        $this->closeButton->setDataAttributes(['action' => $actionId]);
115 1
    }
116
117 2
    public function getXml()
118
    {
119 2
        if (empty($this->closeButton->getDataAttribute('action'))) {
120 1
            throw new MissingCloseActionException("Close action is missing for window. Check if you are using the proper factory.");
121
        }
122
123 1
        return parent::getXml();
124
    }
125
126
127
}
128