Completed
Push — develop ( b21f1c...9f4e1d )
by
unknown
07:38
created

LanguageSwitcher::setPartial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2017 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Core view helpers */
11
namespace Core\View\Helper;
12
13
use Zend\View\Helper\AbstractHelper;
14
15
/**
16
 * Renders the Language Switcher
17
 *
18
 * <code>
19
 *
20
 *      // Renders the language switcher
21
 *      echo $this->languageSwitcher($lang);
22
 *
23
 * </code>
24
 *
25
 * @author Carsten Bleek <[email protected]>
26
 */
27
class LanguageSwitcher extends AbstractHelper
28
{
29
30
    /**
31
    * @var string
32
    */
33
    protected $partial = 'partial/language-switcher';
34
35
    /**
36
     * generates a select2 form with enabled languages
37
     *
38
     * @return string
39
     */
40
    public function __invoke($options = [])
41
    {
42
43
        $options = array_merge([
44
                                   'partial' => $this->partial,
45
                               ], $options);
46
47
        $view = $this->view;
48
        $partial = $options['partial'];
49
50
51
        $variables=[];
52
53
        return $view->partial($partial, $variables);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method partial() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getPartial()
60
    {
61
        return $this->partial;
62
    }
63
64
    /**
65
     * @param string $partial
66
     * @return LanguageSwitcher
67
     */
68
    public function setPartial($partial)
69
    {
70
        $this->partial = $partial;
71
72
        return $this;
73
    }
74
}
75