Completed
Push — master ( e9cc16...40005e )
by Vitaly
02:15
created

FixedVariableCG::compareFixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
 * - fixed length character group
11
 * - variable length character group
12
 *
13
 * @author Vitaly Egorov <[email protected]>
14
 */
15
class FixedVariableCG extends AbstractCG
16
{
17
    /** string Character group matching regexp pattern matching group name */
18
    const PATTERN_GROUP = 'fixedVariable';
19
20
    /** string Character group matching regexp pattern */
21
    const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.FixedCG::PATTERN_REGEXP.VariableCG::PATTERN_REGEXP.')';
22
23
    /** @var VariableCG */
24
    protected $variableCG;
25
26
    /** @var FixedCG */
27
    protected $fixedCG;
28
29
    /**
30
     * @inheritdoc
31
     */
32 20 View Code Duplication
    public function __construct(string $string, int $length = 0)
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...
33
    {
34 20
        parent::__construct($string, $length);
35
36
        // Parse internal character groups
37 20
        $this->fixedCG = FixedCG::fromString($string);
38 20
        $this->variableCG = VariableCG::fromString($string);
39 20
    }
40
41
    /**
42
     * Get variable character group common prefix, if exists then
43
     * append fixed character group prefix.
44
     *
45
     * @inheritdoc
46
     */
47 2
    public function getCommonPrefix(AbstractCG $group): string
48
    {
49
        // Get common prefix as concatenation of variable and fixed character groups common prefixes
50 2
        if ($this->isSameType($group)) {
51 2
            $prefix = $this->fixedCG->getCommonPrefix($group->fixedCG);
0 ignored issues
show
Bug introduced by
The property fixedCG does not seem to exist in samsonframework\stringco...ntree\string\AbstractCG.

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...
52
53 2
            if ($prefix === $this->fixedCG->getString() && $prefix === $group->fixedCG->getString()) {
54 2
                return $prefix . $this->variableCG->getCommonPrefix($group->variableCG);
0 ignored issues
show
Bug introduced by
The property variableCG does not seem to exist in samsonframework\stringco...ntree\string\AbstractCG.

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...
55
            }
56
57 2
            return $prefix;
58
        }
59
60
        // Compare only first variable character groups
61 1
        if ($group instanceof FixedCG) {
62 1
            return $this->fixedCG->getCommonPrefix($group);
63
        }
64
65 1
        return '';
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 14
    public function compare(AbstractCG $group): int
72
    {
73
        // Compare with fixed character group
74 14
        if ($group instanceof FixedCG) {
75 2
            return $this->fixedCG->compare($group);
76
        }
77
78
        // Always FixedVariable character group has higher priority over variable character group
79
        // FixedVariable character group has higher priority over VariableFixed character group
80 13
        if ($group instanceof VariableCG || $group instanceof VariableFixedCG) {
81 4
            return 1;
82
        }
83
84 10
        if ($this->isSameType($group)) {
85 10
            return $this->compareLength($group);
86
        }
87
88
        return 0;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     * @param AbstractCG|FixedVariableCG|FixedVariableCG|VariableFixedCG $group
94
     */
95 10 View Code Duplication
    protected function compareLength(AbstractCG $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...
96
    {
97
        // Fixed CG are equal
98 10
        if (($return = $this->compareFixed($group)) === 0) {
0 ignored issues
show
Compatibility introduced by
$group of type object<samsonframework\s...tree\string\AbstractCG> is not a sub-type of object<samsonframework\s...string\FixedVariableCG>. It seems like you assume a child class of the class samsonframework\stringco...ntree\string\AbstractCG 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...
99
            // Compare variable character groups
100 5
            $return = $this->variableCG->compare($group->variableCG);
0 ignored issues
show
Bug introduced by
The property variableCG does not seem to exist in samsonframework\stringco...ntree\string\AbstractCG.

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...
101
        }
102
103 10
        return $return;
104
    }
105
106
    /**
107
     * Longer fixed character group has higher priority.
108
     *
109
     * @param FixedVariableCG $group Compared character group
110
     *
111
     * @return int Comparison result
112
     */
113 10
    private function compareFixed(FixedVariableCG $group): int
114
    {
115
        // Opposite fixed CG comparison
116 10
        return $this->fixedCG->length <=> $group->fixedCG->length;
0 ignored issues
show
Bug introduced by
The property length cannot be accessed from this context as it is declared protected in class samsonframework\stringco...ntree\string\AbstractCG.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
117
    }
118
}
119