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 |
||
| 14 | class MultipleDelete extends Action |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string Model name, ie. `Product::className()` |
||
| 18 | */ |
||
| 19 | public $modelName = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool Mark as deleted instead of calling `delete()` |
||
| 23 | */ |
||
| 24 | public $markAsDeleted = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string Attribute that stores deleted state |
||
| 28 | */ |
||
| 29 | public $deletedMarkAttribute = 'is_active'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var mixed Deleted state value(ie. 1 for is_deleted or 0 for is_active) |
||
| 33 | */ |
||
| 34 | public $deletedMarkValue = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array Route to redirect after deletion |
||
| 38 | */ |
||
| 39 | public $redirect = ['index']; |
||
| 40 | |||
| 41 | View Code Duplication | public function init() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | public function run() |
||
| 74 | } |
||
| 75 |
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: