Tooltip   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 5
dl 0
loc 163
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addTooltip() 0 17 3
A prepare() 0 4 1
B getFunctions() 0 78 1
A getScriptFeatures() 0 4 1
A render() 0 27 1
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Control;
6
use FML\Controls\Frame;
7
use FML\Controls\Label;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eXpansion\Framework\Gui\Components\Label.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use FML\Controls\Quad;
9
use FML\Script\Features\ScriptFeature;
10
use FML\Script\Script;
11
use FML\Types\ScriptFeatureable;
12
13
class Tooltip extends AbstractUiElement implements ScriptFeatureable
14
{
15
    protected $element;
16
17
    /**
18
     * @param AbstractUiElement|Control $control
19
     * @param string                    $text
20
     */
21
    public function addTooltip($control, $text)
22
    {
23
        if ($control instanceof Control) {
24
            $control->addDataAttribute("tooltip", $text);
25
            $control->addClass("uiTooltip");
26
            $control->setScriptEvents(true);
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\Builders\WidgetLabel, eXpansion\Framework\Gui\Components\Label. 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...
27
28
            return;
29
        }
30
31
        if ($control instanceof AbstractUiElement) {
32
            $control->addDataAttribute("tooltip", $text);
33
            $control->addClass("uiTooltip");
34
35
            return;
36
        }
37
    }
38
39
    /**
40
     * Prepare the given Script for rendering by adding the needed Labels, etc.
41
     *
42
     * @param Script $script Script to prepare
43
     * @return void
44
     *
45
     */
46
    public function prepare(Script $script)
47
    {
48
        $script->addScriptFunction("exp_tooltipFunctions", $this->getFunctions());
49
    }
50
51
52
    public function getFunctions()
53
    {
54
55
        return /** @lang textmate */
56
            <<<EOL
57
            
58
       ***FML_OnInit***
59
       ***
60
       declare CMlFrame exp_tooltip = (Page.GetFirstChild("exp_tooltip") as CMlFrame);	
61
	   declare Boolean exp_tooltip_move = False;
62
	   declare Boolean exp_tooltip_toggle = True;
63
	   declare Integer exp_tooltip_delay = 0;
64
	   declare Vec2 mouse_pos = <0., 0.>;
65
	   declare Vec2 exp_tooltip_rel = <0., 0.>;     
66
       ***
67
                    
68
       ***FML_Loop***
69
       ***
70
       if (exp_tooltip_move) {         
71
            exp_tooltip.RelativePosition_V3 =  <MouseX, MouseY> - mouse_pos + exp_tooltip_rel;
72
                             
73
          /*  if (exp_tooltip_rel.Y > -10.) {
74
                exp_tooltip.RelativePosition_V3 = exp_tooltip_rel + <4., 0.>;
75
            } else { 
76
               exp_tooltip.RelativePosition_V3 = exp_tooltip_rel + <4., 4.>;
77
            } */
78
            
79
            if (exp_tooltip_delay + 350 < Now) {
80
                if (exp_tooltip_toggle) {
81
                    AnimMgr.Add(exp_tooltip.Controls[0], "<elem scale=\"1\" />",  450, CAnimManager::EAnimManagerEasing::ElasticOut);
82
                    AnimMgr.Add(exp_tooltip.Controls[1], "<elem scale=\"1\" />",  450, CAnimManager::EAnimManagerEasing::ElasticOut);
83
                    exp_tooltip_toggle = False;
84
                }          	    					
85
            }								
86
	   }
87
	   
88
	   if (MouseLeftButton) {
89
	            (exp_tooltip.Controls[0] as CMlLabel).RelativeScale = 0.;
90
                (exp_tooltip.Controls[1] as CMlQuad).RelativeScale = 0.;       
91
	   }
92
       ***
93
       
94
       ***FML_MouseOver***      
95
       ***
96
       if (Event.Control != Null) {
97
			if (Event.Control.HasClass("uiTooltip") )  {
98
                declare tooltipLabel = (exp_tooltip.Controls[0] as CMlLabel);
99
                declare text = Event.Control.DataAttributeGet("tooltip");
100
                declare sizeX = tooltipLabel.ComputeWidth(text);			 			    						       
101
                tooltipLabel.Value = text;
102
                tooltipLabel.Size.X = sizeX;    
103
                (exp_tooltip.Controls[1] as CMlQuad).Size.X = sizeX;                            
104
                exp_tooltip_move = True;
105
                exp_tooltip_delay = Now;
106
                exp_tooltip_toggle = True;
107
                mouse_pos = <MouseX, MouseY>;
108
              //  exp_tooltip_rel = Event.Control.AbsolutePosition_V3 + Exp_Window.RelativePosition_V3;
109
                exp_tooltip_rel = Event.Control.AbsolutePosition_V3 - Exp_Window.RelativePosition_V3;                                                  
110
            }
111
        }
112
       ***
113
       
114
       ***FML_MouseOut***      
115
       ***
116
       if (Event.Control != Null) {
117
			if (Event.Control.HasClass("uiTooltip") )  {
118
                exp_tooltip_move = False;
119
                exp_tooltip_delay = 0;  
120
                exp_tooltip_toggle = True;                         
121
                (exp_tooltip.Controls[0] as CMlLabel).RelativeScale = 0.;
122
                (exp_tooltip.Controls[1] as CMlQuad).RelativeScale = 0.;          	                                	
123
            }    
124
       }
125
       ***
126
                      
127
EOL;
128
129
    }
130
131
132
    /**
133
     * Get the Script Features
134
     *
135
     * @return ScriptFeature[]
136
     */
137
    public function getScriptFeatures()
138
    {
139
        return ScriptFeature::collect($this);
140
    }
141
142
    /**
143
     * Render the XML element
144
     *
145
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
146
     * @return \DOMElement
147
     */
148
    public function render(\DOMDocument $domDocument)
149
    {
150
        $frame = new Frame("exp_tooltip");
151
        $frame->setZ(100)->setAlign("left", "center");
152
153
        $label = new Label();
154
        $label->setSize(36, 5)
155
            ->setAlign("left", "center2")
156
            ->setTextFont('file://Media/Font/BiryaniDemiBold.Font.gbx')
157
            ->setTextSize(2)
158
            ->setTextColor("eee")
159
            ->setOpacity(1)
160
            ->setAreaFocusColor("0000")
161
            ->setAreaColor("0000")
162
            ->setScriptEvents(true)
163
            ->setScale(0);
164
165
        $quad = new Quad();
166
        $quad->setAlign("left", "center2")->setScale(0);
167
        $quad->setSize(36, 5)->setBackgroundColor('000')->setOpacity(1)->setScriptEvents(true);
168
169
170
        $frame->addChild($label);
171
        $frame->addChild($quad);
172
173
        return $frame->render($domDocument);
174
    }
175
}
176