Issues (33)

HeadingPermalink/HeadingPermalinkProcessor.php (1 issue)

Severity
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\HeadingPermalink;
15
16
use League\CommonMark\Environment\EnvironmentAwareInterface;
17
use League\CommonMark\Environment\EnvironmentInterface;
18
use League\CommonMark\Event\DocumentParsedEvent;
19
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
20
use League\CommonMark\Node\Block\Document;
21
use League\CommonMark\Node\StringContainerHelper;
22
use League\CommonMark\Normalizer\TextNormalizerInterface;
23
use League\Config\ConfigurationInterface;
24
25
/**
26
 * Searches the Document for Heading elements and adds HeadingPermalinks to each one
27
 */
28
final class HeadingPermalinkProcessor implements EnvironmentAwareInterface
29
{
30
    public const INSERT_BEFORE = 'before';
31
    public const INSERT_AFTER  = 'after';
32
33
    /**
34
     * @var TextNormalizerInterface
35
     *
36
     * @psalm-readonly-allow-private-mutation
37
     */
38
    private $slugNormalizer;
39
40
    /**
41
     * @var ConfigurationInterface
42
     *
43
     * @psalm-readonly-allow-private-mutation
44
     */
45
    private $config;
46
47 87
    public function setEnvironment(EnvironmentInterface $environment): void
48
    {
49 87
        $this->config         = $environment->getConfiguration();
50 87
        $this->slugNormalizer = $environment->getSlugNormalizer();
51 84
    }
52
53 84
    public function __invoke(DocumentParsedEvent $e): void
54
    {
55 84
        $min = (int) $this->config->get('heading_permalink/min_heading_level');
56 84
        $max = (int) $this->config->get('heading_permalink/max_heading_level');
57
58 84
        $slugLength = (int) $this->config->get('slug_normalizer/max_length');
59
60 84
        $walker = $e->getDocument()->walker();
61
62 84
        while ($event = $walker->next()) {
63 84
            $node = $event->getNode();
64 84
            if ($node instanceof Heading && $event->isEntering() && $node->getLevel() >= $min && $node->getLevel() <= $max) {
65 81
                $this->addHeadingLink($node, $e->getDocument(), $slugLength);
66
            }
67
        }
68 84
    }
69
70 81
    private function addHeadingLink(Heading $heading, Document $document, int $slugLength): void
0 ignored issues
show
The parameter $document is not used and could be removed. ( Ignorable by Annotation )

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

70
    private function addHeadingLink(Heading $heading, /** @scrutinizer ignore-unused */ Document $document, int $slugLength): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72 81
        $text = StringContainerHelper::getChildText($heading);
73 81
        $slug = $this->slugNormalizer->normalize($text, [
74 81
            'node' => $heading,
75 81
            'length' => $slugLength,
76
        ]);
77
78 81
        $headingLinkAnchor = new HeadingPermalink($slug);
79
80 81
        switch ($this->config->get('heading_permalink/insert')) {
81 81
            case self::INSERT_BEFORE:
82 72
                $heading->prependChild($headingLinkAnchor);
83
84 72
                return;
85 9
            case self::INSERT_AFTER:
86 9
                $heading->appendChild($headingLinkAnchor);
87
88 9
                return;
89
            default:
90
                throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
91
        }
92
    }
93
}
94