AttributesUtils   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 93
Duplicated Lines 6.45 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.23%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 1
dl 6
loc 93
ccs 49
cts 52
cp 0.9423
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 3 63 16
B merge() 3 24 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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