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 Fenos\Notifynder\Builder; |
||
| 16 | trait BuilderRules |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $requiredFields = ['from_id','to_id','url','category_id']; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Value has to be a string |
||
| 26 | * |
||
| 27 | * @param $value |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | protected function isString($value) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Value has to be a valid Carbon Instance |
||
| 41 | * |
||
| 42 | * @param $value |
||
| 43 | * @return bool | InvalidArgumentException |
||
| 44 | */ |
||
| 45 | protected function isCarbon($value) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Value has to be numeric |
||
| 54 | * |
||
| 55 | * @param $value |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | protected function isNumeric($value) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns all required fields including the config ones |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function getRequiredFields() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Check that the builder has |
||
| 85 | * the required field to send the |
||
| 86 | * notifications correctly |
||
| 87 | * |
||
| 88 | * @param $array |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public function hasRequiredFields($array) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Check if is a required field |
||
| 104 | * |
||
| 105 | * @param $offset |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function isRequiredField($offset) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Check if the array passed is |
||
| 115 | * multidimensional |
||
| 116 | * |
||
| 117 | * @param $arr |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | protected function isReadyArrToFormatInJson(array $arr) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $arr |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | protected function isAssociativeArr(array $arr) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Check if the array is |
||
| 145 | * multidimensional |
||
| 146 | * |
||
| 147 | * @param $arr |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function isMultidimensionalArray($arr) |
|
| 159 | } |
||
| 160 |
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: