Completed
Push — master ( 33a6e9...f57b25 )
by Vitaly
03:15 queued 01:15
created

AbstractCG::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 4

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
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:28
5
 */
6
namespace samsonframework\stringconditiontree\string;
7
8
/**
9
 * This class describes string structure character group.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
abstract class AbstractCG
14
{
15
    /** string Character group matching regexp pattern matching group name */
16
    const PATTERN_GROUP = '';
17
18
    /** string Regular expression matching character group */
19
    const PATTERN_REGEXP = '';
20
21
    /** string Character group matching regexp pattern */
22
    const PATTERN = '';
23
24
    /** @var int Character group length */
25
    public $length;
26
27
    /** @var string Character group string */
28
    public $string;
29
30
    /** @var int Character group size */
31
    protected $size = 1;
32
33
    /**
34
     * AbstractCharacterGroup constructor.
35
     *
36
     * @param string $string Character group string
37
     * @param int    $length Character group length
38
     */
39 21
    public function __construct(string $string, int $length = 0)
40
    {
41 21
        $this->string = $string;
42 21
        $this->length = $length;
43 21
    }
44
45
    /**
46
     * Create character group from input string.
47
     *
48
     * @param string $input Input string
49
     *
50
     * @return null|AbstractCG|FixedCG|VariableCG Character group instance
51
     */
52 17
    public static function fromString(string &$input)
53
    {
54 17
        if (preg_match('/^'.static::PATTERN.'/', $input, $matches)) {
55
            // Replace only first occurrence of character group
56 17
            if (($pos = strpos($input, $matches[0])) !== false) {
57 17
                $input = substr_replace($input, '', $pos, strlen($matches[0]));
58
59 17
                $className = static::class;
60 17
                return new $className($matches[static::PATTERN_GROUP], strlen($matches[static::PATTERN_GROUP]));
61
            }
62
        }
63
64 15
        return null;
65
    }
66
67
    /**
68
     * @return bool True if character group is variable length otherwise false
69
     */
70 1
    public function isVariable(): bool
71
    {
72 1
        return $this instanceof VariableCG;
73
    }
74
75
    /**
76
     * Compare character groups.
77
     *
78
     * @param AbstractCG $group Compared character group
79
     *
80
     * @return int -1, 0, 1 Lower, equal, higher
81
     */
82 13 View Code Duplication
    public function compare(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...
83
    {
84
        // Equal character group types - return length comparison
85 13
        if ($this->isSameType($group)) {
86
            // Variable character groups with longer length has higher priority
87 12
            return $this->compareLength($group);
88
        }
89
90
        /**
91
         * Character groups are different:
92
         * Fixed character groups has higher priority,
93
         * variable character groups has lower priority
94
         */
95 6
        return $this->isFixed() ? 1 : -1;
96
    }
97
98
    /**
99
     * Check if compared character group has same type.
100
     *
101
     * @param AbstractCG $group Compared character group
102
     *
103
     * @return bool True if character group has same type otherwise false
104
     */
105 18
    public function isSameType(AbstractCG $group): bool
106
    {
107 18
        return get_class($group) === get_class($this);
108
    }
109
110
    /**
111
     * Compare this character group length to compared character group length.
112
     *
113
     * @param AbstractCG $group Compared character group
114
     *
115
     * @return int -1, 0, 1 Character groups comparison result
116
     */
117
    abstract protected function compareLength(AbstractCG $group): int;
118
119
    /**
120
     * @return bool True if character group is fixed length otherwise false
121
     */
122 7
    public function isFixed(): bool
123
    {
124 7
        return $this instanceof FixedCG;
125
    }
126
}
127