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 |
||
34 | class Comment extends \yii\db\ActiveRecord |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * @inheritdoc |
||
39 | */ |
||
40 | public function behaviors() |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function rules() |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | public function attributeLabels() |
||
78 | |||
79 | /** |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function isEdited() |
||
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function isDeleted() |
||
94 | |||
95 | /** |
||
96 | * @return bool |
||
97 | */ |
||
98 | public static function canCreate() |
||
104 | |||
105 | /** |
||
106 | * @return bool |
||
107 | */ |
||
108 | View Code Duplication | public function canUpdate() |
|
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | View Code Duplication | public function canDelete() |
|
128 | |||
129 | /** |
||
130 | * @return queries\CommentQuery |
||
131 | */ |
||
132 | public function getAuthor() |
||
136 | |||
137 | /** |
||
138 | * @return queries\CommentQuery |
||
139 | */ |
||
140 | public function getLastUpdateAuthor() |
||
144 | |||
145 | /** |
||
146 | * @return queries\CommentQuery |
||
147 | */ |
||
148 | public static function find() |
||
155 | |||
156 | /** |
||
157 | * @inheritdoc |
||
158 | */ |
||
159 | public static function tableName() |
||
163 | |||
164 | const NOT_DELETED = 0; |
||
165 | const DELETED = 1; |
||
166 | } |
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: