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; |
||
| 15 | trait BuilderRules |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $requiredFields = ['from_id','to_id','url','category_id']; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Value has to be a string |
||
| 25 | * |
||
| 26 | * @param $value |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | protected function isString($value) |
||
| 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) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Value has to be numeric |
||
| 53 | * |
||
| 54 | * @param $value |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | protected function isNumeric($value) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns all required fields including the config ones |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function getRequiredFields() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Check that the builder has |
||
| 78 | * the required field to send the |
||
| 79 | * notifications correctly |
||
| 80 | * |
||
| 81 | * @param $array |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function hasRequiredFields($array) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check if is a required field |
||
| 97 | * |
||
| 98 | * @param $offset |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function isRequiredField($offset) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Check if the array passed is |
||
| 108 | * multidimensional |
||
| 109 | * |
||
| 110 | * @param $arr |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | protected function isReadyArrToFormatInJson(array $arr) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param array $arr |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | protected function isAssociativeArr(array $arr) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if the array is |
||
| 138 | * multidimensional |
||
| 139 | * |
||
| 140 | * @param $arr |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function isMultidimensionalArray($arr) |
|
| 152 | } |
||
| 153 |
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.