Passed
Push — 2.0 ( 989696...a89474 )
by Colin
06:29 queued 03:39
created

ReplaceUnpairedQuotesListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 4
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\SmartPunct;
15
16
use League\CommonMark\Event\DocumentParsedEvent;
17
use League\CommonMark\Node\Inline\AdjacentTextMerger;
18
use League\CommonMark\Node\Inline\Text;
19
use League\CommonMark\Node\Query;
20
21
/**
22
 * Identifies any lingering Quote nodes that were missing pairs and converts them into Text nodes
23
 */
24
final class ReplaceUnpairedQuotesListener
25
{
26 66
    public function __invoke(DocumentParsedEvent $event): void
27
    {
28 66
        $query = (new Query())->where(Query::type(Quote::class));
29 66
        foreach ($query->findAll($event->getDocument()) as $quote) {
30
            \assert($quote instanceof Quote);
31
32 48
            $literal = $quote->getLiteral();
33 48
            if ($literal === Quote::SINGLE_QUOTE) {
34 15
                $literal = Quote::SINGLE_QUOTE_CLOSER;
35 39
            } elseif ($literal === Quote::DOUBLE_QUOTE) {
36 9
                $literal = Quote::DOUBLE_QUOTE_OPENER;
37
            }
38
39 48
            $quote->replaceWith($new = new Text($literal));
40 48
            AdjacentTextMerger::mergeWithDirectlyAdjacentNodes($new);
41
        }
42 66
    }
43
}
44