Completed
Push — master ( 235985...1f7999 )
by Colin
01:38 queued 10s
created

AttributesListener::isAttributesNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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) 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\Event\DocumentParsedEvent;
18
use League\CommonMark\Extension\Attributes\Node\Attributes;
19
use League\CommonMark\Extension\Attributes\Node\AttributesInline;
20
use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
21
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
22
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
23
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
24
use League\CommonMark\Node\Block\AbstractBlock;
25
use League\CommonMark\Node\Inline\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...
Documentation introduced by
$node is of type object<League\CommonMark\Node\Node>, but the function expects a object<League\CommonMark...\Node\AttributesInline>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
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 15
                    $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
            $node->detach();
60
        }
61 18
    }
62
63
    /**
64
     * @param Attributes|AttributesInline $node
65
     *
66
     * @return array<Node|string|null>
67
     */
68 18
    private static function findTargetAndDirection($node): array
69
    {
70 18
        $target    = null;
71 18
        $direction = null;
72 18
        $previous  = $next = $node;
73 18
        while (true) {
74 18
            $previous = self::getPrevious($previous);
75 18
            $next     = self::getNext($next);
76
77 18
            if ($previous === null && $next === null) {
78 12
                if (! $node->parent() instanceof FencedCode) {
79 12
                    $target    = $node->parent();
80 12
                    $direction = self::DIRECTION_SUFFIX;
81
                }
82
83 12
                break;
84
            }
85
86 18
            if ($node instanceof AttributesInline && ($previous === null || ($previous instanceof AbstractInline && $node->isBlock()))) {
87 12
                continue;
88
            }
89
90 18
            if ($previous !== null && ! self::isAttributesNode($previous)) {
91 15
                $target    = $previous;
92 15
                $direction = self::DIRECTION_SUFFIX;
93
94 15
                break;
95
            }
96
97 9
            if ($next !== null && ! self::isAttributesNode($next)) {
98 9
                $target    = $next;
99 9
                $direction = self::DIRECTION_PREFIX;
100
101 9
                break;
102
            }
103
        }
104
105 18
        return [$target, $direction];
106
    }
107
108
    /**
109
     * Get any previous block (sibling or parent) this might apply to
110
     */
111 18
    private static function getPrevious(?Node $node = null): ?Node
112
    {
113 18
        if ($node instanceof Attributes) {
114 15
            if ($node->getTarget() === Attributes::TARGET_NEXT) {
115 9
                return null;
116
            }
117
118 12
            if ($node->getTarget() === Attributes::TARGET_PARENT) {
119 3
                return $node->parent();
120
            }
121
        }
122
123 15
        return $node instanceof Node ? $node->previous() : null;
124
    }
125
126
    /**
127
     * Get any previous block (sibling or parent) this might apply to
128
     */
129 18
    private static function getNext(?Node $node = null): ?Node
130
    {
131 18
        if ($node instanceof Attributes && $node->getTarget() !== Attributes::TARGET_NEXT) {
132 12
            return null;
133
        }
134
135 18
        return $node instanceof Node ? $node->next() : null;
136
    }
137
138 18
    private static function isAttributesNode(Node $node): bool
139
    {
140 18
        return $node instanceof Attributes || $node instanceof AttributesInline;
141
    }
142
}
143