Completed
Push — master ( deae46...6c5f37 )
by Colin
01:01
created

BacktickParser::getCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Extension\CommonMark\Parser\Inline;
18
19
use League\CommonMark\Extension\CommonMark\Node\Inline\Code;
20
use League\CommonMark\Node\Inline\Text;
21
use League\CommonMark\Parser\Inline\InlineParserInterface;
22
use League\CommonMark\Parser\InlineParserContext;
23
24
final class BacktickParser implements InlineParserInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 2496
    public function getCharacters(): array
30
    {
31 2496
        return ['`'];
32
    }
33
34 150
    public function parse(InlineParserContext $inlineContext): bool
35
    {
36 150
        $cursor = $inlineContext->getCursor();
37 150
        $ticks  = $cursor->match('/^`+/');
38 150
        if ($ticks === null) {
39
            return false; // This should never happen
40
        }
41
42 150
        $currentPosition = $cursor->getPosition();
43 150
        $previousState   = $cursor->saveState();
44
45 150
        while ($matchingTicks = $cursor->match('/`+/m')) {
46 129
            if ($matchingTicks !== $ticks) {
47 24
                continue;
48
            }
49
50 126
            $code = $cursor->getSubstring($currentPosition, $cursor->getPosition() - $currentPosition - \strlen($ticks));
51
52 126
            $c = \preg_replace('/\n/m', ' ', $code) ?? '';
53
54
            if (
55 126
                ! empty($c) &&
56 126
                $c[0] === ' ' &&
57 126
                \substr($c, -1, 1) === ' ' &&
58 126
                \preg_match('/[^ ]/', $c)
59
            ) {
60 27
                $c = \substr($c, 1, -1);
61
            }
62
63 126
            $inlineContext->getContainer()->appendChild(new Code($c));
64
65 126
            return true;
66
        }
67
68
        // If we got here, we didn't match a closing backtick sequence
69 42
        $cursor->restoreState($previousState);
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...
70 42
        $inlineContext->getContainer()->appendChild(new Text($ticks));
71
72 42
        return true;
73
    }
74
}
75