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.

Identical   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 35
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRules() 0 10 2
A getMessages() 0 4 1
A canHandle() 0 4 1
1
<?php
2
/**
3
 * Identical
4
 *
5
 * @category  StrokerForm
6
 * @package   StrokerForm\Renderer
7
 * @copyright 2012 Bram Gerritsen
8
 * @version   SVN: $Id$
9
 */
10
11
namespace StrokerForm\Renderer\JqueryValidate\Rule;
12
13
use Zend\Form\ElementInterface;
14
use Zend\Validator\ValidatorInterface;
15
use Zend\Validator\Identical as IdenticalValidator;
16
17
class Identical extends AbstractRule
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function getRules(ValidatorInterface $validator, ElementInterface $element = null)
23
    {
24
        $token = $validator->getToken();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Validator\ValidatorInterface as the method getToken() does only exist in the following implementations of said interface: Zend\Validator\Identical.

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...
25
26
        if (strpos($element->getName(), "[") !== false) {
27
            $token = preg_replace('#\[[^\]]+\]$#i', "[" . $token . "]", $element->getName());
0 ignored issues
show
Bug introduced by
It seems like $element is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
28
        }
29
30
        return ['equalTo' => '[name="' . $token . '"]'];
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function getMessages(ValidatorInterface $validator)
37
    {
38
        return ['equalTo' => $this->translateMessage('Please enter the same value again.')];
39
    }
40
41
    /**
42
     * Whether this rule supports certain validators
43
     *
44
     * @param ValidatorInterface $validator
45
     * @return mixed
46
     */
47
    public function canHandle(ValidatorInterface $validator)
48
    {
49
        return $validator instanceof IdenticalValidator;
50
    }
51
}
52