Completed
Pull Request — master (#119)
by Tobias
05:34
created

ClassDiscovery::setStrategies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Discovery;
4
5
use Http\Discovery\Exception\ClassInstantiationFailedException;
6
use Http\Discovery\Exception\DiscoveryFailedException;
7
use Http\Discovery\Exception\NoCandidateFoundException;
8
use Http\Discovery\Exception\StrategyUnavailableException;
9
10
/**
11
 * Registry that based find results on class existence.
12
 *
13
 * @author David de Boer <[email protected]>
14
 * @author Márk Sági-Kazár <[email protected]>
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
abstract class ClassDiscovery
18
{
19
    /**
20
     * A list of strategies to find classes.
21
     *
22
     * @var array
23
     */
24
    private static $strategies = [
25
        Strategy\PuliBetaStrategy::class,
26
        Strategy\CommonClassesStrategy::class,
27
    ];
28
29
    /**
30
     * Discovery cache to make the second time we use discovery faster.
31
     *
32
     * @var array
33
     */
34
    private static $cache = [];
35
36
    /**
37
     * Finds a class.
38
     *
39
     * @param string $type
40
     *
41
     * @return string|\Closure
42
     *
43
     * @throws DiscoveryFailedException
44
     */
45 15
    protected static function findOneByType($type)
46
    {
47
        // Look in the cache
48 15
        if (null !== ($class = self::getFromCache($type))) {
49
            return $class;
50
        }
51
52 15
        $exceptions = [];
53 15
        foreach (self::$strategies as $strategy) {
54
            try {
55 15
                $candidates = call_user_func($strategy.'::getCandidates', $type);
56 15
            } catch (StrategyUnavailableException $e) {
57
                $exceptions[] = $e;
58
59
                continue;
60
            }
61
62 15
            foreach ($candidates as $candidate) {
63 9
                if (isset($candidate['condition'])) {
64 7
                    if (!self::evaluateCondition($candidate['condition'])) {
65 1
                        continue;
66
                    }
67 7
                }
68
69
                // save the result for later use
70 9
                self::storeInCache($type, $candidate);
71
72 9
                return $candidate['class'];
73 7
            }
74
75 7
            $exceptions[] = new NoCandidateFoundException($strategy, $candidates);
76 7
        }
77
78 6
        throw DiscoveryFailedException::create($exceptions);
79
    }
80
81
    /**
82
     * Get a value from cache.
83
     *
84
     * @param string $type
85
     *
86
     * @return string|null
87
     */
88 15
    private static function getFromCache($type)
89
    {
90 15
        if (!isset(self::$cache[$type])) {
91 15
            return;
92
        }
93
94
        $candidate = self::$cache[$type];
95
        if (isset($candidate['condition'])) {
96
            if (!self::evaluateCondition($candidate['condition'])) {
97
                return;
98
            }
99
        }
100
101
        return $candidate['class'];
102
    }
103
104
    /**
105
     * Store a value in cache.
106
     *
107
     * @param string $type
108
     * @param string $class
109
     */
110 9
    private static function storeInCache($type, $class)
111
    {
112 9
        self::$cache[$type] = $class;
113 9
    }
114
115
    /**
116
     * Set new strategies and clear the cache.
117
     *
118
     * @param array $strategies string array of fully qualified class name to a DiscoveryStrategy
119
     */
120 27
    public static function setStrategies(array $strategies)
121
    {
122 27
        self::$strategies = $strategies;
123 27
        self::clearCache();
124 27
    }
125
126
    /**
127
     * Append a strategy at the end of the strategy queue.
128
     *
129
     * @param string $strategy Fully qualified class name to a DiscoveryStrategy
130
     */
131 1
    public static function appendStrategy($strategy)
132
    {
133 1
        self::$strategies[] = $strategy;
134 1
        self::clearCache();
135 1
    }
136
137
    /**
138
     * Prepend a strategy at the beginning of the strategy queue.
139
     *
140
     * @param string $strategy Fully qualified class name to a DiscoveryStrategy
141
     */
142 1
    public static function prependStrategy($strategy)
143
    {
144 1
        array_unshift(self::$strategies, $strategy);
145 1
        self::clearCache();
146 1
    }
147
148
    /**
149
     * Clear the cache.
150
     */
151 27
    public static function clearCache()
152
    {
153 27
        self::$cache = [];
154 27
    }
155
156
    /**
157
     * Evaluates conditions to boolean.
158
     *
159
     * @param mixed $condition
160
     *
161
     * @return bool
162
     */
163 7
    protected static function evaluateCondition($condition)
164
    {
165 7
        if (is_string($condition)) {
166
            // Should be extended for functions, extensions???
167
            return self::safeClassExists($condition);
168 7
        } elseif (is_callable($condition)) {
169
            return $condition();
170 7
        } elseif (is_bool($condition)) {
171 7
            return $condition;
172 1
        } elseif (is_array($condition)) {
173
            // Immediately stop execution if the condition is false
174 1
            for ($i = 0; $i < count($condition); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
175 1
                if (false === static::evaluateCondition($condition[$i])) {
176 1
                    return false;
177
                }
178 1
            }
179
180
            return true;
181
        }
182
183
        return false;
184
    }
185
186
    /**
187
     * Get an instance of the $class.
188
     *
189
     * @param string|\Closure $class A FQCN of a class or a closure that instantiate the class.
190
     *
191
     * @return object
192
     *
193
     * @throws ClassInstantiationFailedException
194
     */
195 5
    protected static function instantiateClass($class)
196
    {
197
        try {
198 5
            if (is_string($class)) {
199 5
                return new $class();
200
            }
201
202
            if (is_callable($class)) {
203
                return $class();
204
            }
205
        } catch (\Exception $e) {
206
            throw new ClassInstantiationFailedException('Unexpected exception when instantiating class.', 0, $e);
207
        }
208
209
        throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string');
210
    }
211
212
    /**
213
     * We want to do a "safe" version of PHP's "class_exists" because Magento has a bug
214
     * (or they call it a "feature"). Magento is throwing an exception if you do class_exists()
215
     * on a class that ends with "Factory" and if that file does not exits.
216
     *
217
     * This function will catch all potential exceptions and make sure it returns a boolean.
218
     *
219
     * @param string $class
220
     * @param bool   $autoload
0 ignored issues
show
Bug introduced by
There is no parameter named $autoload. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
221
     *
222
     * @return bool
223
     */
224
    public static function safeClassExists($class)
225
    {
226
        try {
227
            return class_exists($class);
228
        } catch (\Exception $e) {
229
            return false;
230
        }
231
    }
232
}
233