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 ( cb7f05...c88989 )
by Maximilian
09:13
created

AnnotationDriver::getMetadataForUser()   C

Complexity

Conditions 15
Paths 26

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 15

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 55
ccs 37
cts 37
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\Service\Mapping\Driver;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata;
7
use ReflectionClass;
8
9
/**
10
 * Annotation driver which parses the annotations of the user model instance.
11
 *
12
 * @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases
13
 */
14
final class AnnotationDriver implements ModelConfigurationDriverInterface
15
{
16
    /**
17
     * @var Reader
18
     */
19
    private $reader;
20
21
    /**
22
     * @var string
23
     */
24
    private $userClass;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param Reader $annotationReader
30
     * @param string $userClass
31
     */
32 18
    public function __construct(Reader $annotationReader, $userClass)
33
    {
34 18
        $this->reader = $annotationReader;
35 18
        $this->userClass = (string) $userClass;
36 18
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @throws \LogicException If one of the annotations is missing
42
     * @throws \LogicException If one property has multiple "auth" annotations
43
     */
44 11
    public function getMetadataForUser()
45
    {
46 11
        $reflection = new ReflectionClass($this->userClass);
47 11
        $properties = $reflection->getProperties();
48 11
        $loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null;
49
50 11
        foreach ($properties as $reflectionProperty) {
51 11
            foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) {
52 11
                $class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation));
53 11
                $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...
54
55 11
                if ($annotationObject) {
56
                    switch ($annotation) {
57 11
                        case 'login':
58 10
                            $this->assertUnique($loginProperty);
59 10
                            $loginProperty = $reflectionProperty;
60 10
                            break;
61 11
                        case 'password':
62 10
                            $this->assertUnique($passwordProperty);
63 10
                            $passwordProperty = $reflectionProperty;
64 10
                            break;
65 11
                        case 'apiKey':
66 11
                            $this->assertUnique($apiKeyProperty);
67 11
                            $apiKeyProperty = $reflectionProperty;
68 11
                            break;
69 10
                        case 'lastAction':
70 10
                            $this->assertUnique($lastActionProperty);
71 10
                            $lastActionProperty = $reflectionProperty;
72
                    }
73
74 11
                    if ($loginProperty && $passwordProperty && $apiKeyProperty && $lastActionProperty) {
75 10
                        break;
76
                    }
77
78 11
                    continue;
79
                }
80
            }
81
        }
82
83 11
        if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) {
84 1
            throw new \LogicException(sprintf(
85 1
                'A user class must have a "%s", "%s", "%s" annotation!',
86 1
                'Login',
87 1
                'Password',
88 1
                'ApiKey'
89
            ));
90
        }
91
92
        return array(
0 ignored issues
show
Best Practice introduced by
The expression return array(\Ma27\ApiKe...> $lastActionProperty); seems to be an array, but some of its elements' types (null) are incompatible with the return type declared by the interface Ma27\ApiKeyAuthenticatio...ace::getMetadataForUser of type ReflectionProperty[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
93 10
            ClassMetadata::LOGIN_PROPERTY       => $loginProperty,
94 10
            ClassMetadata::PASSWORD_PROPERTY    => $passwordProperty,
95 10
            ClassMetadata::API_KEY_PROPERTY     => $apiKeyProperty,
96 10
            ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty,
97
        );
98
    }
99
100
    /**
101
     * Checks whether a property is already set.
102
     *
103
     * @param \ReflectionProperty $property
104
     */
105 11
    private function assertUnique(\ReflectionProperty $property = null)
106
    {
107 11
        if (!empty($property)) {
108
            throw $this->createDuplicateAnnotationException();
109
        }
110 11
    }
111
112
    /**
113
     * Creates the exception when.
114
     *
115
     * @return \InvalidArgumentException
116
     */
117
    private function createDuplicateAnnotationException()
118
    {
119
        return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!');
120
    }
121
}
122