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 |
||
| 10 | class DatabaseModule extends Module |
||
| 11 | { |
||
| 12 | public $attributes; |
||
| 13 | |||
| 14 | public function __construct(Container $app, string $name, $path) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @return ModuleEntity|Builder |
||
| 22 | */ |
||
| 23 | public function getModel() |
||
| 27 | |||
| 28 | private function loadAttributes() |
||
| 54 | |||
| 55 | public function getAttributes() |
||
| 59 | |||
| 60 | public function setAttributes($attributes) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get a specific data from json file by given the key. |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * @param null $default |
||
| 70 | * |
||
| 71 | * @return mixed |
||
| 72 | */ |
||
| 73 | public function get(string $key, $default = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Determine whether the given status same with the current module status. |
||
| 80 | * |
||
| 81 | * @param bool $status |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | public function isStatus(bool $status): bool |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Determine whether the current module activated. |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function isEnabled(): bool |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Determine whether the current module not disabled. |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function isDisabled(): bool |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set active state for current module. |
||
| 112 | * |
||
| 113 | * @param bool $active |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function setActive(bool $active): void |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Disable the current module. |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function disable(): void |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Enable the current module. |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function enable(): void |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Delete the current module. |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function delete(): bool |
||
| 164 | } |
||
| 165 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: