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
Push — master ( 96f568...9426da )
by Maximilian
09:12
created

AnnotationDriver::getMetadataForUser()   C

Complexity

Conditions 19
Paths 30

Size

Total Lines 75
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 19.1739

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 75
ccs 47
cts 51
cp 0.9216
rs 5.2752
cc 19
eloc 49
nc 30
nop 0
crap 19.1739

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
namespace Ma27\ApiKeyAuthenticationBundle\Annotation\Driver;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata;
7
use Ma27\ApiKeyAuthenticationBundle\Model\User\ModelConfigurationDriverInterface;
8
use ReflectionClass;
9
10
/**
11
 * Annotation driver which parses the annotations of the user model instance.
12
 *
13
 * @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases
14
 */
15
final class AnnotationDriver implements ModelConfigurationDriverInterface
16
{
17
    /**
18
     * @var Reader
19
     */
20
    private $reader;
21
22
    /**
23
     * @var string
24
     */
25
    private $userClass;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param Reader $annotationReader
31
     * @param string $userClass
32
     */
33 10
    public function __construct(Reader $annotationReader, $userClass)
34
    {
35 10
        $this->reader = $annotationReader;
36 10
        $this->userClass = (string) $userClass;
37 10
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @throws \LogicException If one of the annotations is missing
43
     * @throws \LogicException If one property has multiple "auth" annotations
44
     */
45 10
    public function getMetadataForUser()
46
    {
47 10
        $reflection = new ReflectionClass($this->userClass);
48 10
        $properties = $reflection->getProperties();
49 10
        $loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null;
50
51 10
        foreach ($properties as $reflectionProperty) {
52 10
            foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) {
53 10
                $class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation));
54 10
                $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...
55
56 10
                if ($annotationObject) {
57
                    switch ($annotation) {
58 10
                        case 'login':
59 9
                            if (!empty($loginProperty)) {
60
                                throw $this->createDuplicateAnnotationException();
61
                            }
62
63 9
                            $loginProperty = $reflectionProperty;
64 9
                            break;
65 10
                        case 'password':
66 9
                            if (!empty($passwordProperty)) {
67
                                throw $this->createDuplicateAnnotationException();
68
                            }
69
70 9
                            $passwordProperty = $reflectionProperty;
71 9
                            break;
72 10
                        case 'apiKey':
73 10
                            if (!empty($apiKeyProperty)) {
74
                                throw $this->createDuplicateAnnotationException();
75
                            }
76
77 10
                            $apiKeyProperty = $reflectionProperty;
78 10
                            break;
79 9
                        case 'lastAction':
80 9
                            if (!empty($lastActionProperty)) {
81
                                throw $this->createDuplicateAnnotationException();
82
                            }
83
84 9
                            $lastActionProperty = $reflectionProperty;
85 9
                    }
86
87
                    if ($loginProperty
88 10
                        && $passwordProperty
89 10
                        && $apiKeyProperty
90 10
                        && $lastActionProperty
91 10
                    ) {
92 9
                        break;
93
                    }
94
95 10
                    continue;
96
                }
97 10
            }
98 10
        }
99
100 10
        if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) {
101 1
            throw new \LogicException(sprintf(
102 1
                'A user class must have a "%s", "%s", "%s" annotation!',
103 1
                'Login',
104 1
                'Password',
105
                'ApiKey'
106 1
            ));
107
        }
108
109 9
        return new ClassMetadata(
110 9
            $reflection,
111 9
            $this->userClass,
112
            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...
113 9
                ClassMetadata::LOGIN_PROPERTY       => $loginProperty,
114 9
                ClassMetadata::PASSWORD_PROPERTY    => $passwordProperty,
115 9
                ClassMetadata::API_KEY_PROPERTY     => $apiKeyProperty,
116 9
                ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty,
117
            )
118 9
        );
119
    }
120
121
    /**
122
     * Creates the exception when.
123
     *
124
     * @return \InvalidArgumentException
125
     */
126
    private function createDuplicateAnnotationException()
127
    {
128
        return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!');
129
    }
130
}
131