ClassResolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 12 2
A resolveConstructorArgs() 0 24 4
A tryToGetArgsFromInput() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/cuttle.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Cuttle;
13
14
use ReflectionClass;
15
16
/**
17
 * Class ClassResolver.
18
 *
19
 * @author overtrue <[email protected]>
20
 */
21
class ClassResolver
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $class;
27
28
    /**
29
     * ClassResolver constructor.
30
     *
31
     * @param string $class
32
     */
33
    public function __construct(string $class)
34
    {
35
        $this->class = $class;
36
    }
37
38
    /**
39
     * @param array $args
40
     *
41
     * @return object
42
     */
43
    public function resolve(array $args = [])
44
    {
45
        $reflected = new ReflectionClass($this->class);
46
47
        if (0 == $reflected->getConstructor()->getNumberOfParameters()) {
48
            return $reflected->newInstanceWithoutConstructor();
49
        }
50
51
        $args = $this->resolveConstructorArgs($reflected, $args);
52
53
        return $reflected->newInstanceArgs($args);
54
    }
55
56
    /**
57
     * @param \ReflectionClass $reflected
58
     * @param array            $inputArgs
59
     *
60
     * @return array
61
     */
62
    protected function resolveConstructorArgs(ReflectionClass $reflected, array $inputArgs)
63
    {
64
        $args = [];
65
66
        foreach ($reflected->getConstructor()->getParameters() as $parameter) {
67
            $name = $parameter->getName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
            $value = $this->tryToGetArgsFromInput($inputArgs, $name);
69
70
            if (is_null($value)) {
71
                if (!$parameter->isOptional()) {
72
                    throw new InvalidArgumentException(sprintf(
73
                        'No value configured for parameter "%s" of %s::__constructor method.',
74
                        $name,
75
                        $reflected->getName()
76
                    ));
77
                }
78
                $value = $parameter->getDefaultValue();
79
            }
80
81
            $args[$name] = $value;
82
        }
83
84
        return $args;
85
    }
86
87
    /**
88
     * @param array  $input
89
     * @param string $name
90
     *
91
     * @return mixed
92
     */
93
    public function tryToGetArgsFromInput(array $input, string $name)
94
    {
95
        return $input[camel_case($name)] ?? $input[snake_case($name)] ?? null;
96
    }
97
}
98