AbstractEntryProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A getMethods() 0 14 3
A getMethodIdentifier() 0 17 6
1
<?php
2
3
namespace Simply\Container;
4
5
/**
6
 * Abstract entry provider that provides the most basic provider initialization functionality.
7
 *
8
 * @author Riikka Kalliomäki <[email protected]>
9
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
10
 * @license http://opensource.org/licenses/mit-license.php MIT License
11
 */
12
abstract class AbstractEntryProvider implements EntryProvider
13
{
14
    /**
15
     * Returns a new instance of the entry provider.
16
     * @return EntryProvider New entry provider instance initialized with default constructor
17
     */
18 2
    public static function initialize(): EntryProvider
19
    {
20 2
        return new static();
21
    }
22
23
    /**
24
     * Returns provided list of all classes returned by the container methods.
25
     * @return string[] Provided list of all classes returned by the container methods
26
     */
27 1
    public function getMethods(): array
28
    {
29 1
        $methods = [];
30 1
        $reflection = new \ReflectionClass($this);
31
32 1
        foreach ($reflection->getMethods() as $method) {
33 1
            $identifier = $this->getMethodIdentifier($method);
34
35 1
            if (!\is_null($identifier)) {
36 1
                $methods[$identifier] = $method->getName();
37
            }
38
        }
39
40 1
        return $methods;
41
    }
42
43
    /**
44
     * Tells the identifier to use for the given provider method.
45
     * @param \ReflectionMethod $method The provider method
46
     * @return string|null The identifier for the method or null if not applicable
47
     */
48 1
    private function getMethodIdentifier(\ReflectionMethod $method): ?string
49
    {
50 1
        if (!$method->isPublic() || $method->isStatic()) {
51 1
            return null;
52
        }
53
54 1
        if (preg_match('/^__[^_]/', $method->getName())) {
55 1
            return null;
56
        }
57
58 1
        $type = $method->getReturnType();
59
60 1
        if (!$type instanceof \ReflectionType || $type->isBuiltin()) {
61 1
            return null;
62
        }
63
64 1
        return $type->getName();
65
    }
66
}
67