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

AnnotationDriver::getMetadataForUser()   C

Complexity

Conditions 19
Paths 30

Size

Total Lines 75
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 26.4668

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 75
ccs 37
cts 51
cp 0.7255
rs 5.2753
cc 19
eloc 49
nc 30
nop 0
crap 26.4668

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 5
    public function __construct(Reader $annotationReader, $userClass)
42
    {
43 5
        $this->reader = $annotationReader;
44 5
        $this->userClass = (string) $userClass;
45 5
    }
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 5
    public function getMetadataForUser()
54
    {
55 5
        $reflection = new ReflectionClass($this->userClass);
56 5
        $properties = $reflection->getProperties();
57 5
        $loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null;
58
59 5
        foreach ($properties as $reflectionProperty) {
60 5
            foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) {
61 5
                $class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation));
62 5
                $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 5
                if ($annotationObject) {
65
                    switch ($annotation) {
66 5
                        case 'login':
67 5
                            if (!empty($loginProperty)) {
68
                                throw $this->createDuplicateAnnotationException();
69
                            }
70
71 5
                            $loginProperty = $reflectionProperty;
72 5
                            break;
73 5
                        case 'password':
74 5
                            if (!empty($passwordProperty)) {
75
                                throw $this->createDuplicateAnnotationException();
76
                            }
77
78 5
                            $passwordProperty = $reflectionProperty;
79 5
                            break;
80 5
                        case 'apiKey':
81 5
                            if (!empty($apiKeyProperty)) {
82
                                throw $this->createDuplicateAnnotationException();
83
                            }
84
85 5
                            $apiKeyProperty = $reflectionProperty;
86 5
                            break;
87
                        case 'lastAction':
88
                            if (!empty($lastActionProperty)) {
89
                                throw $this->createDuplicateAnnotationException();
90
                            }
91
92
                            $lastActionProperty = $reflectionProperty;
93
                    }
94
95
                    if ($loginProperty
96 5
                        && $passwordProperty
97 5
                        && $apiKeyProperty
98 5
                        && $lastActionProperty
99 5
                    ) {
100
                        break;
101
                    }
102
103 5
                    continue;
104
                }
105 5
            }
106 5
        }
107
108 5
        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 5
        return new ClassMetadata(
118 5
            $reflection,
119 5
            $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 5
                ClassMetadata::LOGIN_PROPERTY       => $loginProperty,
122 5
                ClassMetadata::PASSWORD_PROPERTY    => $passwordProperty,
123 5
                ClassMetadata::API_KEY_PROPERTY     => $apiKeyProperty,
124 5
                ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty,
125
            )
126 5
        );
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