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.

Reader::getMethodAnnotations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Minime\Annotations;
4
5
use Minime\Annotations\Cache\ArrayCache;
6
use Minime\Annotations\Interfaces\CacheInterface;
7
use Minime\Annotations\Interfaces\ParserInterface;
8
use Minime\Annotations\Interfaces\ReaderInterface;
9
use Minime\Annotations\Reflector\ReflectionConst;
10
11
/**
12
 * This class is the primary entry point to read annotations
13
 *
14
 * @package Minime\Annotations
15
 */
16
class Reader implements ReaderInterface
17
{
18
    /**
19
     * @var \Minime\Annotations\Interfaces\ParserInterface
20
     */
21
    protected $parser;
22
23
    /**
24
     * @var \Minime\Annotations\Interfaces\CacheInterface
25
     */
26
    protected $cache;
27
28
    /**
29
     * @param \Minime\Annotations\Interfaces\ParserInterface $parser
30
     */
31
    public function __construct(ParserInterface $parser, CacheInterface $cache = null)
32
    {
33
        $this->setParser($parser);
34
        $this->setCache($cache);
35
    }
36
37
    /**
38
     * @param \Minime\Annotations\Interfaces\CacheInterface $cache Cache handler
39
     */
40
    public function setCache(CacheInterface $cache = null)
41
    {
42
        $this->cache = $cache;
43
    }
44
45
    /**
46
     * @return \Minime\Annotations\Interfaces\CacheInterface Cache handler
47
     */
48
    public function getCache()
49
    {
50
        return $this->cache;
51
    }
52
53
    /**
54
     * @param \Minime\Annotations\Interfaces\ParserInterface $parser
55
     */
56
    public function setParser(ParserInterface $parser)
57
    {
58
        $this->parser = $parser;
59
    }
60
61
    /**
62
     * @return \Minime\Annotations\Interfaces\ParserInterface
63
     */
64
    public function getParser()
65
    {
66
        return $this->parser;
67
    }
68
69
    /**
70
     * Retrieve all annotations from a given function or closure
71
     *
72
     * @param  mixed                                                  $fn Full qualified function name or closure
73
     * @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
74
     * @throws \ReflectionException                                   If function is not found
75
     */
76
    public function getFunctionAnnotations($fn)
77
    {
78
        return $this->getAnnotations(new \ReflectionFunction($fn));
79
    }
80
81
    /**
82
     * Retrieve all annotations from a given class
83
     *
84
     * @param  mixed                                                  $class Full qualified class name or object
85
     * @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
86
     * @throws \ReflectionException                                   If class is not found
87
     */
88
    public function getClassAnnotations($class)
89
    {
90
        return $this->getAnnotations(new \ReflectionClass($class));
91
    }
92
93
    /**
94
     * Retrieve all annotations from a given property of a class
95
     *
96
     * @param  mixed                                                  $class    Full qualified class name or object
97
     * @param  string                                                 $property Property name
98
     * @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
99
     * @throws \ReflectionException                                   If property is undefined
100
     */
101
    public function getPropertyAnnotations($class, $property)
102
    {
103
        return $this->getAnnotations(new \ReflectionProperty($class, $property));
104
    }
105
106
    /**
107
     * Retrieve all annotations from a given method of a class
108
     *
109
     * @param  mixed                                                  $class  Full qualified class name or object
110
     * @param  string                                                 $method Method name
111
     * @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
112
     * @throws \ReflectionException                                   If method is undefined
113
     */
114
    public function getMethodAnnotations($class, $method)
115
    {
116
        return $this->getAnnotations(new \ReflectionMethod($class, $method));
117
    }
118
119
    /**
120
     * Retrieve all annotations from a given constant of a class
121
     *
122
     * @param  string|object                                          $class fully qualified name or instance of the class
123
     * @param  string                                                 $const name of the constant
124
     * @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
125
     */
126
    public function getConstantAnnotations($class, $const)
127
    {
128
        return $this->getAnnotations(new ReflectionConst($class, $const));
129
    }
130
131
    /**
132
     * Retrieve annotations from docblock of a given reflector
133
     *
134
     * @param  \Reflector                                             $Reflection Reflector object
135
     * @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection
136
     */
137
    public function getAnnotations(\Reflector $Reflection)
138
    {
139
        $doc = $Reflection->getDocComment();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Reflector as the method getDocComment() does only exist in the following implementations of said interface: Minime\Annotations\Reflector\ReflectionConst, ReflectionClass, ReflectionFunction, ReflectionFunctionAbstract, ReflectionMethod, ReflectionObject, ReflectionProperty.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
140
        if ($this->cache) {
141
            $key = $this->cache->getKey($doc);
142
            $ast = $this->cache->get($key);
143
            if (! $ast) {
144
                $ast = $this->parser->parse($doc);
145
                $this->cache->set($key, $ast);
146
            }
147
        } else {
148
            $ast = $this->parser->parse($doc);
149
        }
150
151
        return new AnnotationsBag($ast);
152
    }
153
154
    /**
155
     * Shortcut to create an instance of the default annotations Reader
156
     * bundled with the default Parser implementation
157
     *
158
     * @return \Minime\Annotations\Interfaces\ReaderInterface
159
     */
160
    public static function createFromDefaults()
161
    {
162
        return new self(new Parser, new ArrayCache);
163
    }
164
}
165