Test Failed
Push — master ( 9630e3...8f03a2 )
by Kamil
02:34
created

ArgumentResolver::resolveArgument()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.5435

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 4
cts 9
cp 0.4444
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 4.5435
1
<?php
2
declare(strict_types=1);
3
namespace VirCom\Behat3ZendFramework3Extension\Context\Argument;
4
5
use Behat\Behat\Context\Argument\ArgumentResolver as ArgumentResolverInterface;
6
7
use Zend\Mvc\ApplicationInterface;
8
9
use ReflectionClass;
10
11
class ArgumentResolver implements
12
    ArgumentResolverInterface
13
{
14
    /**
15
     * @var ApplicationInterface
16
     */
17
    protected $application;
18
19
    /**
20
     * @param ApplicationInterface $application
21
     */
22 2
    public function __construct(
23
        ApplicationInterface $application
24
    ) {
25 2
        $this->application = $application;
26 2
    }
27
28
    /**
29
     * @param ReflectionClass $classReflection
30
     * @param array $arguments
31
     * @return array
32
     *
33
     * @SuppressWarnings("unused")
34
     */
35 1
    public function resolveArguments(
36
        ReflectionClass $classReflection,
37
        array $arguments
38
    ): array {
39 1
        $result = [];
40
41 1
        foreach ($arguments as $key => $name) {
42 1
            $result[$key] = $this->resolveArgument($name);
43
        }
44
45
        return $result;
46
    }
47
48
    /**
49
     * @param string $argument
50
     * @return mixed
51
     */
52 1
    protected function resolveArgument(
53
        string $argument
54
    ) {
55 1
        if (substr($argument, 0, 1) !== "@") {
56
            return $argument;
57
        }
58
        
59 1
        $serviceManager = $this->application->getServiceManager();
60 1
        if($serviceManager->has(substr($argument, 1))) {
61
            return $this->application
62
                ->getServiceManager()
63
                ->get(substr($argument, 1));
64
        }
65
66
        return $argument;
67
    }
68
}
69