Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 4 | trait PatternTrait |
||
| 5 | { |
||
| 6 | use XMLPatterns; |
||
| 7 | /** |
||
| 8 | * @Exclude |
||
| 9 | * @var array defines the exact sequence of characters that are acceptable |
||
| 10 | */ |
||
| 11 | public $pattern = array(); |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param string $newPatternToAdd |
||
| 15 | * @param bool $multiCharacterEscape |
||
| 16 | */ |
||
| 17 | protected function setPatternFacet($newPatternToAdd, $multiCharacterEscape = true) |
||
| 18 | { |
||
| 19 | if (null == self::$Letter) { |
||
| 20 | self::init(); |
||
| 21 | } |
||
| 22 | $newPatternToAdd = $this->processRegex($newPatternToAdd, $multiCharacterEscape); |
||
|
|
|||
| 23 | if (!$this->checkRegexValidPattern($newPatternToAdd)) { |
||
| 24 | $newPatternToAdd = '/' . $newPatternToAdd . '/u'; |
||
| 25 | if (!$this->checkRegexValidPattern($newPatternToAdd)) { |
||
| 26 | throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this) . |
||
| 27 | 'pattern is: ' . $newPatternToAdd); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | $this->pattern[] = $newPatternToAdd; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $patternToProcess |
||
| 35 | * @param bool $multiCharacterEscape |
||
| 36 | * |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | private function processRegex($patternToProcess, $multiCharacterEscape) |
||
| 40 | { |
||
| 41 | if (!$multiCharacterEscape) { |
||
| 42 | return $patternToProcess; |
||
| 43 | } |
||
| 44 | if (null == self::$NameChar) { |
||
| 45 | init(); |
||
| 46 | } |
||
| 47 | $patternToProcess = $this->processRegexSlashS($patternToProcess); |
||
| 48 | $patternToProcess = $this->processRegexSlashI($patternToProcess); |
||
| 49 | $patternToProcess = $this->processRegexSlashC($patternToProcess); |
||
| 50 | $patternToProcess = $this->processRegexSlashD($patternToProcess); |
||
| 51 | $patternToProcess = $this->processRegexSlashW($patternToProcess); |
||
| 52 | return $patternToProcess; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $patternToProcess |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | View Code Duplication | private function processRegexSlashS($patternToProcess) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $patternToProcess |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | private function processRegexSlashI($patternToProcess) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $patternToProcess |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | private function processRegexSlashC($patternToProcess) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $patternToProcess |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | View Code Duplication | private function processRegexSlashD($patternToProcess) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $patternToProcess |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | View Code Duplication | private function processRegexSlashW($patternToProcess) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $pattern |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | private function checkRegexValidPattern($pattern) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $value |
||
| 145 | */ |
||
| 146 | private function checkPattern($value) |
||
| 147 | { |
||
| 148 | if (!empty($this->pattern)) { |
||
| 149 | foreach ($this->pattern as $pattern) { |
||
| 150 | if (!$this->matchesRegexPattern($pattern, $value)) { |
||
| 151 | throw new \InvalidArgumentException('Assigned value (' . $value . ') for ' . get_class($this) . |
||
| 152 | ' does not match pattern ' . $pattern); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Checks a pattern against a string. |
||
| 160 | * |
||
| 161 | * @param string $pattern the regex pattern |
||
| 162 | * @param string $string the string to check |
||
| 163 | * @return bool true if string matches pattern |
||
| 164 | */ |
||
| 165 | private function matchesRegexPattern($pattern, $string) |
||
| 170 | } |
||
| 171 |