Passed
Push — master ( dc91ec...37d931 )
by Divine Niiquaye
07:45
created

ArgumentResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 92
ccs 31
cts 31
cp 1
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A appendResolver() 0 4 2
A getDefaultArgumentValueResolvers() 0 7 1
A prependResolver() 0 3 1
B getParameters() 0 37 7
A __construct() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of PHP Invoker.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace DivineNii\Invoker;
19
20
use DivineNii\Invoker\ArgumentResolver\ClassValueResolver;
21
use DivineNii\Invoker\ArgumentResolver\DefaultValueResolver;
22
use DivineNii\Invoker\ArgumentResolver\NamedValueResolver;
23
use DivineNii\Invoker\ArgumentResolver\TypeHintValueResolver;
24
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface;
25
use DivineNii\Invoker\Interfaces\ArgumentResolverInterface;
26
use Psr\Container\ContainerInterface;
27
use ReflectionFunctionAbstract;
28
29
class ArgumentResolver implements ArgumentResolverInterface
30
{
31
    /** @var ArgumentValueResolverInterface[] */
32
    private $argumentValueResolvers;
33
34
    /** @var null|ContainerInterface */
35
    private static $container;
36
37
    /**
38
     * @param iterable<ArgumentValueResolverInterface> $argumentValueResolvers
39
     * @param null|ContainerInterface $container
40
     */
41 50
    public function __construct(iterable $argumentValueResolvers = [], ?ContainerInterface $container = null)
42
    {
43 50
        self::$container              = $container;
44 50
        $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
0 ignored issues
show
Documentation Bug introduced by
It seems like $argumentValueResolvers ...rgumentValueResolvers() can also be of type iterable. However, the property $argumentValueResolvers is declared as type DivineNii\Invoker\Interf...alueResolverInterface[]. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
45 50
    }
46
47
    /**
48
     * Push a parameter resolver after the ones already registered.
49
     *
50
     * @param ArgumentValueResolverInterface $resolvers
51
     */
52 1
    public function appendResolver(ArgumentValueResolverInterface ...$resolvers): void
53
    {
54 1
        foreach ($resolvers as $resolver) {
55 1
            $this->argumentValueResolvers[] = $resolver;
56
        }
57 1
    }
58
59
    /**
60
     * Insert a parameter resolver before the ones already registered.
61
     *
62
     * @param ArgumentValueResolverInterface $resolvers
63
     */
64 1
    public function prependResolver(ArgumentValueResolverInterface ...$resolvers): void
65
    {
66 1
        \array_unshift($this->argumentValueResolvers, ...$resolvers);
67 1
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 41
    public function getParameters(ReflectionFunctionAbstract $reflection, array $providedParameters = []): array
73
    {
74 41
        $resolvedParameters   = [];
75 41
        $reflectionParameters = $reflection->getParameters();
76
77 41
        foreach ($reflectionParameters as $parameter) {
78 29
            $position = $parameter->getPosition();
79
80
            /**
81
             * Simply returns all the values of the $providedParameters array that are
82
             * indexed by the parameter position (i.e. a number).
83
             * E.g. `->call($callable, ['foo', 'bar'])` will simply resolve the parameters
84
             * to `['foo', 'bar']`.
85
             * Parameters that are not indexed by a number (i.e. parameter position)
86
             * will be ignored.
87
             */
88 29
            if (isset($providedParameters[$position])) {
89 8
                $providedParameters[$parameter->name] = $providedParameters[$position];
90 8
                unset($providedParameters[$position]);
91
            }
92
93 29
            foreach ($this->argumentValueResolvers as $resolver) {
94 29
                if (null !== $resolved = $resolver->resolve($parameter, $providedParameters)) {
95 28
                    $resolvedParameters[$position] = DefaultValueResolver::class !== $resolved ? $resolved : null;
96
                }
97
98 29
                if (empty(\array_diff_key($reflectionParameters, $resolvedParameters))) {
99
                    // Stop traversing: all parameters are resolved
100 26
                    return $resolvedParameters;
101
                }
102
103
                // continue to the next callable argument
104 26
                continue 1;
105
            }
106
        }
107
108 15
        return $resolvedParameters;
109
    }
110
111
    /**
112
     * @return iterable<ArgumentValueResolverInterface>
113
     */
114 50
    public static function getDefaultArgumentValueResolvers(): iterable
115
    {
116
        return [
117 50
            new TypeHintValueResolver(self::$container),
118 50
            new ClassValueResolver(self::$container),
119 50
            new NamedValueResolver(self::$container),
120 50
            new DefaultValueResolver(),
121
        ];
122
    }
123
}
124