GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ComponentBuilder   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 10 2
A buildComponent() 0 14 1
A buildProperties() 0 15 2
A buildSubComponents() 0 16 2
A chipLine() 0 17 3
1
<?php
2
3
namespace Spatie\IcalendarGenerator\Builders;
4
5
use Spatie\IcalendarGenerator\ComponentPayload;
6
7
final class ComponentBuilder
8
{
9
    /** @var \Spatie\IcalendarGenerator\ComponentPayload */
10
    private $componentPayload;
11
12
    public function __construct(ComponentPayload $componentPayload)
13
    {
14
        $this->componentPayload = $componentPayload;
15
    }
16
17
    public function build(): string
18
    {
19
        $lines = [];
20
21
        foreach ($this->buildComponent() as $line) {
22
            $lines = array_merge($lines, $this->chipLine($line));
23
        }
24
25
        return implode("\r\n", $lines);
26
    }
27
28
    public function buildComponent(): array
29
    {
30
        $lines[] = "BEGIN:V{$this->componentPayload->getType()}";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lines was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lines = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
31
32
        $lines = array_merge(
33
            $lines,
34
            $this->buildProperties(),
35
            $this->buildSubComponents()
36
        );
37
38
        $lines[] = "END:V{$this->componentPayload->getType()}";
39
40
        return $lines;
41
    }
42
43
    private function buildProperties(): array
44
    {
45
        $lines = [];
46
47
        foreach ($this->componentPayload->getProperties() as $property) {
48
            $builder = new PropertyBuilder($property);
49
50
            $lines = array_merge(
51
                $lines,
52
                $builder->build()
53
            );
54
        }
55
56
        return $lines;
57
    }
58
59
    private function buildSubComponents(): array
60
    {
61
        $lines = [];
62
63
        /** @var \Spatie\IcalendarGenerator\Components\Component $component */
64
        foreach ($this->componentPayload->getSubComponents() as $component) {
65
            $builder = new ComponentBuilder($component->resolvePayload());
66
67
            $lines = array_merge(
68
                $lines,
69
                $builder->buildComponent()
70
            );
71
        }
72
73
        return $lines;
74
    }
75
76
    private function chipLine(string $line): array
77
    {
78
        $chippedLines = [];
79
80
        while (strlen($line) > 0) {
81
            if (strlen($line) > 75) {
82
                $chippedLines[] = mb_strcut($line, 0, 75, 'utf-8');
83
                $line = ' '.mb_strcut($line, 75, strlen($line), 'utf-8');
84
            } else {
85
                $chippedLines[] = $line;
86
87
                break;
88
            }
89
        }
90
91
        return $chippedLines;
92
    }
93
}
94