Completed
Pull Request — master (#139)
by De Cramer
02:36
created

BestCheckpointsWidgetFactory::createColumnBox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 0
cts 15
cp 0
rs 9.2
cc 1
eloc 15
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\WidgetBestCheckpoints\Plugins\Gui;
4
5
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
6
use eXpansion\Framework\Core\Model\Gui\Widget;
7
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
8
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
9
use eXpansion\Framework\Gui\Builders\WidgetBackground;
10
use eXpansion\Framework\Gui\Ui\Factory;
11
use FML\Controls\Frame;
12
use FML\Script\ScriptInclude;
13
use FML\Script\ScriptLabel;
14
15
class BestCheckpointsWidgetFactory extends WidgetFactory
16
{
17
    const rowCount = 3;
18
    const columnCount = 6;
19
20
    /***
21
     * MenuFactory constructor.
22
     *
23
     * @param $name
24
     * @param $sizeX
25
     * @param $sizeY
26
     * @param null $posX
27
     * @param null $posY
28
     * @param WidgetFactoryContext $context
29
     * @param Factory $uiFactory
30
     */
31
    public function __construct(
32
        $name,
33
        $sizeX,
34
        $sizeY,
35
        $posX,
36
        $posY,
37
        WidgetFactoryContext $context,
38
        Factory $uiFactory
39
    ) {
40
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
41
42
        $this->uiFactory = $uiFactory;
43
    }
44
45
    /**
46
     * @param Widget|ManialinkInterface $manialink
47
     */
48
    protected function createContent(ManialinkInterface $manialink)
49
    {
50
        $elementCount = 0;
51
        $rows = $this->uiFactory->createLayoutRow(0, 0, [], -1);
52
53
        for ($i = 0; $i < self::rowCount; $i++) {
54
            $elements = [];
55
            for ($c = 0; $c < self::columnCount; $c++) {
56
                $elements[] = $this->createColumnBox($elementCount);
57
                $elementCount++;
58
            }
59
            $line = $this->uiFactory->createLayoutLine(0, 0, $elements, 1);
60
61
            $rows->addChild($line);
62
        }
63
64
        $manialink->getFmlManialink()->getScript()->setScriptInclude(ScriptInclude::TextLib);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getFmlManialink() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65
        $manialink->getFmlManialink()->getScript()->addScriptFunction("",
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getFmlManialink() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
66
            <<<EOL
67
            
68
            Void UpdateCp(Integer _Index, Integer _Score, Text _Nickname, Boolean _Animate) {
69
                declare CMlLabel Label <=> (Page.GetFirstChild("Cp_"^ _Index) as CMlLabel);
70
                                
71
                if (_Score == -1) {
72
                    Label.Value = "\$fff\$o" ^ (_Index+1);
73
                } else {
74
                    Label.Value = "\$fff\$o" ^ (_Index+1) ^ " \$o\$ff3" ^ TextLib::TimeToText(_Score, True) ^" \$fff "^ TextLib::StripFormatting(_Nickname); 
75
                }
76
                if (_Animate) {                                
77
                    AnimMgr.Add (Label, "<elem scale=\"1.5\" />", 350, CAnimManager::EAnimManagerEasing::ElasticOut);
78
                    AnimMgr.AddChain (Label, "<elem scale=\"1.0\" />", 350, CAnimManager::EAnimManagerEasing::ElasticIn);
79
                }          
80
            }
81
            
82
             Void HideCp(Integer _Index) {
83
                (Page.GetFirstChild("Cp_"^ _Index) as CMlLabel).Hide();
84
                (Page.GetFirstChild("Bg_"^ _Index) as CMlFrame).Hide();               
85
             }   
86
EOL
87
88
        );
89
90
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit,
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getFmlManialink() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
91
            <<<EOL
92
            declare Integer ElementCount for Page = $elementCount;
93
            declare Integer[Integer] BestCheckpoints for Page = Integer[Integer];                          
94
            
95
            // clear
96
            for (i,0, (ElementCount-1)) {
97
                BestCheckpoints[i] = 99999999;           
98
                UpdateCp(i, -1, "", False);                
99
            }                             
100
                           
101
            // hide checkpoints not needed
102
            for (i, (MapCheckpointPos.count+1), (ElementCount-1)) {
103
                HideCp(i);
104
            }    
105
EOL
106
        );
107
108
109
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getFmlManialink() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
110
            <<<EOL
111
            foreach (RaceEvent in RaceEvents) {
112
                if (RaceEvent.Type == CTmRaceClientEvent::EType::WayPoint) {
113
                    if (RaceEvent.CheckpointInLap < ElementCount) {                    
114
                        if (RaceEvent.LapTime < BestCheckpoints[RaceEvent.CheckpointInLap]) {
115
                            BestCheckpoints[RaceEvent.CheckpointInLap] = RaceEvent.LapTime;
116
                            UpdateCp(RaceEvent.CheckpointInLap, RaceEvent.LapTime, RaceEvent.Player.User.Name, True);
117
                        }                     
118
                    }
119
                }
120
            }
121
EOL
122
        );
123
        $manialink->addChild($rows);
124
125
126
    }
127
128
    /**
129
     * @param int $index
130
     * @return Frame
131
     */
132
    private function createColumnBox($index)
133
    {
134
        $width = 40;
135
        $height = 5;
136
137
        $frame = Frame::create();
138
139
        $label = $this->uiFactory->createLabel();
140
        $label->setAlign("left", "center");
141
        $label->setTextSize(1)->setPosition(1, -($height / 2));
142
        $label->setSize($width, $height)
143
            ->setId("Cp_".$index);
144
        $frame->addChild($label);
145
146
        $background = new WidgetBackground($width, $height);
147
        $background->setId("Bg_".$index);
148
        $frame->addChild($background);
149
150
        $frame->setSize($width, $height);
151
152
        return $frame;
153
    }
154
155
    protected function updateContent(ManialinkInterface $manialink)
156
    {
157
        parent::updateContent($manialink); // TODO: Change the autogenerated stub
158
    }
159
160
161
}
162