Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / HelperContainer / ArgumentAutowirer.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\HelperContainer;
12
13 use Psr\Container\ContainerExceptionInterface;
14 use Psr\Container\ContainerInterface;
15 use ReflectionFunctionAbstract;
16 use ReflectionParameter;
17
18 /**
19  * Automatically wires arguments of a given function from inside the container by using type-hitns.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 final class ArgumentAutowirer
24 {
25     /**
26      * @var ContainerInterface
27      */
28     private $container;
29
30     /**
31      * Initialises wirer.
32      *
33      * @param ContainerInterface $container
34      */
35     public function __construct(ContainerInterface $container)
36     {
37         $this->container = $container;
38     }
39
40     /**
41      * Autowires given arguments using provided container.
42      *
43      * @param ReflectionFunctionAbstract $reflection
44      * @param array $arguments
45      *
46      * @return array
47      *
48      * @throws ContainerExceptionInterface if unset argument typehint can not be resolved from container
49      */
50     public function autowireArguments(ReflectionFunctionAbstract $reflection, array $arguments)
51     {
52         $newArguments = $arguments;
53         foreach ($reflection->getParameters() as $index => $parameter) {
54             if ($this->isArgumentWireable($newArguments, $index, $parameter)) {
55                 $newArguments[$index] = $this->container->get($parameter->getClass()->getName());
56             }
57         }
58
59         return $newArguments;
60     }
61
62     /**
63      * Checks if given argument is wireable.
64      *
65      * Argument is wireable if it was not previously set and it has a class type-hint.
66      *
67      * @param array               $arguments
68      * @param integer             $index
69      * @param ReflectionParameter $parameter
70      *
71      * @return bool
72      */
73     private function isArgumentWireable(array $arguments, $index, ReflectionParameter $parameter)
74     {
75         return !isset($arguments[$index]) && !isset($arguments[$parameter->getName()]) && $parameter->getClass();
76     }
77 }