Completed
Push — 1.5 ( 7c783c...7cd09e )
by Colin
01:28
created

HeadingPermalinkProcessor::ensureUnique()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\HeadingPermalink;
13
14
use League\CommonMark\Block\Element\Document;
15
use League\CommonMark\Block\Element\Heading;
16
use League\CommonMark\Event\DocumentParsedEvent;
17
use League\CommonMark\Exception\InvalidOptionException;
18
use League\CommonMark\Extension\HeadingPermalink\Slug\SlugGeneratorInterface as DeprecatedSlugGeneratorInterface;
19
use League\CommonMark\Inline\Element\Code;
20
use League\CommonMark\Inline\Element\Text;
21
use League\CommonMark\Node\Node;
22
use League\CommonMark\Normalizer\SlugNormalizer;
23
use League\CommonMark\Normalizer\TextNormalizerInterface;
24
use League\CommonMark\Util\ConfigurationAwareInterface;
25
use League\CommonMark\Util\ConfigurationInterface;
26
27
/**
28
 * Searches the Document for Heading elements and adds HeadingPermalinks to each one
29
 */
30
final class HeadingPermalinkProcessor implements ConfigurationAwareInterface
31
{
32
    const INSERT_BEFORE = 'before';
33
    const INSERT_AFTER = 'after';
34
35
    /** @var TextNormalizerInterface|DeprecatedSlugGeneratorInterface */
36
    private $slugNormalizer;
37
38
    /** @var ConfigurationInterface */
39
    private $config;
40
41
    /**
42
     * @param TextNormalizerInterface|DeprecatedSlugGeneratorInterface|null $slugNormalizer
43
     */
44 87
    public function __construct($slugNormalizer = null)
45
    {
46 87
        if ($slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
47
            @trigger_error(sprintf('Passing a %s into the %s constructor is deprecated; use a %s instead', DeprecatedSlugGeneratorInterface::class, self::class, TextNormalizerInterface::class), E_USER_DEPRECATED);
48
        }
49
50 87
        $this->slugNormalizer = $slugNormalizer ?? new SlugNormalizer();
51 87
    }
52
53 87
    public function setConfiguration(ConfigurationInterface $configuration)
54
    {
55 87
        $this->config = $configuration;
56 87
    }
57
58 87
    public function __invoke(DocumentParsedEvent $e): void
59
    {
60 87
        $this->useNormalizerFromConfigurationIfProvided();
61
62 84
        $walker = $e->getDocument()->walker();
63
64 84
        while ($event = $walker->next()) {
65 84
            $node = $event->getNode();
66 84
            if ($node instanceof Heading && $event->isEntering()) {
67 81
                $this->addHeadingLink($node, $e->getDocument());
68
            }
69
        }
70 81
    }
71
72 87
    private function useNormalizerFromConfigurationIfProvided(): void
73
    {
74 87
        $generator = $this->config->get('heading_permalink/slug_normalizer');
75 87
        if ($generator === null) {
76 81
            return;
77
        }
78
79 6
        if (!($generator instanceof DeprecatedSlugGeneratorInterface || $generator instanceof TextNormalizerInterface)) {
80 3
            throw new InvalidOptionException('The heading_permalink/slug_normalizer option must be an instance of ' . TextNormalizerInterface::class);
81
        }
82
83 3
        $this->slugNormalizer = $generator;
84 3
    }
85
86 81
    private function addHeadingLink(Heading $heading, Document $document): void
87
    {
88 81
        $text = $this->getChildText($heading);
0 ignored issues
show
Deprecated Code introduced by
The method League\CommonMark\Extens...ocessor::getChildText() has been deprecated with message: Not needed in 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89 81
        if ($this->slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
90
            $slug = $this->slugNormalizer->createSlug($text);
91
        } else {
92 81
            $slug = $this->slugNormalizer->normalize($text, $heading);
93
        }
94
95 81
        $slug = $this->ensureUnique($slug, $document);
96
97 81
        $headingLinkAnchor = new HeadingPermalink($slug);
98
99 81
        switch ($this->config->get('heading_permalink/insert', 'before')) {
100 81
            case self::INSERT_BEFORE:
101 69
                $heading->prependChild($headingLinkAnchor);
102
103 69
                return;
104 12
            case self::INSERT_AFTER:
105 9
                $heading->appendChild($headingLinkAnchor);
106
107 9
                return;
108
            default:
109 3
                throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
110
        }
111
    }
112
113
    /**
114
     * @deprecated Not needed in 2.0
115
     */
116 81
    private function getChildText(Node $node): string
117
    {
118 81
        $text = '';
119
120 81
        $walker = $node->walker();
121 81
        while ($event = $walker->next()) {
122 81
            if ($event->isEntering() && (($child = $event->getNode()) instanceof Text || $child instanceof Code)) {
123 81
                $text .= $child->getContent();
124
            }
125
        }
126
127 81
        return $text;
128
    }
129
130 81
    private function ensureUnique(string $proposed, Document $document): string
131
    {
132
        // Quick path, it's a unique ID
133 81
        if (!in_array($proposed, $document->data['heading_ids'] ?? [])) {
134 81
            $document->data['heading_ids'][] = $proposed;
135
136 81
            return $proposed;
137
        }
138
139 9
        $extension = 0;
140
        do {
141 9
            ++$extension;
142 9
        } while (in_array("$proposed-$extension", $document->data['heading_ids'] ?? []));
143
144 9
        $document->data['heading_ids'][] = "$proposed-$extension";
145
146 9
        return "$proposed-$extension";
147
    }
148
}
149