Completed
Push — master ( 0b6d3a...3ff1a5 )
by Colin
31:14 queued 11s
created

AnonymousFootnoteRefParser::createReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 * (c) Rezo Zero / Ambroise Maupate
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\CommonMark\Extension\Footnote\Parser;
16
17
use League\CommonMark\Extension\Footnote\Node\FootnoteRef;
18
use League\CommonMark\Normalizer\SlugNormalizer;
19
use League\CommonMark\Normalizer\TextNormalizerInterface;
20
use League\CommonMark\Parser\Inline\InlineParserInterface;
21
use League\CommonMark\Parser\InlineParserContext;
22
use League\CommonMark\Reference\Reference;
23
24
final class AnonymousFootnoteRefParser implements InlineParserInterface
25
{
26
    /**
27 42
     * @var TextNormalizerInterface
28
     *
29 42
     * @psalm-readonly
30
     */
31
    private $slugNormalizer;
32 18
33
    public function __construct()
34 18
    {
35 18
        $this->slugNormalizer = new SlugNormalizer();
36 18
    }
37 18
38
    /**
39
     * {@inheritDoc}
40
     */
41 18
    public function getCharacters(): array
42
    {
43 18
        return ['^'];
44 18
    }
45 18
46 18
    public function parse(InlineParserContext $inlineContext): bool
47 18
    {
48
        $container = $inlineContext->getContainer();
49 18
        $cursor    = $inlineContext->getCursor();
50
        $nextChar  = $cursor->peek();
51
        if ($nextChar !== '[') {
52
            return false;
53
        }
54
55
        $state = $cursor->saveState();
56
57
        $m = $cursor->match('/\^\[[^\n^\]]+\]/');
58 18
        if ($m !== null) {
59
            if (\preg_match('#\^\[([^\]]+)\]#', $m, $matches) > 0) {
60 18
                $reference = $this->createReference($matches[1]);
61 18
                $container->appendChild(new FootnoteRef($reference, $matches[1]));
62 18
63
                return true;
64 18
            }
65
        }
66
67
        $cursor->restoreState($state);
0 ignored issues
show
Unused Code introduced by
The call to the method League\CommonMark\Parser\Cursor::restoreState() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
68
69
        return false;
70
    }
71
72
    /**
73
     * @psalm-pure
74
     */
75
    private function createReference(string $label): Reference
76
    {
77
        $refLabel = $this->slugNormalizer->normalize($label);
78
        $refLabel = \mb_substr($refLabel, 0, 20);
79
80
        return new Reference($refLabel, '#fn:' . $refLabel, $label);
81
    }
82
}
83