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.
Completed
Pull Request — master (#31)
by Maximilian
02:09
created

createDuplicateAnnotationException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sententiaregum project.
5
 *
6
 * (c) Maximilian Bosch <[email protected]>
7
 * (c) Ben Bieler <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Ma27\ApiKeyAuthenticationBundle\Annotation\Driver;
14
15
use Doctrine\Common\Annotations\Reader;
16
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata;
17
use Ma27\ApiKeyAuthenticationBundle\Model\User\ModelConfigurationDriverInterface;
18
use ReflectionClass;
19
20
/**
21
 * Annotation driver which parses the annotations of the user model instance.
22
 */
23
final class AnnotationDriver implements ModelConfigurationDriverInterface
24
{
25
    /**
26
     * @var Reader
27
     */
28
    private $reader;
29
30
    /**
31
     * @var string
32
     */
33
    private $userClass;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param Reader $annotationReader
39
     * @param string $userClass
40
     */
41
    public function __construct(Reader $annotationReader, $userClass)
42
    {
43
        $this->reader = $annotationReader;
44
        $this->userClass = (string) $userClass;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @throws \LogicException If one of the annotations is missing
51
     * @throws \LogicException If one property has multiple "auth" annotations
52
     */
53
    public function getMetadataForUser()
54
    {
55
        $reflection = new ReflectionClass($this->userClass);
56
        $properties = $reflection->getProperties();
57
        $loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null;
58
59
        foreach ($properties as $reflectionProperty) {
60
            foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) {
61
                $class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation));
62
                $annotationObject = $this->reader->getPropertyAnnotation($reflectionProperty, $class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $annotationObject is correct as $this->reader->getProper...ectionProperty, $class) (which targets Doctrine\Common\Annotati...getPropertyAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
64
                if ($annotationObject) {
65
                    switch ($annotation) {
66
                        case 'login':
67
                            if (!empty($loginProperty)) {
68
                                throw $this->createDuplicateAnnotationException();
69
                            }
70
71
                            $loginProperty = $reflectionProperty;
72
                            break;
73
                        case 'password':
74
                            if (!empty($passwordProperty)) {
75
                                throw $this->createDuplicateAnnotationException();
76
                            }
77
78
                            $passwordProperty = $reflectionProperty;
79
                            break;
80
                        case 'apiKey':
81
                            if (!empty($apiKeyProperty)) {
82
                                throw $this->createDuplicateAnnotationException();
83
                            }
84
85
                            $apiKeyProperty = $reflectionProperty;
86
                            break;
87
                        case 'lastAction':
88
                            if (!empty($lastActionProperty)) {
89
                                throw $this->createDuplicateAnnotationException();
90
                            }
91
92
                            $lastActionProperty = $reflectionProperty;
93
                    }
94
95
                    if ($loginProperty
96
                        && $passwordProperty
97
                        && $apiKeyProperty
98
                        && $lastActionProperty
99
                    ) {
100
                        break;
101
                    }
102
103
                    continue;
104
                }
105
            }
106
        }
107
108
        if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) {
109
            throw new \LogicException(sprintf(
110
                'A user class must have a "%s", "%s", "%s" annotation!',
111
                'Login',
112
                'Password',
113
                'ApiKey'
114
            ));
115
        }
116
117
        return new ClassMetadata(
118
            $reflection,
119
            $this->userClass,
120
            array(
0 ignored issues
show
Documentation introduced by
array(\Ma27\ApiKeyAuthen...=> $lastActionProperty) is of type array<string|integer,obj...flectionProperty>|null>, but the function expects a array<integer,object<ReflectionProperty>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
                ClassMetadata::LOGIN_PROPERTY       => $loginProperty,
122
                ClassMetadata::PASSWORD_PROPERTY    => $passwordProperty,
123
                ClassMetadata::API_KEY_PROPERTY     => $apiKeyProperty,
124
                ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty,
125
            )
126
        );
127
    }
128
129
    /**
130
     * Creates the exception when
131
     *
132
     * @return \InvalidArgumentException
133
     */
134
    private function createDuplicateAnnotationException()
135
    {
136
        return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!');
137
    }
138
}
139