AsIsNormalizerStrategy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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 4
    public function __construct(TableOfContents $toc)
32
    {
33 4
        $this->parentListBlock = $toc;
34
    }
35
36 4
    public function addItem(int $level, ListItem $listItemToAdd): void
37
    {
38 4
        while ($level > $this->parentLevel) {
39
            // Descend downwards, creating new ListBlocks if needed, until we reach the correct depth
40 4
            if ($this->lastListItem === null) {
41 4
                $this->lastListItem = new ListItem($this->parentListBlock->getListData());
42 4
                $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 4
            $newListBlock = new ListBlock($this->parentListBlock->getListData());
46 4
            $newListBlock->setStartLine($listItemToAdd->getStartLine());
47 4
            $newListBlock->setEndLine($listItemToAdd->getEndLine());
48 4
            $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 4
            $this->parentListBlock = $newListBlock;
50 4
            $this->lastListItem    = null;
51
52 4
            $this->parentLevel++;
53
        }
54
55 4
        while ($level < $this->parentLevel) {
56
            // Search upwards for the previous parent list block
57 4
            $search = $this->parentListBlock;
58 4
            while ($search = $search->parent()) {
59 4
                if ($search instanceof ListBlock) {
60 4
                    $this->parentListBlock = $search;
61 4
                    break;
62
                }
63
            }
64
65 4
            $this->parentLevel--;
66
        }
67
68 4
        $this->parentListBlock->appendChild($listItemToAdd);
69
70 4
        $this->lastListItem = $listItemToAdd;
71
    }
72
}
73