Passed
Pull Request — main (#1074)
by
unknown
02:24
created

TableOfContentsGenerator::generate()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8.0036

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 12
nop 1
dl 0
loc 51
ccs 25
cts 26
cp 0.9615
crap 8.0036
rs 8.4444
c 0
b 0
f 0

How to fix   Long Method   

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
/*
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;
15
16
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
19
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
20
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
21
use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
22
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
23
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
24
use League\CommonMark\Extension\TableOfContents\Normalizer\AsIsNormalizerStrategy;
25
use League\CommonMark\Extension\TableOfContents\Normalizer\FlatNormalizerStrategy;
26
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
27
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
28
use League\CommonMark\Node\Block\Document;
29
use League\CommonMark\Node\Inline\Text;
30
use League\CommonMark\Node\NodeIterator;
31
use League\CommonMark\Node\RawMarkupContainerInterface;
32
use League\CommonMark\Node\StringContainerHelper;
33
use League\Config\Exception\InvalidConfigurationException;
34
35
final class TableOfContentsGenerator implements TableOfContentsGeneratorInterface
36
{
37
    public const STYLE_BULLET  = ListBlock::TYPE_BULLET;
38
    public const STYLE_ORDERED = ListBlock::TYPE_ORDERED;
39
40
    public const NORMALIZE_DISABLED = 'as-is';
41
    public const NORMALIZE_RELATIVE = 'relative';
42
    public const NORMALIZE_FLAT     = 'flat';
43
44
    /** @psalm-readonly */
45
    private string $style;
46
47
    /** @psalm-readonly */
48
    private string $normalizationStrategy;
49
50
    /** @psalm-readonly */
51
    private int $minHeadingLevel;
52
53
    /** @psalm-readonly */
54
    private int $maxHeadingLevel;
55
56
    /** @psalm-readonly */
57
    private string $fragmentPrefix;
58
59
    /** @psalm-readonly */
60
    private string $label;
61
62 70
    public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel, string $fragmentPrefix, string $label)
63
    {
64 70
        $this->style                 = $style;
65 70
        $this->normalizationStrategy = $normalizationStrategy;
66 70
        $this->minHeadingLevel       = $minHeadingLevel;
67 70
        $this->maxHeadingLevel       = $maxHeadingLevel;
68 70
        $this->fragmentPrefix        = $fragmentPrefix;
69 70
        $this->label                 = $label;
70
71 70
        if ($fragmentPrefix !== '') {
72 70
            $this->fragmentPrefix .= '-';
73
        }
74
    }
75
76 70
    public function generate(Document $document): ?TableOfContents
77
    {
78 70
        $toc = $this->createToc($document);
79
80 70
        $normalizer = $this->getNormalizer($toc);
81
82 70
        $firstHeading = null;
83
84 70
        foreach ($this->getHeadingLinks($document) as $headingLink) {
85 62
            $heading = $headingLink->parent();
86
            // Make sure this is actually tied to a heading
87 62
            if (! $heading instanceof Heading) {
88
                continue;
89
            }
90
91
            // Skip any headings outside the configured min/max levels
92 62
            if ($heading->getLevel() < $this->minHeadingLevel || $heading->getLevel() > $this->maxHeadingLevel) {
93 4
                continue;
94
            }
95
96
            // Keep track of the first heading we see - we might need this later
97 62
            $firstHeading ??= $heading;
98
99
            // Keep track of the start and end lines
100 62
            $toc->setStartLine($firstHeading->getStartLine());
101 62
            $toc->setEndLine($heading->getEndLine());
102
103
            // Create the new link
104 62
            $link = new Link('#' . $this->fragmentPrefix . $headingLink->getSlug(), StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]));
105
106 62
            $listItem = new ListItem($toc->getListData());
107 62
            $listItem->setStartLine($heading->getStartLine());
108 62
            $listItem->setEndLine($heading->getEndLine());
109 62
            $listItem->appendChild($link);
110
111
            // Add it to the correct place
112 62
            $normalizer->addItem($heading->getLevel(), $listItem);
113
        }
114
115
        // Don't add the TOC if no headings were present
116 70
        if (! $toc->hasChildren() || $firstHeading === null) {
117 8
            return null;
118
        }
119
120 62
        if ($this->label !== '') {
121 4
            $label = new Strong();
122 4
            $label->appendChild(new Text($this->label));
123 4
            $toc->prependChild($label);
124
        }
125
126 62
        return $toc;
127
    }
128
129 70
    private function createToc(Document $document): TableOfContents
130
    {
131 70
        $listData = new ListData();
132
133 70
        if ($this->style === self::STYLE_BULLET) {
134 66
            $listData->type = ListBlock::TYPE_BULLET;
135 4
        } elseif ($this->style === self::STYLE_ORDERED) {
136 4
            $listData->type = ListBlock::TYPE_ORDERED;
137
        } else {
138
            throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style));
139
        }
140
141 70
        $toc = new TableOfContents($listData);
142
143 70
        $toc->setStartLine($document->getStartLine());
144 70
        $toc->setEndLine($document->getEndLine());
145
146 70
        return $toc;
147
    }
148
149
    /**
150
     * @return iterable<HeadingPermalink>
151
     */
152 70
    private function getHeadingLinks(Document $document): iterable
153
    {
154 70
        foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
155 70
            if (! $node instanceof Heading) {
156 70
                continue;
157
            }
158
159 62
            foreach ($node->children() as $child) {
160 62
                if ($child instanceof HeadingPermalink) {
161 62
                    yield $child;
162
                }
163
            }
164
        }
165
    }
166
167 70
    private function getNormalizer(TableOfContents $toc): NormalizerStrategyInterface
168
    {
169 70
        switch ($this->normalizationStrategy) {
170 35
            case self::NORMALIZE_DISABLED:
171 4
                return new AsIsNormalizerStrategy($toc);
172 33
            case self::NORMALIZE_RELATIVE:
173 62
                return new RelativeNormalizerStrategy($toc);
174 2
            case self::NORMALIZE_FLAT:
175 4
                return new FlatNormalizerStrategy($toc);
176
            default:
177
                throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy));
178
        }
179
    }
180
}
181