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 namespace Propaganistas\LaravelPhone\Rules; |
||
| 7 | class Phone |
||
| 8 | { |
||
| 9 | use ParsesCountries, |
||
| 10 | ParsesTypes; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The provided phone countries. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $allowedCountries = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The input field name to check for a country value. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $countryField; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The provided phone types. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $allowedTypes = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Whether the number's country should be auto-detected. |
||
| 35 | * |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $detect = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Whether to allow lenient checks (i.e. landline numbers without area codes). |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $lenient = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set the phone countries. |
||
| 49 | * |
||
| 50 | * @param string|array $country |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | 3 | View Code Duplication | public function country($country) |
| 64 | |||
| 65 | /** |
||
| 66 | * Set the country input field. |
||
| 67 | * |
||
| 68 | * @param string $name |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | 3 | public function countryField($name) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Set the phone types. |
||
| 80 | * |
||
| 81 | * @param string|array $type |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | 3 | View Code Duplication | public function type($type) |
| 95 | |||
| 96 | /** |
||
| 97 | * Shortcut method for mobile type restriction. |
||
| 98 | * |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | 3 | public function mobile() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Shortcut method for fixed line type restriction. |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | 3 | public function fixedLine() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Enable automatic country detection. |
||
| 122 | * |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | 3 | public function detect() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Enable lenient number checking. |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | 3 | public function lenient() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Convert the rule to a validation string. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 3 | public function __toString() |
|
| 161 | } |
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.