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 |
||
| 12 | class Telephone |
||
| 13 | { |
||
| 14 | //Telephone variations |
||
| 15 | protected $format_E164; |
||
| 16 | protected $format_international; |
||
| 17 | protected $format_national; |
||
| 18 | protected $format_RFC3966; |
||
| 19 | |||
| 20 | //Telephone structure |
||
| 21 | protected $country_code; |
||
| 22 | protected $area_code; |
||
| 23 | protected $subscriber_code; |
||
| 24 | protected $extension; |
||
| 25 | |||
| 26 | //Telephone extras |
||
| 27 | protected $region; |
||
| 28 | protected $input; |
||
| 29 | |||
| 30 | //Telephone types |
||
| 31 | protected $typeMap = [ |
||
| 32 | Type::FIXED_LINE => 'FIXED_LINE', |
||
| 33 | Type::MOBILE => 'MOBILE', |
||
| 34 | Type::FIXED_LINE_OR_MOBILE => 'FIXED_LINE_OR_MOBILE', |
||
| 35 | Type::TOLL_FREE => 'TOLL_FREE', |
||
| 36 | Type::PREMIUM_RATE => 'PREMIUM_RATE', |
||
| 37 | Type::SHARED_COST => 'SHARED_COST', |
||
| 38 | Type::VOIP => 'VOIP', |
||
| 39 | Type::PERSONAL_NUMBER => 'PERSONAL_NUMBER', |
||
| 40 | Type::PAGER => 'PAGER', |
||
| 41 | Type::UAN => 'UAN', |
||
| 42 | Type::UNKNOWN => 'UNKNOWN', |
||
| 43 | Type::EMERGENCY => 'EMERGENCY', |
||
| 44 | Type::VOICEMAIL => 'VOICEMAIL', |
||
| 45 | Type::SHORT_CODE => 'SHORT_CODE', |
||
| 46 | Type::STANDARD_RATE => 'STANDARD_RATE', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $input The phone number |
||
| 51 | * @param string $region The region or country of the phone number |
||
| 52 | */ |
||
| 53 | 6 | public function __construct($input,$region = 'US') |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | 6 | protected function setup() |
|
| 84 | |||
| 85 | 6 | View Code Duplication | protected function extractAreaCode() |
| 97 | |||
| 98 | 6 | View Code Duplication | protected function extractSubscriberCode() |
| 110 | |||
| 111 | /*===================================== |
||
| 112 | = Ouput Methods = |
||
| 113 | =====================================*/ |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 3 | public function getRFC3966() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 3 | public function getNational() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 3 | public function getInternational() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | 3 | public function getE164() |
|
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | 3 | public function getOriginal() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 3 | public function getAreaCode() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | 3 | public function getCountryCode() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 3 | public function getSubscriberCode() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | 3 | public function getType() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | 6 | public function isValid() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 3 | public function __toString() |
|
| 203 | |||
| 204 | /*===== End of Ouput Methods ======*/ |
||
| 205 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: