Passed
Push — main ( a2d04f...938ee5 )
by Andrey
24:43 queued 22:52
created

Resolvable::getSameClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/******************************************************************************
3
 * This file is part of the "andrey-helldar/support" project.                 *
4
 *                                                                            *
5
 * @author Andrey Helldar <[email protected]>                                *
6
 *                                                                            *
7
 * @copyright 2021 Andrey Helldar                                             *
8
 *                                                                            *
9
 * @license MIT                                                               *
10
 *                                                                            *
11
 * @see https://github.com/andrey-helldar/support                             *
12
 *                                                                            *
13
 * For the full copyright and license information, please view the LICENSE    *
14
 * file that was distributed with this source code.                           *
15
 ******************************************************************************/
16
17
namespace Helldar\Support\Concerns;
18
19
trait Resolvable
20
{
21
    protected static $resolved = [];
22
23 672
    protected static function resolveInstance($instance, ...$parameters)
24
    {
25 672
        $class = is_object($instance) ? get_class($instance) : $instance;
26
27 672
        if (isset(self::$resolved[$class])) {
28 521
            return self::$resolved[$class];
29
        }
30
31 671
        return self::$resolved[$class] = is_object($instance) ? $instance : new $instance(...$parameters);
32
    }
33
34 1
    protected static function resolveCallback(string $value, callable $callback)
35
    {
36 1
        $class = static::getSameClass();
37
38 1
        if (isset(static::$resolved[$class][$value])) {
39 1
            return static::$resolved[$class][$value];
40
        }
41
42 1
        return static::$resolved[$class][$value] = $callback($value);
43
    }
44
45 2
    protected static function getSameClass(): string
46
    {
47 2
        return static::class;
48
    }
49
}
50