Completed
Pull Request — master (#130)
by De Cramer
07:55
created

WindowFrameFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace eXpansion\Bundle\ImmersiveWindows\Model\Gui\Factory;
4
5
use eXpansion\Bundle\Menu\DataProviders\MenuItemProvider;
6
use eXpansion\Bundle\Menu\Gui\MenuTabsFactory;
7
use eXpansion\Framework\Core\Model\Gui\Factory\WindowFrameFactory as OriginalWindowFrameFactory;
8
use FML\Controls\Frame;
9
use FML\Controls\Quad;
10
use FML\ManiaLink;
11
12
/**
13
 * Class WindowFrameFactory
14
 *
15
 * @author    de Cramer Oliver<[email protected]>
16
 * @copyright 2017 Smile
17
 * @package eXpansion\Bundle\ImmersiveWindows\Model\Gui\Factory
18
 */
19
class WindowFrameFactory extends OriginalWindowFrameFactory
20
{
21
    /** @var MenuTabsFactory */
22
    protected $menuTabsFactory;
23
24
    /** @var MenuItemProvider */
25
    protected $menuItemProvider;
26
27
    /**
28
     * WindowFrameFactory constructor.
29
     *
30
     * @param MenuTabsFactory $menuTabsFactory
31
     * @param MenuItemProvider $menuItemProvider
32
     */
33
    public function __construct(MenuTabsFactory $menuTabsFactory, MenuItemProvider $menuItemProvider)
34
    {
35
        $this->menuTabsFactory = $menuTabsFactory;
36
        $this->menuItemProvider = $menuItemProvider;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function build(ManiaLink $manialink, Frame $mainFrame, $name, $sizeX, $sizeY)
43
    {
44
        // Creating sub frame to keep all the pieces together. Position needs to be top left corner.
45
        $frame = new Frame();
46
        $frame->setPosition(-144 - $mainFrame->getX(), 82 - $mainFrame->getY());
47
        $mainFrame->addChild($frame);
48
49
        // Creating the tabs.
50
        $mainFrame->addChild(
51
            $this->menuTabsFactory->createTabsMenu(
0 ignored issues
show
Documentation introduced by
$this->menuTabsFactory->...tItem(), 'admin/admin') is of type object<FML\Types\Container>, but the function expects a object<FML\Types\Renderable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
                $manialink,
0 ignored issues
show
Documentation introduced by
$manialink is of type object<FML\ManiaLink>, but the function expects a object<eXpansion\Framewo...Gui\ManialinkInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
                $frame,
54
                $this->menuItemProvider->getRootItem(),
0 ignored issues
show
Documentation introduced by
$this->menuItemProvider->getRootItem() is of type object<eXpansion\Bundle\...enu\ItemInterface>|null, but the function expects a object<eXpansion\Bundle\...\Model\Menu\ParentItem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
                'admin/admin'
56
            )
57
        );
58
59
        $closeButton = new Label('Close');
60
        $closeButton->setSize(6, 6)
61
            ->setPosition(132, 72)
62
            ->setAlign(Label::CENTER, Label::CENTER2)
63
            ->setText("✖")
64
            ->setTextColor('fff')
65
            ->setTextSize(2)
66
            ->setTextFont('OswaldMono')
67
            ->setScriptEvents(true)
68
            ->setAreaColor("FFF")
69
            ->setAreaFocusColor('f22');
70
        $frame->addChild($closeButton);
71
72
        /*
73
         * Adding background frame
74
         */
75
        $bgFrame = Frame::create("background");
76
77
        $quad = new Quad();
78
        $quad->addClass("bg")
79
            ->setId("mainBg")
80
            ->setPosition(0, 0)
81
            ->setSize(322, 182);
82
        $quad->setAlign("center", "center")
83
            ->setStyles("Bgs1", "BgDialogBlur");
84
        $bgFrame->addChild($quad);
85
86
        $frame->addChild($bgFrame);
87
88
        return $closeButton;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $closeButton; (eXpansion\Bundle\Immersi...Model\Gui\Factory\Label) is incompatible with the return type of the parent method eXpansion\Framework\Core...ndowFrameFactory::build of type FML\Controls\Label.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
    }
90
91
}