ReflectionCompositeProvider::getDefault()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Service;
16
17
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite;
18
use Spaark\CompositeUtils\Factory\Reflection\ReflectionCompositeFactory;
19
use Spaark\CompositeUtils\Model\Collection\Map\HashMap;
20
21
/**
22
 */
23
class ReflectionCompositeProvider
24
    implements ReflectionCompositeProviderInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
25
{
26
    /**
27
     * @var ReflectionCompositeProviderInterface
28
     */
29
    protected static $default;
30
31
    /**
32
     * Returns the default ReflectionCompositeProviderInteface used by
33
     * the application
34
     *
35
     * If one has not been set, a new ReflectionCompositeProvider is
36
     * instanciated on the fly
37
     *
38
     * @return ReflectionCompositeProviderInterface
39
     */
40 23
    public static function getDefault()
41
        : ReflectionCompositeProviderInterface
42
    {
43 23
        if (!static::$default)
44
        {
45 1
            static::$default = new static();
46
        }
47
48 23
        return static::$default;
49
    }
50
51
    /**
52
     * Sets the default ReflectionCompositeProviderInterface used by
53
     * the applciation
54
     *
55
     * @param ReflectionCompositeProviderInterface $default
56
     */
57 1
    public static function setDefault
58
    (
59
        ReflectionCompositeProviderInterface $default
60
    )
61
    {
62 1
        static::$default = $default;
63 1
    }
64
65
    /**
66
     * Cache used by this provider
67
     *
68
     * @var HashMap
69
     */
70
    private $cache;
71
72
    /**
73
     * Builds the provider by instanciating its cache
74
     */
75 2
    public function __construct()
76
    {
77 2
        $this->cache = new HashMap();
78 2
    }
79
80
    /**
81
     * Gets a ReflectionComposite, either by using a cached version of
82
     * it or building it on the fly
83
     *
84
     * @param string $classname
85
     * @return ReflectionComposite
86
     */
87 2
    public function get(string $classname) : ReflectionComposite
88
    {
89 2
        if (!$this->cache->containsKey($classname))
0 ignored issues
show
Documentation introduced by
$classname is of type string, but the function expects a object<Spaark\CompositeU...Collection\Map\KeyType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
        {
91 2
            $this->cache[$classname] =
92
                (
93 2
                    ReflectionCompositeFactory::fromClassName
94
                    (
95 2
                        $classname
96
                    )
97
                )
98 2
                ->build();
99
        }
100
        
101 2
        return $this->cache[$classname];
102
    }
103
}
104