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
|
|
|
* - fixed length character group |
13
|
|
|
* |
14
|
|
|
* @author Vitaly Egorov <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class FixedVariableFixedCG extends AbstractCharacterGroup |
17
|
|
|
{ |
18
|
|
|
/** string Character group matching regexp pattern matching group name */ |
19
|
|
|
const PATTERN_GROUP = 'fixedVariableFixed'; |
20
|
|
|
|
21
|
|
|
/** string Character group matching regexp pattern */ |
22
|
|
|
const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.FixedCG::PATTERN_REGEXP.VariableCG::PATTERN_REGEXP.FixedCG::PATTERN_REGEXP.')'; |
23
|
|
|
|
24
|
|
|
/** @var FixedCG */ |
25
|
|
|
protected $firstFixedCG; |
26
|
|
|
|
27
|
|
|
/** @var VariableCG */ |
28
|
|
|
protected $variableCG; |
29
|
|
|
|
30
|
|
|
/** @var FixedCG */ |
31
|
|
|
protected $lastFixedCG; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
6 |
View Code Duplication |
public function __construct(string $string, int $length = 0) |
|
|
|
|
37
|
|
|
{ |
38
|
6 |
|
parent::__construct($string, $length); |
39
|
|
|
|
40
|
|
|
// Parse internal character groups |
41
|
6 |
|
$this->firstFixedCG = FixedCG::fromString($string); |
42
|
6 |
|
$this->variableCG = VariableCG::fromString($string); |
43
|
6 |
|
$this->lastFixedCG = FixedCG::fromString($string); |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
4 |
|
protected function compareLength(AbstractCharacterGroup $group): int |
50
|
|
|
{ |
51
|
|
|
/** @var FixedVariableFixedCG $group */ |
52
|
|
|
|
53
|
|
|
// Shorter first FCG has higher priority |
54
|
4 |
|
$return = $group->firstFixedCG->length <=> $this->firstFixedCG->length; |
|
|
|
|
55
|
|
|
|
56
|
|
|
// First FCG are equal |
57
|
4 |
|
if ($return === 0) { |
58
|
|
|
// Longer last FCG has higher priority |
59
|
2 |
|
$return = $this->lastFixedCG->length <=> $group->lastFixedCG->length; |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Last FCG are equal |
62
|
2 |
|
if ($return === 0) { |
63
|
|
|
// Longer VCG has higher priority |
64
|
2 |
|
$return = $this->variableCG->length <=> $group->variableCG->length; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
return $return; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.