Completed
Push — master ( 463d73...47ba96 )
by Gerrit
02:04
created

ArgumentCallFactory::understandsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
20
final class ArgumentCallFactory implements ArgumentFactory
21
{
22
23
    /**
24
     * @var ArgumentFactory
25
     */
26
    private $argumentFactory;
27
28
    public function __construct(
29
        ArgumentFactory $argumentFactory
30
    ) {
31
        $this->argumentFactory = $argumentFactory;
32
    }
33
34
    public function understandsString(string $source): bool
35
    {
36
        return strpos($source, '::') > 0;
37
    }
38
39
    public function understandsArray(array $source): bool
40
    {
41
        return isset($source['method']) && isset($source['callee']);
42
    }
43
44
    public function createArgumentFromString(string $source): Argument
45
    {
46
        /** @var array<Argument> $arguments */
47
        $arguments = array();
48
49
        Assert::contains($source, '::');
50
51
        /** @var int|bool $argumentsPosition */
52
        $argumentsPosition = strpos($source, '(');
53
54
        /** @var string $sourceWithoutArguments */
55
        $sourceWithoutArguments = $source;
56
57
        if (is_int($argumentsPosition)) {
58
            /** @var string $argumentsSources */
59
            $argumentsSources = substr($source, $argumentsPosition + 1);
60
            $argumentsSources = str_replace(')', '', $argumentsSources);
61
62
            foreach (explode(',', $argumentsSources) as $argumentsSource) {
63
                $arguments[] = $this->argumentFactory->createArgumentFromString($argumentsSource);
64
            }
65
66
            $sourceWithoutArguments = substr($source, 0, $argumentsPosition);
67
        }
68
69
        Assert::contains($sourceWithoutArguments, '::');
70
71
        [$calleeSource, $methodName] = explode('::', $sourceWithoutArguments);
0 ignored issues
show
Bug introduced by
The variable $calleeSource does not exist. Did you mean $source?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $methodName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
72
73
        /** @var Argument $callee */
74
        $callee = $this->argumentFactory->createArgumentFromString($calleeSource);
0 ignored issues
show
Bug introduced by
The variable $calleeSource does not exist. Did you mean $source?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
75
76
        return new ArgumentCall($callee, $methodName, $arguments);
77
    }
78
79
    public function createArgumentFromArray(array $source): Argument
80
    {
81
        Assert::keyExists($source, 'method');
82
        Assert::keyExists($source, 'callee');
83
84
        /** @var Argument $callee */
85
        $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...
86
87
        if (is_array($source['callee'])) {
88
            $callee = $this->argumentFactory->createArgumentFromArray($source['callee']);
89
90
        } else {
91
            $callee = $this->argumentFactory->createArgumentFromString($source['callee']);
92
        }
93
94
        /** @var array<Argument> $arguments */
95
        $arguments = array();
96
97
        if (isset($source['arguments'])) {
98
            foreach ($source['arguments'] as $argumentsSource) {
99
                /** @var array|string $argumentsSource */
100
101
                if (is_array($argumentsSource)) {
102
                    $arguments[] = $this->argumentFactory->createArgumentFromArray($argumentsSource);
103
104
                } else {
105
                    $arguments[] = $this->argumentFactory->createArgumentFromString($argumentsSource);
106
                }
107
            }
108
        }
109
110
        return new ArgumentCall($callee, $source['method'], $arguments);
111
    }
112
113
}
114