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 |
||
| 18 | class Module |
||
| 19 | { |
||
| 20 | /** @var YF\Modules\Base\Model\Module[] Module model cache. */ |
||
| 21 | protected static $cache; |
||
| 22 | |||
| 23 | /** @var string Module name. */ |
||
| 24 | protected $moduleName; |
||
| 25 | |||
| 26 | /** @var array Fields. */ |
||
| 27 | protected $fields; |
||
| 28 | |||
| 29 | /** @var array Fields models. */ |
||
| 30 | protected $fieldsModels; |
||
| 31 | |||
| 32 | protected $defaultView = 'ListView'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get module model instance. |
||
| 36 | * |
||
| 37 | * @param string $moduleName |
||
| 38 | * |
||
| 39 | * @return self |
||
|
|
|||
| 40 | */ |
||
| 41 | public static function getInstance(string $moduleName): self |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor function. |
||
| 52 | * |
||
| 53 | * @param string $moduleName |
||
| 54 | */ |
||
| 55 | public function __construct(string $moduleName) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Function to check permission for a Module/Action. |
||
| 62 | * |
||
| 63 | * @param string $module |
||
| 64 | * @param string $action |
||
| 65 | * |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public static function isPermitted(string $module, string $action) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get all fields. |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function getFields(): array |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get all fields models. |
||
| 104 | * |
||
| 105 | * @return \YF\Modules\Base\FieldTypes\BaseField[] |
||
| 106 | */ |
||
| 107 | public function getFieldsModels(): array |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get fields and blocks. |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getFieldsFromApi(): array |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get tabs. |
||
| 134 | * |
||
| 135 | * @param int $record |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function getTabsFromApi(int $record): array |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get field by name. |
||
| 161 | * |
||
| 162 | * @param string $name |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function getField(string $name): array |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get field model by name. |
||
| 179 | * |
||
| 180 | * @param string $name |
||
| 181 | * |
||
| 182 | * @return \YF\Modules\Base\FieldTypes\BaseField |
||
| 183 | */ |
||
| 184 | public function getFieldModel(string $name): \YF\Modules\Base\FieldTypes\BaseField |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns default view for module. |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getDefaultView(): string |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns default address url. |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getDefaultUrl(): string |
||
| 211 | } |
||
| 212 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.