Passed
Push — master ( 0d07bf...dd4616 )
by Steve
35s
created

ChangeTextGenerator::getChanged()   F

Complexity

Conditions 14
Paths 1604

Size

Total Lines 66
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 14.6815

Importance

Changes 0
Metric Value
cc 14
eloc 32
nc 1604
nop 1
dl 0
loc 66
ccs 28
cts 33
cp 0.8485
crap 14.6815
rs 2.1
c 0
b 0
f 0

How to fix   Long Method    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
 * (c) Steve Nebes <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\DaisyDiff\Html\Ancestor;
12
13
use SN\DaisyDiff\Html\Ancestor\TagToString\TagToStringFactory;
14
use SN\DaisyDiff\Html\ChangeText;
15
use SN\DaisyDiff\Html\Dom\TagNode;
16
use SN\DaisyDiff\Html\Modification\HtmlLayoutChange;
17
use SN\DaisyDiff\RangeDifferencer\RangeDifference;
18
19
/**
20
 * ChangeTextGenerator
21
 */
22
class ChangeTextGenerator
23
{
24
    /** @var HtmlLayoutChange[] */
25
    private $htmlLayoutChanges = [];
26
27
    /** @var AncestorComparator */
28
    private $ancestorComparator;
29
30
    /** @var AncestorComparator */
31
    private $other;
32
33
    /** @var TagToStringFactory */
34
    private $factory;
35
36
    /**
37
     * @param AncestorComparator $ancestorComparator
38
     * @param AncestorComparator $other
39
     */
40 7
    public function __construct(AncestorComparator $ancestorComparator, AncestorComparator $other)
41
    {
42 7
        $this->ancestorComparator = $ancestorComparator;
43 7
        $this->other = $other;
44 7
        $this->factory = new TagToStringFactory();
45 7
    }
46
47
    /**
48
     * @param RangeDifference[] $differences
49
     * @return ChangeText
50
     */
51 6
    public function getChanged(array $differences): ChangeText
52
    {
53 6
        $text = new ChangeText();
54 6
        $rootListOpened = false;
55
56 6
        if (\count($differences) > 1) {
57
            $text->startElement('ul', ['class' => 'changelist']);
58
            $rootListOpened = true;
59
        }
60
61 6
        for ($j = 0, $jMax = \count($differences); $j < $jMax; $j++) {
62
            /** @var RangeDifference $d */
63 6
            $d = $differences[$j];
64 6
            $lvl1ListOpened = false;
65
66 6
            if ($rootListOpened) {
67
                $text->startElement('li');
68
            }
69
70 6
            if ($d->getLeftLength() + $d->getRightLength() > 1) {
71 5
                $text->startElement('ul', ['class' => 'changelist']);
72 5
                $lvl1ListOpened = true;
73
            }
74
75
            // Left are the old ones.
76 6
            for ($i = $d->getLeftStart(), $iMax = $d->getLeftEnd(); $i < $iMax; $i++) {
77 5
                if ($lvl1ListOpened) {
78 5
                    $text->startElement('li');
79
                }
80
81
                // Add a bullet for an old tag.
82 5
                $this->addTagOld($text, $this->other->getAncestor($i));
83
84 5
                if ($lvl1ListOpened) {
85 5
                    $text->endElement('li');
86
                }
87
            }
88
89
            // Right are the new ones.
90 6
            for ($i = $d->getRightStart(), $iMax = $d->getRightEnd(); $i < $iMax; $i++) {
91 6
                if ($lvl1ListOpened) {
92 5
                    $text->startElement('li');
93
                }
94
95
                // Add a bullet for an old tag.
96 6
                $this->addTagNew($text, $this->getAncestor($i));
97
98 6
                if ($lvl1ListOpened) {
99 5
                    $text->endElement('li');
100
                }
101
            }
102
103 6
            if ($lvl1ListOpened) {
104 5
                $text->endElement('ul');
105
            }
106
107 6
            if ($rootListOpened) {
108
                $text->endElement('li');
109
            }
110
        }
111
112 6
        if ($rootListOpened) {
113
            $text->endElement('ul');
114
        }
115
116 6
        return $text;
117
    }
118
119
    /**
120
     * @param ChangeText $text
121
     * @param TagNode    $ancestor
122
     */
123 5
    private function addTagOld(ChangeText $text, TagNode $ancestor): void
124
    {
125 5
        $tagToString = $this->factory->create($ancestor);
126 5
        $tagToString->getRemovedDescription($text);
127 5
        $this->htmlLayoutChanges[] = $tagToString->getHtmlLayoutChange();
128 5
    }
129
130
    /**
131
     * @param ChangeText $text
132
     * @param TagNode    $ancestor
133
     */
134 6
    private function addTagNew(ChangeText $text, TagNode $ancestor): void
135
    {
136 6
        $tagToString = $this->factory->create($ancestor);
137 6
        $tagToString->getAddedDescription($text);
138 6
        $this->htmlLayoutChanges[] = $tagToString->getHtmlLayoutChange();
139 6
    }
140
141
    /**
142
     * @param int $index
143
     * @return TagNode
144
     */
145 6
    private function getAncestor(int $index): TagNode
146
    {
147 6
        return $this->ancestorComparator->getAncestor($index);
148
    }
149
150
    /**
151
     * @return HtmlLayoutChange[]
152
     */
153 6
    public function getHtmlLayoutChanges(): array
154
    {
155 6
        return $this->htmlLayoutChanges;
156
    }
157
}
158