Completed
Push — 1.5 ( 2fb592...563c17 )
by Colin
01:07
created

AttributesListener   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 10
dl 0
loc 114
ccs 56
cts 56
cp 1
rs 9.28
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C processDocument() 0 36 16
C findTargetAndDirection() 0 39 13
A getPrevious() 0 10 4
A getNext() 0 10 4
A isAttributesNode() 0 4 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) 2015 Martin Hasoň <[email protected]>
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\Attributes\Event;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Block\Element\FencedCode;
19
use League\CommonMark\Block\Element\ListBlock;
20
use League\CommonMark\Block\Element\ListItem;
21
use League\CommonMark\Event\DocumentParsedEvent;
22
use League\CommonMark\Extension\Attributes\Node\Attributes;
23
use League\CommonMark\Extension\Attributes\Node\AttributesInline;
24
use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
25
use League\CommonMark\Inline\Element\AbstractInline;
26
use League\CommonMark\Node\Node;
27
28
final class AttributesListener
29
{
30
    private const DIRECTION_PREFIX = 'prefix';
31
    private const DIRECTION_SUFFIX = 'suffix';
32
33 18
    public function processDocument(DocumentParsedEvent $event): void
34
    {
35 18
        $walker = $event->getDocument()->walker();
36 18
        while ($event = $walker->next()) {
37 18
            $node = $event->getNode();
38 18
            if (!$node instanceof AttributesInline && ($event->isEntering() || !$node instanceof Attributes)) {
39 18
                continue;
40
            }
41
42 18
            [$target, $direction] = self::findTargetAndDirection($node);
0 ignored issues
show
Bug introduced by
The variable $target does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $direction does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
44 18
            if ($target instanceof AbstractBlock || $target instanceof AbstractInline) {
45 18
                $parent = $target->parent();
46 18
                if ($parent instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) {
47 3
                    $target = $parent;
48
                }
49
50 18
                if ($direction === self::DIRECTION_SUFFIX) {
51 18
                    $attributes = AttributesHelper::mergeAttributes($target, $node->getAttributes());
52
                } else {
53 9
                    $attributes = AttributesHelper::mergeAttributes($node->getAttributes(), $target);
54
                }
55
56 18
                $target->data['attributes'] = $attributes;
57
            }
58
59 18
            if ($node instanceof AbstractBlock && $node->endsWithBlankLine() && $node->next() && $node->previous()) {
60 9
                $previous = $node->previous();
61 9
                if ($previous instanceof AbstractBlock) {
62 9
                    $previous->setLastLineBlank(true);
63
                }
64
            }
65
66 18
            $node->detach();
67
        }
68 18
    }
69
70
    /**
71
     * @param Node $node
72
     *
73
     * @return array<Node|string|null>
74
     */
75 18
    private static function findTargetAndDirection(Node $node): array
76
    {
77 18
        $target = null;
78 18
        $direction = null;
79 18
        $previous = $next = $node;
80 18
        while (true) {
81 18
            $previous = self::getPrevious($previous);
82 18
            $next = self::getNext($next);
83
84 18
            if ($previous === null && $next === null) {
85 15
                if (!$node->parent() instanceof FencedCode) {
86 12
                    $target = $node->parent();
87 12
                    $direction = self::DIRECTION_SUFFIX;
88
                }
89
90 15
                break;
91
            }
92
93 18
            if ($node instanceof AttributesInline && ($previous === null || ($previous instanceof AbstractInline && $node->isBlock()))) {
94 15
                continue;
95
            }
96
97 18
            if ($previous !== null && !self::isAttributesNode($previous)) {
98 18
                $target = $previous;
99 18
                $direction = self::DIRECTION_SUFFIX;
100
101 18
                break;
102
            }
103
104 9
            if ($next !== null && !self::isAttributesNode($next)) {
105 9
                $target = $next;
106 9
                $direction = self::DIRECTION_PREFIX;
107
108 9
                break;
109
            }
110
        }
111
112 18
        return [$target, $direction];
113
    }
114
115 18
    private static function getPrevious(?Node $node = null): ?Node
116
    {
117 18
        $previous = $node instanceof Node ? $node->previous() : null;
118
119 18
        if ($previous instanceof AbstractBlock && $previous->endsWithBlankLine()) {
120 6
            $previous = null;
121
        }
122
123 18
        return $previous;
124
    }
125
126 18
    private static function getNext(?Node $node = null): ?Node
127
    {
128 18
        $next = $node instanceof Node ? $node->next() : null;
129
130 18
        if ($node instanceof AbstractBlock && $node->endsWithBlankLine()) {
131 9
            $next = null;
132
        }
133
134 18
        return $next;
135
    }
136
137 18
    private static function isAttributesNode(Node $node): bool
138
    {
139 18
        return $node instanceof Attributes || $node instanceof AttributesInline;
140
    }
141
}
142