Completed
Push — master ( 509144...34378c )
by Martin
07:36
created

AttributesUtils   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 103
Duplicated Lines 5.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 2
dl 6
loc 103
ccs 52
cts 55
cp 0.9455
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 3 73 17
C merge() 3 24 8

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
/*
4
 * This is part of the webuni/commonmark-attributes-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[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
namespace Webuni\CommonMark\AttributesExtension;
14
15
use League\CommonMark\Block\Element\AbstractBlock;
16
use League\CommonMark\Cursor;
17
use League\CommonMark\Inline\Element\AbstractInline;
18
use League\CommonMark\Util\RegexHelper;
19
20
class AttributesUtils
21
{
22
    private static $regexp;
23
24 5
    public static function parse(Cursor $cursor)
25
    {
26 5
        if (null === self::$regexp) {
27 1
            $regex = RegexHelper::getInstance();
0 ignored issues
show
Deprecated Code introduced by
The method League\CommonMark\Util\RegexHelper::getInstance() has been deprecated with message: Instances are no longer needed and will be removed in 0.18 or 1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
28
29 1
            self::$regexp = sprintf(
30 1
                '/^\s*([.#][_a-z0-9-]+|%s%s)(?<!})\s*/i',
31 1
                $regex->getPartialRegex(RegexHelper::ATTRIBUTENAME),
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::ATTRIBUTENAME has been deprecated with message: Use PARTIAL_ATTRIBUTENAME instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The method League\CommonMark\Util\R...lper::getPartialRegex() has been deprecated with message: Just grab the constant directly

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
32 1
                $regex->getPartialRegex(RegexHelper::ATTRIBUTEVALUESPEC)
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...per::ATTRIBUTEVALUESPEC has been deprecated with message: Use PARTIAL_ATTRIBUTEVALUESPEC instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The method League\CommonMark\Util\R...lper::getPartialRegex() has been deprecated with message: Just grab the constant directly

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
33
            );
34
        }
35
36 5
        $state = $cursor->saveState();
37 5
        $cursor->advanceToNextNonSpaceOrNewline();
38 5
        if ('{' !== $cursor->getCharacter()) {
39 4
            $cursor->restoreState($state);
40
41 4
            return [];
42
        }
43
44 5
        $cursor->advanceBy(1);
45 5
        if (':' === $cursor->getCharacter()) {
46
            $cursor->advanceBy(1);
47
        }
48
49 5
        $attributes = [];
50 5
        while ($attribute = trim($cursor->match(self::$regexp))) {
51 5
            if ('#' === $attribute[0]) {
52 3
                $attributes['id'] = substr($attribute, 1);
53
54 3
                continue;
55
            }
56
57 5
            if ('.' === $attribute[0]) {
58 5
                $attributes['class'][] = substr($attribute, 1);
59
60 5
                continue;
61
            }
62
63 1
            list($name, $value) = explode('=', $attribute, 2);
64 1
            $first = $value[0];
65 1
            $last = substr($value, -1);
66 1
            if ((('"' === $first && '"' === $last) || ("'" === $first && "'" === $last)) && strlen($value) > 1) {
67 1
                $value = substr($value, 1, -1);
68
            }
69
70 1
            if ('class' === strtolower(trim($name))) {
71 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...
72
                    $attributes['class'][] = $class;
73
                }
74
            } else {
75 1
                $attributes[trim($name)] = trim($value);
76
            }
77
        }
78
79 5
        if (null === $cursor->match('/}/')) {
80 1
            $cursor->restoreState($state);
81
82 1
            return [];
83
        }
84
85 5
        if (!count($attributes)) {
86 1
            $cursor->restoreState($state);
87
88 1
            return [];
89
        }
90
91 5
        if (isset($attributes['class'])) {
92 5
            $attributes['class'] = implode(' ', $attributes['class']);
93
        }
94
95 5
        return $attributes;
96
    }
97
98 5
    public static function merge($attributes1, $attributes2)
99
    {
100 5
        $attributes = [];
101 5
        foreach ([$attributes1, $attributes2] as $arg) {
102 5
            if ($arg instanceof AbstractBlock || $arg instanceof AbstractInline) {
103 5
                $arg = isset($arg->data['attributes']) ? $arg->data['attributes'] : [];
104
            }
105
106 5
            $arg = (array) $arg;
107 5
            if (isset($arg['class'])) {
108 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...
109 5
                    $attributes['class'][] = $class;
110
                }
111 5
                unset($arg['class']);
112
            }
113 5
            $attributes = array_merge($attributes, $arg);
114
        }
115
116 5
        if (isset($attributes['class'])) {
117 5
            $attributes['class'] = implode(' ', $attributes['class']);
118
        }
119
120 5
        return $attributes;
121
    }
122
}
123