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 LoginAction extends Action |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string name of the view, which should be rendered |
||
| 18 | */ |
||
| 19 | public $view = '@vendor/yii2mod/yii2-user/views/login'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string Login Form className |
||
| 23 | */ |
||
| 24 | public $modelClass = 'yii2mod\user\models\LoginForm'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string layout the name of the layout to be applied to this view |
||
| 28 | */ |
||
| 29 | public $layout; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @inheritdoc |
||
| 33 | */ |
||
| 34 | public function init() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Logs in a user. |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function run() |
||
| 71 | } |
||
| 72 |
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: