Passed
Push — master ( 2ee547...bfef22 )
by Aleksei
13:34
created

QuotedValue::trimValue()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 48
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12.3654

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 48
ccs 19
cts 22
cp 0.8636
rs 6.9666
c 0
b 0
f 0
cc 12
nc 10
nop 0
crap 12.3654

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Stempler\Transform;
6
7
use Spiral\Stempler\Node\HTML\Nil;
8
use Spiral\Stempler\Node\Mixin;
9
use Spiral\Stempler\Node\NodeInterface;
10
use Spiral\Stempler\Node\Raw;
11
12
/**
13
 * Carries value defined inside via attribute.
14
 */
15
final class QuotedValue
16
{
17 36
    public function __construct(
18
        private readonly NodeInterface|string $value
19
    ) {
20 36
    }
21
22 18
    public function getValue(): mixed
23
    {
24 18
        return $this->value;
25
    }
26
27
    /**
28
     * @return array<NodeInterface|string>
29
     */
30 35
    public function trimValue(): array
31
    {
32 35
        $value = $this->value;
33 35
        if ($value instanceof Nil) {
34
            return [];
35
        }
36
37 35
        if (\is_string($value)) {
38 20
            return [new Raw(\trim($value, '\'"'))];
39
        }
40
41 16
        if (!$value instanceof Mixin) {
42
            return [$value];
43
        }
44
45
        // trim mixin quotes
46 16
        $nodes = $value->nodes;
47
48 16
        if (\count($nodes) >= 3 && $nodes[0] instanceof Raw && $nodes[\count($nodes) - 1] instanceof Raw) {
49
            /**
50
             * TODO issue #767
51
             * @link https://github.com/spiral/framework/issues/767
52
             * @psalm-suppress InvalidArrayAccess
53
             */
54 16
            $quote = $nodes[0]->content[0];
55 16
            if (!\in_array($quote, ['"', "'"])) {
56
                return $nodes;
57
            }
58
59 16
            $nodes[0] = new Raw(\ltrim($nodes[0]->content, $quote));
60
            /**
61
             * TODO issue #767
62
             * @link https://github.com/spiral/framework/issues/767
63
             * @psalm-suppress NoInterfaceProperties
64
             */
65 16
            $content = $nodes[\count($nodes) - 1]->content;
66 16
            $nodes[\count($nodes) - 1] = new Raw(
67 16
                \is_string($content) ? \rtrim($content, $quote) : $content,
68 16
            );
69
        }
70
71 16
        foreach ($nodes as $index => $node) {
72 16
            if ($node instanceof Raw && $node->content === '') {
73 16
                unset($nodes[$index]);
74
            }
75
        }
76
77 16
        return [new Mixin(\array_values($nodes))];
78
    }
79
}
80