Issues (3)

src/ArgumentResolver.php (1 issue)

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\DefaultValueResolver;
21
use DivineNii\Invoker\ArgumentResolver\NamedValueResolver;
22
use DivineNii\Invoker\ArgumentResolver\TypeHintValueResolver;
23
use DivineNii\Invoker\Interfaces\ArgumentResolverInterface;
24
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface;
25
use Psr\Container\ContainerInterface;
26
27
class ArgumentResolver implements ArgumentResolverInterface
28
{
29
    /** @var ArgumentValueResolverInterface[] */
30
    private $argumentValueResolvers;
31
32
    /** @var null|ContainerInterface */
33
    private static $container;
34
35
    /**
36
     * @param iterable<ArgumentValueResolverInterface> $argumentValueResolvers
37
     * @param null|ContainerInterface                  $container
38
     */
39 56
    public function __construct(iterable $argumentValueResolvers = [], ?ContainerInterface $container = null)
40
    {
41 56
        self::$container              = $container;
42 56
        $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...
43 56
    }
44
45
    /**
46
     * Push a parameter resolver after the ones already registered.
47
     *
48
     * @param ArgumentValueResolverInterface $resolvers
49
     */
50 1
    public function appendResolver(ArgumentValueResolverInterface ...$resolvers): void
51
    {
52 1
        foreach ($resolvers as $resolver) {
53 1
            $this->argumentValueResolvers[] = $resolver;
54
        }
55 1
    }
56
57
    /**
58
     * Insert a parameter resolver before the ones already registered.
59
     *
60
     * @param ArgumentValueResolverInterface $resolvers
61
     */
62 1
    public function prependResolver(ArgumentValueResolverInterface ...$resolvers): void
63
    {
64 1
        \array_unshift($this->argumentValueResolvers, ...$resolvers);
65 1
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 46
    public function getParameters(\ReflectionFunctionAbstract $reflection, array $providedParameters = []): array
71
    {
72 46
        $resolvedParameters   = [];
73 46
        $reflectionParameters = $reflection->getParameters();
74
75 46
        foreach ($reflectionParameters as $parameter) {
76 33
            $position = $parameter->getPosition();
77
78 33
            foreach ($this->argumentValueResolvers as $resolver) {
79 33
                if (null !== $resolved = $resolver->resolve($parameter, $providedParameters)) {
80 32
                    if ($resolved === DefaultValueResolver::class) {
81 4
                        $resolved = null;
82
                    }
83
84 32
                    if ($parameter->isVariadic() && \is_array($resolved)) {
85 2
                        if (\count($resolved) > 1) {
86 1
                            foreach (\array_chunk($resolved, 1) as $index => [$value]) {
87 1
                                $resolvedParameters[$index + 1] = $value;
88
                            }
89
90 1
                            continue;
91
                        }
92
93 1
                        $resolved = \current($resolved);
94
                    }
95
96 32
                    $resolvedParameters[$position] = $resolved;
97
                }
98
99 33
                if (empty(\array_diff_key($reflectionParameters, $resolvedParameters))) {
100
                    // Stop traversing: all parameters are resolved
101 30
                    return $resolvedParameters;
102
                }
103
            }
104
        }
105
106 17
        return $resolvedParameters;
107
    }
108
109
    /**
110
     * @return iterable<ArgumentValueResolverInterface>
111
     */
112 56
    public static function getDefaultArgumentValueResolvers(): iterable
113
    {
114
        return [
115 56
            new NamedValueResolver(self::$container),
116 56
            new TypeHintValueResolver(self::$container),
117 56
            new DefaultValueResolver(),
118
        ];
119
    }
120
}
121