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:46
created

AnnotationDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
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
use ReflectionProperty;
20
21
/**
22
 * Annotation driver which parses the annotations of the user model instance.
23
 */
24
final class AnnotationDriver implements ModelConfigurationDriverInterface
25
{
26
    /**
27
     * @var Reader
28
     */
29
    private $reader;
30
31
    /**
32
     * @var string
33
     */
34
    private $userClass;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param Reader $annotationReader
40
     * @param string $userClass
41
     */
42
    public function __construct(Reader $annotationReader, $userClass)
43
    {
44
        $this->reader = $annotationReader;
45
        $this->userClass = (string) $userClass;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @throws \LogicException If one of the annotations is missing
52
     * @throws \LogicException If one property has multiple "auth" annotations
53
     */
54
    public function getMetadataForUser()
55
    {
56
        $reflection = new ReflectionClass($this->userClass);
57
        $properties = $reflection->getProperties();
58
        $loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null;
59
60
        foreach ($properties as $reflectionProperty) {
61
            foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) {
62
                $class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation));
63
                $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...
64
65
                if ($annotationObject) {
66
                    switch ($annotation) {
67
                        case 'login':
68
                            if (!empty($loginProperty)) {
69
                                throw $this->createDuplicateAnnotationException();
70
                            }
71
72
                            $loginProperty = $reflectionProperty;
73
                            break;
74
                        case 'password':
75
                            if (!empty($passwordProperty)) {
76
                                throw $this->createDuplicateAnnotationException();
77
                            }
78
79
                            $passwordProperty = $reflectionProperty;
80
                            break;
81
                        case 'apiKey':
82
                            if (!empty($apiKeyProperty)) {
83
                                throw $this->createDuplicateAnnotationException();
84
                            }
85
86
                            $apiKeyProperty = $reflectionProperty;
87
                            break;
88
                        case 'lastAction':
89
                            if (!empty($lastActionProperty)) {
90
                                throw $this->createDuplicateAnnotationException();
91
                            }
92
93
                            $lastActionProperty = $reflectionProperty;
94
                    }
95
96
                    if ($loginProperty
97
                        && $passwordProperty
98
                        && $apiKeyProperty
99
                        && $lastActionProperty
100
                    ) {
101
                        break;
102
                    }
103
104
                    continue;
105
                }
106
            }
107
        }
108
109
        if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) {
110
            throw new \LogicException(sprintf(
111
                'A user class must have a "%s", "%s", "%s" annotation!',
112
                'Login',
113
                'Password',
114
                'ApiKey'
115
            ));
116
        }
117
118
        return new ClassMetadata(
119
            $reflection,
120
            $this->userClass,
121
            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...
122
                ClassMetadata::LOGIN_PROPERTY       => $loginProperty,
123
                ClassMetadata::PASSWORD_PROPERTY    => $passwordProperty,
124
                ClassMetadata::API_KEY_PROPERTY     => $apiKeyProperty,
125
                ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty,
126
            )
127
        );
128
    }
129
130
    /**
131
     * Creates the exception when
132
     *
133
     * @return \InvalidArgumentException
134
     */
135
    private function createDuplicateAnnotationException()
136
    {
137
        return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!');
138
    }
139
}
140