Completed
Push — master ( 57b966...17d4dc )
by Vitaly
02:09
created

VariableFixedCG::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 6

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 06.04.17 at 07:34
5
 */
6
namespace samsonframework\stringconditiontree\string;
7
8
/**
9
 * This class describes character group with next structure:
10
 * - variable length character group
11
 * - fixed length character group
12
 *
13
 * @author Vitaly Egorov <[email protected]>
14
 */
15
class VariableFixedCG extends AbstractCharacterGroup
16
{
17
    /** string Character group matching regexp pattern matching group name */
18
    const PATTERN_GROUP = 'variableFixed';
19
20
    /** string Character group matching regexp pattern */
21
    const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.VariableCG::PATTERN_REGEXP.FixedCG::PATTERN_REGEXP.')';
22
23
    /** @var VariableCG */
24
    protected $variableCG;
25
26
    /** @var FixedCG */
27
    protected $fixedCG;
28
29
    /**
30
     * @inheritdoc
31
     */
32 15
    public function __construct(string $string, int $length = 0)
33
    {
34 15
        $this->size = 2;
35
36 15
        parent::__construct($string, $length);
37
38
        // Parse internal character groups
39 15
        $this->variableCG = VariableCG::fromString($string);
40 15
        $this->fixedCG = FixedCG::fromString($string);
41 15
    }
42
43
    /**
44
     * @return bool True if character group is fixed length otherwise false
45
     */
46
    public function isFixed(): bool
47
    {
48
        return $this instanceof self;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 12 View Code Duplication
    public function compare(AbstractCharacterGroup $group): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56
        // Equal character group types - return length comparison
57 12
        if ($this->isSameType($group)) {
58
            // Variable character groups with longer length has higher priority
59 11
            return $this->compareLength($group);
60
        }
61
62
        // Fixed character group has higher priority
63 5
        if ($group->isFixed()) {
64 2
            return -1;
65
        }
66
67
        /**
68
         * VariableFixed character group has higher priority than regular
69
         * variable character group.
70
         */
71 4
        return 1;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 11
    protected function compareLength(AbstractCharacterGroup $group): int
78
    {
79
        /** @var VariableFixedCG $group */
80
81
        // Fixed CG are equal
82 11
        if (($return = $this->compareFixed($group)) === 0) {
0 ignored issues
show
Compatibility introduced by
$group of type object<samsonframework\s...AbstractCharacterGroup> is not a sub-type of object<samsonframework\s...string\VariableFixedCG>. It seems like you assume a child class of the class samsonframework\stringco...\AbstractCharacterGroup to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
83
            // Compare variable character groups
84 7
            $return = $this->variableCG->compare($group->variableCG);
0 ignored issues
show
Bug introduced by
The property variableCG does not seem to exist in samsonframework\stringco...\AbstractCharacterGroup.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
        }
86
87 11
        return $return;
88
    }
89
90
    /**
91
     * Longer fixed character group has higher priority.
92
     *
93
     * @param VariableFixedCG $group Compared character group
94
     *
95
     * @return int Comparison result
96
     */
97 11
    private function compareFixed(VariableFixedCG $group): int
98
    {
99
        // Opposite fixed CG comparison
100 11
        return $this->fixedCG->length <=> $group->fixedCG->length;
101
    }
102
}
103