GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AbstractRenderer::setFormManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 2
nc 1
nop 1
1
<?php
2
/**
3
 * Abstract form renderer
4
 *
5
 * @category  StrokerForm
6
 * @package   StrokerForm\Renderer
7
 * @copyright 2012 Bram Gerritsen
8
 * @version   SVN: $Id$
9
 */
10
11
namespace StrokerForm\Renderer;
12
13
use StrokerForm\FormManager;
14
use Zend\I18n\Translator\TranslatorAwareInterface;
15
use Zend\I18n\Translator\TranslatorAwareTrait;
16
use Zend\Mvc\Router\RouteInterface;
17
use Zend\Stdlib\AbstractOptions;
18
19
abstract class AbstractRenderer implements RendererInterface, TranslatorAwareInterface
20
{
21
    use TranslatorAwareTrait;
22
23
    /**
24
     * @var \Zend\Mvc\Router\RouteInterface
25
     */
26
    protected $httpRouter;
27
28
    /**
29
     * @var AbstractOptions
30
     */
31
    protected $defaultOptions = [];
32
33
    /**
34
     * @var Options
35
     */
36
    protected $options = null;
37
38
    /**
39
     * @var FormManager
40
     */
41
    protected $formManager;
42
43
    /**
44
     * @return RouteInterface
45
     */
46
    public function getHttpRouter()
47
    {
48
        return $this->httpRouter;
49
    }
50
51
    /**
52
     * @param RouteInterface $httpRouter
53
     *
54
     * @return void
55
     */
56
    public function setHttpRouter(RouteInterface $httpRouter)
57
    {
58
        $this->httpRouter = $httpRouter;
59
    }
60
61
    /**
62
     * @return AbstractOptions
63
     */
64
    public function getOptions()
65
    {
66
        return $this->options;
67
    }
68
69
    /**
70
     * @param array $options
71
     */
72
    public function setOptions(array $options = [])
73
    {
74
        if ($this->options === null) {
75
            $this->options = clone $this->defaultOptions;
0 ignored issues
show
Documentation Bug introduced by
clone $this->defaultOptions is of type object<Zend\Stdlib\AbstractOptions>, but the property $options was declared to be of type object<StrokerForm\Renderer\Options>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
76
        }
77
78
        foreach ($options as $key => $value) {
79
            if (isset($this->options->{$key}) && is_array($this->options->{$key})) {
80
                $options[$key] = $this->options->mergeRecursive($this->options->{$key}, $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Zend\Stdlib\AbstractOptions as the method mergeRecursive() does only exist in the following sub-classes of Zend\Stdlib\AbstractOptions: StrokerForm\Renderer\JqueryValidate\Options, StrokerForm\Renderer\Options. 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...
81
            }
82
        }
83
84
        $this->options->setFromArray($options);
85
    }
86
87
    /**
88
     * @param AbstractOptions $options
89
     */
90
    public function setDefaultOptions(AbstractOptions $options = null)
91
    {
92
        $this->defaultOptions = $options;
93
    }
94
95
    /**
96
     * @return FormManager
97
     */
98
    public function getFormManager()
99
    {
100
        return $this->formManager;
101
    }
102
103
    /**
104
     * @param FormManager $formManager
105
     */
106
    public function setFormManager(FormManager $formManager)
107
    {
108
        $this->formManager = $formManager;
109
    }
110
}
111