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 |
||
| 16 | trait BuilderRules |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $requiredFields = ['from_id', 'to_id', 'category_id']; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Value has to be a string. |
||
| 25 | * |
||
| 26 | * @param $value |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | protected function isString($value) |
||
| 30 | { |
||
| 31 | if (! is_string($value)) { |
||
| 32 | throw new InvalidArgumentException('The value Passed is not a string'); |
||
| 33 | } |
||
| 34 | |||
| 35 | return true; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Value has to be a valid Carbon Instance. |
||
| 40 | * |
||
| 41 | * @param $value |
||
| 42 | * @return bool | InvalidArgumentException |
||
| 43 | */ |
||
| 44 | protected function isCarbon($value) |
||
| 45 | { |
||
| 46 | if ($value instanceof Carbon) { |
||
| 47 | return true; |
||
| 48 | } |
||
| 49 | |||
| 50 | throw new InvalidArgumentException('The value Passed has to be an instance of Carbon\\Carbon'); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Value has to be numeric. |
||
| 55 | * |
||
| 56 | * @param $value |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | protected function isNumeric($value) |
||
| 60 | { |
||
| 61 | if (! is_numeric($value)) { |
||
| 62 | throw new InvalidArgumentException('The value Passed must be a number'); |
||
| 63 | } |
||
| 64 | |||
| 65 | return true; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns all required fields including the config ones. |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function getRequiredFields() |
||
| 74 | { |
||
| 75 | $customRequiredFields = []; |
||
| 76 | if (property_exists($this, 'config') && $this->config instanceof Repository) { |
||
| 77 | $customRequiredFields = $this->config->get('notifynder.additional_fields.required', []); |
||
|
|
|||
| 78 | } |
||
| 79 | |||
| 80 | return array_unique($this->requiredFields + $customRequiredFields); |
||
| 81 | } |
||
| 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) |
||
| 92 | { |
||
| 93 | foreach ($this->getRequiredFields() as $field) { |
||
| 94 | if (! array_key_exists($field, $array)) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | return true; |
||
| 100 | } |
||
| 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) |
||
| 121 | { |
||
| 122 | if ($this->isAssociativeArr($arr)) { |
||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (count($arr) > 0) { |
||
| 127 | $error = "The 'extra' value must to be an associative array"; |
||
| 128 | throw new InvalidArgumentException($error); |
||
| 129 | } |
||
| 130 | |||
| 131 | return false; |
||
| 132 | } |
||
| 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: