Completed
Pull Request — master (#130)
by
unknown
02:52
created

WindowFrameFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 123
rs 10
c 1
b 0
f 0
ccs 0
cts 73
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B build() 0 86 1
A setManialinkInterface() 0 4 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Factory;
4
5
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
6
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
7
use FML\Controls\Control;
8
use FML\Controls\Frame;
9
use FML\Controls\Label;
10
use FML\Controls\Quad;
11
use FML\ManiaLink;
12
use FML\Types\Container;
13
14
/**
15
 * Class WindowFrameFactory
16
 *
17
 * @author    de Cramer Oliver<[email protected]>
18
 * @copyright 2017 Smile
19
 * @package eXpansion\Framework\Core\Model\Gui\Factory
20
 */
21
class WindowFrameFactory
22
{
23
    /** @var ManiaScriptFactory */
24
    protected $windowManiaScriptFactory;
25
26
    /** @var ManialinkInterface */
27
    protected $manialinkInterface;
28
29
    /**
30
     * WindowFrameFactory constructor.
31
     *
32
     * @param ManiaScriptFactory $maniaScriptFactory
33
     */
34
    public function __construct(ManiaScriptFactory $maniaScriptFactory)
35
    {
36
        $this->windowManiaScriptFactory = $maniaScriptFactory;
37
    }
38
39
    /**
40
     * Build the window frame content.
41
     *
42
     * @param ManiaLink $manialink
43
     * @param Frame|Container $mainFrame to build into
44
     * @param $name
45
     * @param float $sizeX Size of the inner frame to build the window frame around
46
     * @param float $sizeY Size of the inner frame to build the window frame around
47
     * @return Control
48
     */
49
    public function build(ManiaLink $manialink, Frame $mainFrame, $name, $sizeX, $sizeY)
50
    {
51
        $titleHeight = 5.5;
52
        $closeButtonWidth = 9.5;
53
        $titlebarColor = "000e";
54
        $titleTextColor = "eff";
55
56
        // Creating sub frame to keep all the pieces together.
57
        $frame = new Frame();
58
        $frame->setPosition(-2, ($titleHeight) + 2);
59
        $mainFrame->addChild($frame);
60
61
        // Size of the actual window.
62
        $sizeX += 4;
63
        $sizeY += $titleHeight + 2;
64
65
        // Title bar & title.
66
        $titleLabel = new Label();
67
        $titleLabel->setPosition(3, -$titleHeight / 3 - 1)
68
            ->setAlign(Label::LEFT, Label::CENTER2)
69
            ->setTextId($name)
70
            ->setTextColor($titleTextColor)
71
            ->setTextSize(2)
72
            ->setTranslate(true)
73
            ->setTextFont('RajdhaniMono')
74
            ->setId("TitleText");
75
        $frame->addChild($titleLabel);
76
77
        $titleBar = new Quad();
78
        $titleBar->setSize($sizeX, 0.33)
79
            ->setPosition(0, -$titleHeight)
80
            ->setBackgroundColor('fff');
81
        $frame->addChild($titleBar);
82
83
        $titleBar = new Quad();
84
        $titleBar->setSize($sizeX / 4, 0.5)
85
            ->setPosition(0, -$titleHeight)
86
            ->setBackgroundColor('fff');
87
        $frame->addChild($titleBar);
88
89
        $titleBar = new Quad('Title');
90
        $titleBar->setSize($sizeX - $closeButtonWidth, $titleHeight)
91
            ->setBackgroundColor($titlebarColor)
92
            ->setScriptEvents(true);
93
        $frame->addChild($titleBar);
94
95
        $closeButton = new Label('Close');
96
        $closeButton->setSize($closeButtonWidth, $titleHeight)
97
            ->setPosition($sizeX - $closeButtonWidth + ($closeButtonWidth / 2), -$titleHeight / 2)
98
            ->setAlign(Label::CENTER, Label::CENTER2)
99
            ->setText("✖")
100
            ->setTextColor('fff')
101
            ->setTextSize(2)
102
            ->setTextFont('OswaldMono')
103
            ->setScriptEvents(true)
104
            ->setAreaColor($titlebarColor)
105
            ->setAreaFocusColor('f22');
106
        $frame->addChild($closeButton);
107
108
        //body
109
        $body = new Quad();
110
        $body->setSize($sizeX, $sizeY - $titleHeight)
111
            ->setPosition(0, -$titleHeight)
112
            ->setBackgroundColor("222")
113
            ->setOpacity(0.8);
114
        $frame->addChild($body);
115
116
        $body = new Quad();
117
        $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, eXpansion\Framework\Gui\Components\uiLabel. 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...
118
            ->setPosition(0, -$titleHeight)
119
            ->setStyles('Bgs1', 'BgDialogBlur')
120
            ->setId('WindowBg')
121
            ->setScriptEvents(true);
122
        $frame->addChild($body);
123
124
        $body = new Quad();
125
        $body->setSize($sizeX + 10, $sizeY + 10)
126
            ->setPosition(-5, 5)
127
            ->setStyles('Bgs1InRace', 'BgButtonShadow');
128
        $frame->addChild($body);
129
130
        // Add maniascript for window handling.
131
        $manialink->addChild($this->windowManiaScriptFactory->createScript(['']));
132
133
        return $closeButton;
134
    }
135
136
    /**
137
     * @param ManialinkInterface $manialinkInterface
138
     */
139
    public function setManialinkInterface(ManialinkInterface $manialinkInterface)
140
    {
141
        $this->manialinkInterface = $manialinkInterface;
142
    }
143
}
144