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
Branch develop (4efcd8)
by Maximilian
04:45
created

AnnotationDriver::getMetadataForUser()   C

Complexity

Conditions 15
Paths 26

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 15

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 55
ccs 42
cts 42
cp 1
rs 6.7239
cc 15
eloc 39
nc 26
nop 0
crap 15

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
                            $this->assertUnique($loginProperty);
60 9
                            $loginProperty = $reflectionProperty;
61 9
                            break;
62 10
                        case 'password':
63 9
                            $this->assertUnique($passwordProperty);
64 9
                            $passwordProperty = $reflectionProperty;
65 9
                            break;
66 10
                        case 'apiKey':
67 10
                            $this->assertUnique($apiKeyProperty);
68 10
                            $apiKeyProperty = $reflectionProperty;
69 10
                            break;
70 9
                        case 'lastAction':
71 9
                            $this->assertUnique($lastActionProperty);
72 9
                            $lastActionProperty = $reflectionProperty;
73 9
                    }
74
75 10
                    if ($loginProperty && $passwordProperty && $apiKeyProperty && $lastActionProperty) {
76 9
                        break;
77
                    }
78
79 10
                    continue;
80
                }
81 10
            }
82 10
        }
83
84 10
        if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) {
85 1
            throw new \LogicException(sprintf(
86 1
                'A user class must have a "%s", "%s", "%s" annotation!',
87 1
                'Login',
88 1
                'Password',
89
                'ApiKey'
90 1
            ));
91
        }
92
93 9
        return new ClassMetadata(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...
94 9
            ClassMetadata::LOGIN_PROPERTY       => $loginProperty,
95 9
            ClassMetadata::PASSWORD_PROPERTY    => $passwordProperty,
96 9
            ClassMetadata::API_KEY_PROPERTY     => $apiKeyProperty,
97 9
            ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty,
98 9
        ));
99
    }
100
101
    /**
102
     * Checks whether a property is already set.
103
     *
104
     * @param \ReflectionProperty $property
105
     */
106 10
    private function assertUnique(\ReflectionProperty $property = null)
107
    {
108 10
        if (!empty($property)) {
109
            throw $this->createDuplicateAnnotationException();
110
        }
111 10
    }
112
113
    /**
114
     * Creates the exception when.
115
     *
116
     * @return \InvalidArgumentException
117
     */
118
    private function createDuplicateAnnotationException()
119
    {
120
        return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!');
121
    }
122
}
123