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

WindowHelpFactory::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 28
cp 0
rs 8.8571
cc 1
eloc 24
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins\Gui;
4
5
use eXpansion\Framework\Core\DataProviders\ChatCommandDataProvider;
6
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
7
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
9
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
10
use eXpansion\Framework\Core\Services\ChatCommands;
11
use FML\Controls\Frame;
12
use FML\Controls\Label;
13
14
15
/**
16
 * Class HelpFactory
17
 *
18
 * @package eXpansion\Framework\AdminGroups\Plugins\Window;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class WindowHelpFactory extends WindowFactory
22
{
23
    /** @var GridBuilderFactory */
24
    protected $gridBuilderFactory;
25
26
    /** @var DataCollectionFactory */
27
    protected $dataCollectionFactory;
28
29
    /** @var ChatCommands */
30
    protected $chatCommands;
31
32
    /** @var ChatCommandDataProvider */
33
    protected $chatCommandDataPovider;
34
35
    /**
36
     * @param GridBuilderFactory $gridBuilderFactory
37
     */
38
    public function setGridBuilderFactory($gridBuilderFactory)
39
    {
40
        $this->gridBuilderFactory = $gridBuilderFactory;
41
    }
42
43
    /**
44
     * @param DataCollectionFactory $dataCollectionFactory
45
     */
46
    public function setDataCollectionFactory($dataCollectionFactory)
47
    {
48
        $this->dataCollectionFactory = $dataCollectionFactory;
49
    }
50
51
    /**
52
     * @param ChatCommands $chatCommands
53
     */
54
    public function setChatCommands($chatCommands)
55
    {
56
        $this->chatCommands = $chatCommands;
57
    }
58
59
    public function setChatCommandDataProvide($chatCommandDataPovider)
60
    {
61
        $this->chatCommandDataPovider = $chatCommandDataPovider;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function createContent(ManialinkInterface $manialink)
68
    {
69
        $collection = $this->dataCollectionFactory->create($this->chatCommands->getChatCommands());
70
        $collection->setPageSize(2);
71
72
        $helpButton = new Label();
73
        $helpButton->setText('')
74
            ->setSize(6, 6)
75
            ->setAreaColor("0000")
76
            ->setAreaFocusColor("0000");
77
78
        $gridBuilder = $this->gridBuilderFactory->create();
79
        $gridBuilder->setManialink($manialink)
80
            ->setDataCollection($collection)
81
            ->setManialinkFactory($this)
82
            ->addTextColumn(
83
                'command',
84
                "expansion_core.windows.chat_commands.column_command",
85
                25
86
            )
87
            ->addTextColumn(
88
                'description',
89
                'expansion_core.windows.chat_commands.column_description',
90
                70,
91
                false,
92
                true
93
            )
94
            ->addActionColumn('help', '', 5, array($this, 'callbackHelp'), $helpButton);
0 ignored issues
show
Documentation introduced by
$helpButton is of type object<FML\Controls\Label>, but the function expects a object<eXpansion\Framewo...e\Model\Gui\Grid\Label>.

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...
95
96
        $manialink->setData('grid', $gridBuilder);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    protected function updateContent(ManialinkInterface $manialink)
103
    {
104
        /** @var Frame $contentFrame */
105
        $contentFrame = $manialink->getContentFrame();
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 getContentFrame() 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...
106
        $contentFrame->removeAllChildren();
107
108
        $collection = $this->dataCollectionFactory->create($this->chatCommands->getChatCommands());
109
        $collection->setPageSize(20);
110
111
        /** @var GridBuilder $gridBuilder */
112
        $gridBuilder = $manialink->getData('grid');
113
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
114
    }
115
116
    /**
117
     * Callbacked called when help button is pressed.
118
     *
119
     * @param $login
120
     * @param $params
121
     * @param $arguments
122
     */
123
    public function callbackHelp($login, $params, $arguments)
124
    {
125
        $this->chatCommandDataPovider->onPlayerChat(0, $login, '/'.$arguments['command'].' -h', true);
126
    }
127
}
128