AttributesUtils::parse()   C
last analyzed

Complexity

Conditions 16
Paths 57

Size

Total Lines 63

Duplication

Lines 3
Ratio 4.76 %

Code Coverage

Tests 34
CRAP Score 16.1365

Importance

Changes 0
Metric Value
dl 3
loc 63
ccs 34
cts 37
cp 0.9189
rs 5.5666
c 0
b 0
f 0
cc 16
nc 57
nop 1
crap 16.1365

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This is part of the webuni/commonmark-attributes-extension package.
7
 *
8
 * (c) Martin Hasoň <[email protected]>
9
 * (c) Webuni s.r.o. <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Webuni\CommonMark\AttributesExtension;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Cursor;
19
use League\CommonMark\Inline\Element\AbstractInline;
20
use League\CommonMark\Util\RegexHelper;
21
22
final class AttributesUtils
23
{
24 5
    private static $regexp = '/^\s*([.#][_a-z0-9-]+|'.RegexHelper::PARTIAL_ATTRIBUTENAME.RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC.')(?<!})\s*/i';
25
26 5
    public static function parse(Cursor $cursor): array
27 1
    {
28
        $state = $cursor->saveState();
29 1
        $cursor->advanceToNextNonSpaceOrNewline();
30 1
        if ('{' !== $cursor->getCharacter()) {
31 1
            $cursor->restoreState($state);
32 1
33
            return [];
34
        }
35
36 5
        $cursor->advanceBy(1);
37 5
        if (':' === $cursor->getCharacter()) {
38 5
            $cursor->advanceBy(1);
39 4
        }
40
41 4
        $attributes = [];
42
        while ($attribute = trim((string) $cursor->match(self::$regexp))) {
43
            if ('#' === $attribute[0]) {
44 5
                $attributes['id'] = substr($attribute, 1);
45 5
46
                continue;
47
            }
48
49 5
            if ('.' === $attribute[0]) {
50 5
                $attributes['class'][] = substr($attribute, 1);
51 5
52 3
                continue;
53
            }
54 3
55
            list($name, $value) = explode('=', $attribute, 2);
56
            $first = $value[0];
57 5
            $last = substr($value, -1);
58 5
            if ((('"' === $first && '"' === $last) || ("'" === $first && "'" === $last)) && strlen($value) > 1) {
59
                $value = substr($value, 1, -1);
60 5
            }
61
62
            if ('class' === strtolower(trim($name))) {
63 1 View Code Duplication
                foreach (array_filter(explode(' ', trim($value))) as $class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64 1
                    $attributes['class'][] = $class;
65 1
                }
66 1
            } else {
67 1
                $attributes[trim($name)] = trim($value);
68
            }
69
        }
70 1
71
        if (null === $cursor->match('/}/')) {
72
            $cursor->restoreState($state);
73
74
            return [];
75 1
        }
76
77
        if (!count($attributes)) {
78
            $cursor->restoreState($state);
79 5
80 1
            return [];
81
        }
82 1
83
        if (isset($attributes['class'])) {
84
            $attributes['class'] = implode(' ', (array) $attributes['class']);
85 5
        }
86 1
87
        return $attributes;
88 1
    }
89
90
    public static function merge($attributes1, $attributes2): array
91 5
    {
92 5
        $attributes = [];
93
        foreach ([$attributes1, $attributes2] as $arg) {
94
            if ($arg instanceof AbstractBlock || $arg instanceof AbstractInline) {
95 5
                $arg = $arg->data['attributes'] ?? [];
96
            }
97
98 5
            $arg = (array) $arg;
99
            if (isset($arg['class'])) {
100 5 View Code Duplication
                foreach (array_filter(explode(' ', trim($arg['class']))) as $class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101 5
                    $attributes['class'][] = $class;
102 5
                }
103 5
                unset($arg['class']);
104
            }
105
            $attributes = array_merge($attributes, $arg);
106 5
        }
107 5
108 5
        if (isset($attributes['class'])) {
109 5
            $attributes['class'] = implode(' ', $attributes['class']);
110
        }
111 5
112
        return $attributes;
113 5
    }
114
}
115