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 (#65)
by
unknown
02:16
created

ConcreteType::checkClassExistence()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Minime\Annotations\Types;
4
5
use stdClass;
6
use ReflectionClass;
7
use Minime\Annotations\ParserException;
8
9
class ConcreteType extends AbstractType
10
{
11
    /**
12
     * @var TypeInterface
13
     */
14
    private static $instance;
15
16
    public static function getType()
17
    {
18
        if (!isset(self::$instance)) {
19
            self::$instance = new ConcreteType();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Minime\Annotations\Types\ConcreteType() of type object<Minime\Annotations\Types\ConcreteType> is incompatible with the declared type object<Minime\Annotations\Types\TypeInterface> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20
        }
21
22
        return self::$instance;
0 ignored issues
show
Bug Compatibility introduced by
The expression self::$instance; of type Minime\Annotations\Types...ons\Types\TypeInterface adds the type Minime\Annotations\Types\TypeInterface to the return on line 22 which is incompatible with the return type declared by the abstract method Minime\Annotations\Types\AbstractType::getType of type Minime\Annotations\Interfaces\TypeInterface.
Loading history...
23
    }
24
25
    /**
26
     * @var array
27
     */
28
    private $namespaceLookup = [];
29
30
    /**
31
     * Set of user defined namespaces to lookup for class autoloading.
32
     *
33
     * @param array $namespaces
34
     */
35
    public function setNamespaces(array $namespaces)
36
    {
37
        $this->namespaceLookup = $namespaces;
38
    }
39
40
    /**
41
     * @param string $class
42
     *
43
     * @return string
44
     *
45
     * @throws ParserException
46
     */
47
    protected function checkClassExistence($class)
48
    {
49
        $found = class_exists($class);
50
        $classname = $class;
51
        $i = 0;
52
53
        while (!$found && $i < count($this->namespaceLookup)) {
54
            $classname = $this->namespaceLookup[$i] . $class;
55
            $found = class_exists($classname);
56
            $i++;
57
        }
58
59
        if (!$found) {
60
            throw new ParserException("Concrete annotation expects '{$class}' to exist.");
61
        }
62
63
        return $classname;
64
    }
65
66
    /**
67
     * Process a value to be a concrete annotation
68
     *
69
     * @param  string $value json string
70
     * @param  string $class name of concrete annotation type (class)
71
     *
72
     * @throws ParserException
73
     *
74
     * @return object
75
     */
76
    public function parse($value, $class = null)
77
    {
78
        $classname = $this->checkClassExistence($class);
79
80
        $prototype = (new JsonType)->parse($value);
81
82
        if ($prototype instanceof stdClass) {
83
            if (!$this->isPrototypeSchemaValid($prototype)) {
84
                throw new ParserException("Only arrays should be used to configure concrete annotation method calls.");
85
            }
86
87
            return $this->makeInstance($classname, $prototype);
88
        }
89
90
        if (is_array($prototype)) {
91
            return $this->makeConstructSugarInjectionInstance($classname, $prototype);
92
        }
93
94
        throw new ParserException("Json value for annotation({$classname}) must be of type object or array.");
95
    }
96
97
    protected function makeConstructSugarInjectionInstance($class, array $prototype) {
98
        $reflection = new ReflectionClass($class);
99
        $instance = $reflection->newInstanceArgs($prototype);
100
101
        return $instance;
102
    }
103
104
    /**
105
     * Creates and hydrates a concrete annotation class
106
     *
107
     * @param  string   $class     full qualified class name
108
     * @param  stdClass $prototype object prototype
109
     * @return object   hydrated concrete annotation class
110
     */
111
    protected function makeInstance($class, stdClass $prototype)
112
    {
113
        $reflection = new ReflectionClass($class);
114
        if (isset($prototype->__construct)) {
115
            $instance = $reflection->newInstanceArgs($prototype->__construct);
116
            unset($prototype->__construct);
117
        } else {
118
            $instance = $reflection->newInstance();
119
        }
120
121
        return $this->doMethodConfiguration($instance, $prototype);
122
    }
123
124
    /**
125
     * Do configuration injection through method calls
126
     *
127
     * @param  object   $instance  concrete annotation instance
128
     * @param  stdClass $prototype object prototype
129
     * @return object   hydrated concrete annotation class
130
     */
131
    protected function doMethodConfiguration($instance, stdClass $prototype)
132
    {
133
        foreach ($prototype as $method => $args) {
134
            call_user_func_array([$instance, $method], $args);
135
            }
136
137
        return $instance;
138
    }
139
140
    /**
141
     * Validates a prototype object
142
     *
143
     * @param  stdClass $prototype object prototype
144
     * @return boolean  true if prototype is valid
145
     */
146
    protected function isPrototypeSchemaValid(stdclass $prototype)
147
    {
148
        foreach ($prototype as $args) {
149
            if (! is_array($args)) {
150
                return false;
151
            }
152
        }
153
154
        return true;
155
    }
156
157
}
158