Completed
Push — 1.5 ( fb52dd...2fb592 )
by Colin
02:48
created

AttributesParserTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 1
dl 0
loc 72
ccs 33
cts 36
cp 0.9167
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C parseAttributes() 0 64 16
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\Parser;
16
17
use League\CommonMark\Cursor;
18
use League\CommonMark\Util\RegexHelper;
19
20
trait AttributesParserTrait
21
{
22
    /**
23
     * @param Cursor $cursor
24
     *
25
     * @return array<string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
26
     */
27 18
    private function parseAttributes(Cursor $cursor): array
28
    {
29 18
        $state = $cursor->saveState();
30 18
        $cursor->advanceToNextNonSpaceOrNewline();
31 18
        if ($cursor->getCharacter() !== '{') {
32 15
            $cursor->restoreState($state);
33
34 15
            return [];
35
        }
36
37 18
        $cursor->advanceBy(1);
38 18
        if ($cursor->getCharacter() === ':') {
39
            $cursor->advanceBy(1);
40
        }
41
42 18
        $attributes = [];
43 18
        $regex = '/^\s*([.#][_a-z0-9-]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . ')(?<!})\s*/i';
44 18
        while ($attribute = \trim((string) $cursor->match($regex))) {
45 18
            if ($attribute[0] === '#') {
46 12
                $attributes['id'] = \substr($attribute, 1);
47
48 12
                continue;
49
            }
50
51 18
            if ($attribute[0] === '.') {
52 18
                $attributes['class'][] = \substr($attribute, 1);
53
54 18
                continue;
55
            }
56
57 6
            [$name, $value] = \explode('=', $attribute, 2);
0 ignored issues
show
Bug introduced by
The variable $name 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...
Bug introduced by
The variable $value 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...
58 6
            $first = $value[0];
59 6
            $last = \substr($value, -1);
60 6
            if ((($first === '"' && $last === '"') || ($first === "'" && $last === "'")) && \strlen($value) > 1) {
61 6
                $value = \substr($value, 1, -1);
62
            }
63
64 6
            if (\strtolower(\trim($name)) === 'class') {
65
                foreach (\array_filter(\explode(' ', \trim($value))) as $class) {
66
                    $attributes['class'][] = $class;
67
                }
68
            } else {
69 6
                $attributes[trim($name)] = trim($value);
70
            }
71
        }
72
73 18
        if ($cursor->match('/}/') === null) {
74 3
            $cursor->restoreState($state);
75
76 3
            return [];
77
        }
78
79 18
        if ($attributes === []) {
80 3
            $cursor->restoreState($state);
81
82 3
            return [];
83
        }
84
85 18
        if (isset($attributes['class'])) {
86 18
            $attributes['class'] = \implode(' ', (array) $attributes['class']);
87
        }
88
89 18
        return $attributes;
90
    }
91
}
92