Test Failed
Push — master ( c2b4ea...6d4ed5 )
by Gerrit
09:48
created

ArgumentCallFactory::understandsArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory;
14
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use Addiks\SymfonyGenerics\Arguments\Argument;
17
use Addiks\SymfonyGenerics\Arguments\ArgumentCall;
18
use Webmozart\Assert\Assert;
19
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
20
use Addiks\SymfonyGenerics\Arguments\NullArgument;
21
use Addiks\SymfonyGenerics\Arguments\ObjectInstanciationArgument;
22
23
final class ArgumentCallFactory implements ArgumentFactory
24
{
25
26
    /**
27
     * @var ArgumentCompilerInterface
28
     */
29
    private $argumentCompiler;
30
31
    /**
32
     * @var ArgumentFactory
33
     */
34
    private $argumentFactory;
35
36 25
    public function __construct(
37
        ArgumentCompilerInterface $argumentCompiler,
38
        ArgumentFactory $argumentFactory
39
    ) {
40 25
        $this->argumentCompiler = $argumentCompiler;
41 25
        $this->argumentFactory = $argumentFactory;
42 25
    }
43
44 13
    public function understandsString(string $source): bool
45
    {
46 13
        return 1 === preg_match("/^[^\:]+\:\:[a-zA-Z0-9_-]+/is", $source);
47
    }
48
49 4
    public function understandsArray(array $source): bool
50
    {
51 4
        return isset($source['method']) && isset($source['callee']);
52
    }
53
54 6
    public function createArgumentFromString(string $source): Argument
55
    {
56 6
        Assert::true($this->understandsString($source));
57
58
        /** @var array<Argument> $arguments */
59 3
        $arguments = array();
60
61
        /** @var int|bool $argumentsPosition */
62 3
        $argumentsPosition = strpos($source, '(');
63
64
        /** @var string $sourceWithoutArguments */
65 3
        $sourceWithoutArguments = $source;
66
67 3
        if (is_int($argumentsPosition)) {
68
            /** @var string $argumentsSources */
69 2
            $argumentsSources = substr($source, $argumentsPosition + 1);
70 2
            $argumentsSources = str_replace(')', '', $argumentsSources);
71
72 2
            foreach (explode(',', $argumentsSources) as $argumentsSource) {
73 2
                $arguments[] = $this->argumentFactory->createArgumentFromString(trim($argumentsSource));
74
            }
75
76 2
            $sourceWithoutArguments = substr($source, 0, $argumentsPosition);
77
        }
78
79
        /** @var array<string> $sourceParts */
80 3
        $sourceParts = explode('::', $sourceWithoutArguments);
81
82 3
        $methodName = array_pop($sourceParts);
83
84 3
        $calleeSource = implode('::', $sourceParts);
85
86
        /** @var Argument $callee */
87 3
        $callee = $this->argumentFactory->createArgumentFromString($calleeSource);
88
89 3
        return new ArgumentCall($this->argumentCompiler, $callee, $methodName, $arguments);
90
    }
91
92 8
    public function createArgumentFromArray(array $source): Argument
93
    {
94 8
        Assert::keyExists($source, 'method');
95 6
        Assert::keyExists($source, 'callee');
96
97
        /** @var Argument $callee */
98 5
        $callee = null;
0 ignored issues
show
Unused Code introduced by
$callee is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
100 5
        if (is_array($source['callee'])) {
101 2
            $callee = $this->argumentFactory->createArgumentFromArray($source['callee']);
102
103
        } else {
104 3
            $callee = $this->argumentFactory->createArgumentFromString($source['callee']);
105
        }
106
107
        /** @var array<Argument> $arguments */
108 5
        $arguments = array();
109
110 5
        if (isset($source['arguments'])) {
111 3
            foreach ($source['arguments'] as $argumentsSource) {
112
                /** @var array|string $argumentsSource */
113
114 3
                if (is_array($argumentsSource)) {
115 1
                    $arguments[] = $this->argumentFactory->createArgumentFromArray($argumentsSource);
116
117 2
                } elseif (is_null($argumentsSource)) {
118
                    $arguments[] = new NullArgument();
119
120
                } else {
121 2
                    $arguments[] = $this->argumentFactory->createArgumentFromString($argumentsSource);
122
                }
123
            }
124
        }
125
126
        /** @var string $methodName */
127 5
        $methodName = $source['method'];
128
129 5
        if ($methodName === '__construct') {
130
            return new ObjectInstanciationArgument($callee, $arguments);
131
132
        } else {
133 5
            return new ArgumentCall($this->argumentCompiler, $callee, $source['method'], $arguments);
134
        }
135
    }
136
137
}
138