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.

Between::getMax()   A
last analyzed

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Between
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\Between as BetweenValidator;
16
17
class Between extends AbstractRule
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function getRules(ValidatorInterface $validator, ElementInterface $element = null)
23
    {
24
        return ['range' => [$this->getMin($validator), $this->getMax($validator)]];
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    public function getMessages(ValidatorInterface $validator)
31
    {
32
        return [
33
            'range' =>
34
                sprintf($this->translateMessage('The input is not between %s and %s'), $this->getMin($validator), $this->getMax($validator))
35
        ];
36
    }
37
38
    /**
39
     * @param  ValidatorInterface $validator
40
     *
41
     * @return mixed
42
     */
43
    protected function getMin(ValidatorInterface $validator)
44
    {
45
        return $validator->getInclusive() ? $validator->getMin() : $validator->getMin() + 1;
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 getInclusive() does only exist in the following implementations of said interface: Zend\Validator\Between, Zend\Validator\GreaterThan, Zend\Validator\LessThan.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Validator\ValidatorInterface as the method getMin() does only exist in the following implementations of said interface: Zend\Validator\Between, Zend\Validator\File\Count, Zend\Validator\File\FilesSize, Zend\Validator\File\Size, Zend\Validator\File\WordCount, Zend\Validator\GreaterThan, Zend\Validator\StringLength.

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
48
    /**
49
     * @param  ValidatorInterface $validator
50
     *
51
     * @return mixed
52
     */
53
    protected function getMax(ValidatorInterface $validator)
54
    {
55
        return $validator->getInclusive() ? $validator->getMax() : $validator->getMax() - 1;
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 getInclusive() does only exist in the following implementations of said interface: Zend\Validator\Between, Zend\Validator\GreaterThan, Zend\Validator\LessThan.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Validator\ValidatorInterface as the method getMax() does only exist in the following implementations of said interface: Zend\Validator\Between, Zend\Validator\File\Count, Zend\Validator\File\FilesSize, Zend\Validator\File\Size, Zend\Validator\File\WordCount, Zend\Validator\LessThan, Zend\Validator\StringLength.

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...
56
    }
57
58
    /**
59
     * Whether this rule supports certain validators
60
     *
61
     * @param ValidatorInterface $validator
62
     * @return mixed
63
     */
64
    public function canHandle(ValidatorInterface $validator)
65
    {
66
        return $validator instanceof BetweenValidator;
67
    }
68
}
69