| 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 | 19 | View Code Duplication |     public function __construct(string $string, int $length = 0) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 19 |  |         parent::__construct($string, $length); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // Parse internal character groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 19 |  |         $this->fixedCG = FixedCG::fromString($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 19 |  |         $this->variableCG = VariableCG::fromString($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |             if ($prefix === $this->fixedCG->getString() && $prefix === $group->fixedCG->getString()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |                 return $prefix . $this->variableCG->getCommonPrefix($group->variableCG); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * @return bool True if character group is fixed length otherwise false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function isFixed(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $this instanceof FixedVariableCG; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 13 |  |     public function compare(AbstractCG $group): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Compare with fixed character group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 13 |  |         if ($group instanceof FixedCG) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |             return $this->fixedCG->compare($group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         // Always FixedVariable character group has higher priority over variable character group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         // FixedVariable character group has higher priority over VariableFixed character group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 12 |  |         if ($group instanceof VariableCG || $group instanceof VariableFixedCG) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 9 |  |         if ($this->isSameType($group)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 9 |  |             return $this->compareLength($group); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param AbstractCG|FixedVariableCG|FixedVariableCG|VariableFixedCG $group | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 103 | 9 | View Code Duplication |     protected function compareLength(AbstractCG $group): int | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         // Fixed CG are equal | 
            
                                                        
            
                                    
            
            
                | 106 | 9 |  |         if (($return = $this->fixedCG->compare($group->fixedCG)) === 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             // Compare variable character groups | 
            
                                                        
            
                                    
            
            
                | 108 | 5 |  |             $return = $this->variableCG->compare($group->variableCG); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 111 | 9 |  |         return $return; | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
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.