Assignment::identifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Fixer\NestingConsistency;
5
6
final class Assignment
7
{
8
    /**
9
     * @var Identifier
10
     */
11
    private $identifier;
12
13
    /**
14
     * @var Operator
15
     */
16
    private $operator;
17
18
    /**
19
     * @var string
20
     */
21
    private $value;
22
23
    /**
24
     * Assignment constructor.
25
     *
26
     * @param string $string
27
     */
28
    public function __construct(string $string)
29
    {
30
        $this->operator = $this->detectOperator($string);
31
        [$identifierString, $this->value] = $this->splitByOperator($string);
32
        $this->identifier = new Identifier($identifierString);
33
    }
34
35
    /**
36
     * @return Identifier
37
     */
38
    public function identifier(): Identifier
39
    {
40
        return $this->identifier;
41
    }
42
43
    /**
44
     * @return Operator
45
     */
46
    public function operator(): Operator
47
    {
48
        return $this->operator;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function value(): string
55
    {
56
        return $this->value;
57
    }
58
59
    /**
60
     * @param string $line
61
     *
62
     * @return Operator
63
     */
64
    private function detectOperator(string $line): Operator
65
    {
66
        if (strpos($line, '=<') !== false) {
67
            return Operator::createReference();
68
        }
69
        if (strpos($line, ':=') !== false) {
70
            return Operator::createModification();
71
        }
72
        if (strpos($line, '=') !== false) {
73
            return Operator::createEqual();
74
        }
75
        if (strpos($line, '<') !== false) {
76
            return Operator::createCopy();
77
        }
78
        if (strpos($line, '>') !== false) {
79
            return Operator::createDelete();
80
        }
81
        if (strpos($line, '(') !== false && strpos($line, ')') !== false) {
82
            return Operator::createMultiLine();
83
        }
84
        throw new \RuntimeException('Assignment needs an operator');
85
    }
86
87
    /**
88
     * @param string $string
89
     *
90
     * @return array
91
     */
92
    private function splitByOperator(string $string): array
93
    {
94
        if ($this->operator->isMultiLine()) {
95
            $array = explode('(', $string);
96
            $array[1] = trim(rtrim(trim($array[1]), ')'));
97
        } else {
98
            $array = array_filter(explode((string)$this->operator, $string));
99
        }
100
101
        if (!isset($array[1])) {
102
            $array[1] = '';
103
        }
104
105
        foreach ($array as $key => $item) {
106
            $array[$key] = trim($item);
107
        }
108
109
        return $array;
110
    }
111
}
112