Passed
Push — bump-php-version ( 6d2a90...6d31db )
by Colin
32:54 queued 01:20
created

AsIsNormalizerStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 25
c 1
b 0
f 1
dl 0
loc 51
ccs 24
cts 24
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B addItem() 0 35 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
18
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
19
20
final class AsIsNormalizerStrategy implements NormalizerStrategyInterface
21
{
22
    /** @psalm-readonly-allow-private-mutation */
23
    private ListBlock $parentListBlock;
24
25
    /** @psalm-readonly-allow-private-mutation */
26
    private int $parentLevel = 1;
27
28
    /** @psalm-readonly-allow-private-mutation */
29
    private ?ListItem $lastListItem = null;
30
31 6
    public function __construct(TableOfContents $toc)
32
    {
33 6
        $this->parentListBlock = $toc;
34 6
    }
35
36 6
    public function addItem(int $level, ListItem $listItemToAdd): void
37
    {
38 6
        while ($level > $this->parentLevel) {
39
            // Descend downwards, creating new ListBlocks if needed, until we reach the correct depth
40 6
            if ($this->lastListItem === null) {
41 6
                $this->lastListItem = new ListItem($this->parentListBlock->getListData());
42 6
                $this->parentListBlock->appendChild($this->lastListItem);
0 ignored issues
show
Bug introduced by
$this->lastListItem of type null is incompatible with the type League\CommonMark\Node\Node expected by parameter $child of League\CommonMark\Node\Node::appendChild(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                $this->parentListBlock->appendChild(/** @scrutinizer ignore-type */ $this->lastListItem);
Loading history...
43
            }
44
45 6
            $newListBlock = new ListBlock($this->parentListBlock->getListData());
46 6
            $newListBlock->setStartLine($listItemToAdd->getStartLine());
47 6
            $newListBlock->setEndLine($listItemToAdd->getEndLine());
48 6
            $this->lastListItem->appendChild($newListBlock);
0 ignored issues
show
Bug introduced by
The method appendChild() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            $this->lastListItem->/** @scrutinizer ignore-call */ 
49
                                 appendChild($newListBlock);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49 6
            $this->parentListBlock = $newListBlock;
50 6
            $this->lastListItem    = null;
51
52 6
            $this->parentLevel++;
53
        }
54
55 6
        while ($level < $this->parentLevel) {
56
            // Search upwards for the previous parent list block
57 6
            $search = $this->parentListBlock;
58 6
            while ($search = $search->parent()) {
59 6
                if ($search instanceof ListBlock) {
60 6
                    $this->parentListBlock = $search;
61 6
                    break;
62
                }
63
            }
64
65 6
            $this->parentLevel--;
66
        }
67
68 6
        $this->parentListBlock->appendChild($listItemToAdd);
69
70 6
        $this->lastListItem = $listItemToAdd;
71 6
    }
72
}
73