Completed
Pull Request — master (#124)
by
unknown
02:35
created

WindowFactory::ok()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
1
<?php
2
3
namespace eXpansion\Bundle\Acme\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\Window;
8
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory as BaseWindowFactory;
9
use eXpansion\Framework\Gui\Components\uiAnimation;
10
use eXpansion\Framework\Gui\Components\uiButton;
11
use eXpansion\Framework\Gui\Components\uiCheckbox;
12
use eXpansion\Framework\Gui\Components\uiDropdown;
13
use eXpansion\Framework\Gui\Components\uiInput;
14
use eXpansion\Framework\Gui\Components\uiLabel;
15
use eXpansion\Framework\Gui\Components\uiTextbox;
16
use eXpansion\Framework\Gui\Components\uiTooltip;
17
use eXpansion\Framework\Gui\Layouts\layoutLine;
18
use eXpansion\Framework\Gui\Layouts\layoutRow;
19
use eXpansion\Framework\Gui\Layouts\layoutScrollable;
20
use FML\Controls\Label;
21
use FML\Controls\Quad;
22
use FML\Script\ScriptLabel;
23
24
class WindowFactory extends BaseWindowFactory
25
{
26
27
    /**
28
     * @param ManialinkInterface|Widget $manialink
29
     */
30
    protected function createContent(ManialinkInterface $manialink)
31
    {
32
        parent::createContent($manialink);
33
34
        $lbl = $this->uiFactory->createLabel("update me", uiLabel::TYPE_HEADER, "exp2_text");
35
        $manialink->addChild($lbl);
36
37
        $hash = MemoryWidgetFactory::$exp_hash;
38
        // next 2 methods would be generated by script manager
39
        $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...
40
            'declare Text '.$hash.' for LocalUser = "";
41
            declare Text '.$hash.'_check for LocalUser = "";
42
            declare Text '.$hash.'_oldCheck = "";           
43
            '
44
        );
45
        $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...
46
            '
47
            if ('.$hash.'_check != '.$hash.'_oldCheck) {
48
                ---'.$hash.'---               
49
            }'
50
        );
51
52
        // this is what user needs to do for php part...
53
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(
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...
54
            $hash,
55
            '(Page.GetFirstChild("exp2_text") as CMlLabel).Value ='.$hash.';'
56
        );
57
58
59
    }
60
61
}
62