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 |
||
20 | class OwnerMediafile extends Owner |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public static function tableName() |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | View Code Duplication | public function rules() |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function attributeLabels() |
||
78 | |||
79 | /** |
||
80 | * @return \yii\db\ActiveQuery |
||
81 | */ |
||
82 | public function getMediaFile() |
||
86 | |||
87 | /** |
||
88 | * Get all mediafiles by owner. |
||
89 | * |
||
90 | * @param string $owner |
||
91 | * @param int $ownerId |
||
92 | * @param null|string $ownerAttribute |
||
93 | * |
||
94 | * @return Mediafile[] |
||
95 | */ |
||
96 | View Code Duplication | public static function getMediaFiles(string $owner, int $ownerId, string $ownerAttribute = null) |
|
104 | |||
105 | /** |
||
106 | * Get all mediafiles Query by owner. |
||
107 | * |
||
108 | * @param array $args. It can be an array of the next params: owner{string}, ownerId{int}, ownerAttribute{string}. |
||
109 | * |
||
110 | * @return ActiveQuery |
||
111 | */ |
||
112 | public static function getMediaFilesQuery(array $args = []): ActiveQuery |
||
119 | |||
120 | /** |
||
121 | * Get one owner thumbnail file by owner. |
||
122 | * |
||
123 | * @param string $owner |
||
124 | * @param int $ownerId |
||
125 | * |
||
126 | * @return array|null|\yii\db\ActiveRecord|Mediafile |
||
127 | */ |
||
128 | public static function getOwnerThumbnail(string $owner, int $ownerId) |
||
145 | |||
146 | /** |
||
147 | * Get image files by owner. |
||
148 | * |
||
149 | * @param string $owner |
||
150 | * @param int $ownerId |
||
151 | * |
||
152 | * @return Mediafile[] |
||
153 | */ |
||
154 | public static function getImageFiles(string $owner, int $ownerId) |
||
158 | |||
159 | /** |
||
160 | * Get audio files by owner. |
||
161 | * |
||
162 | * @param string $owner |
||
163 | * @param int $ownerId |
||
164 | * |
||
165 | * @return Mediafile[] |
||
166 | */ |
||
167 | public static function getAudioFiles(string $owner, int $ownerId) |
||
171 | |||
172 | /** |
||
173 | * Get video files by owner. |
||
174 | * |
||
175 | * @param string $owner |
||
176 | * @param int $ownerId |
||
177 | * |
||
178 | * @return Mediafile[] |
||
179 | */ |
||
180 | public static function getVideoFiles(string $owner, int $ownerId) |
||
184 | |||
185 | /** |
||
186 | * Get app files by owner. |
||
187 | * |
||
188 | * @param string $owner |
||
189 | * @param int $ownerId |
||
190 | * |
||
191 | * @return Mediafile[] |
||
192 | */ |
||
193 | public static function getAppFiles(string $owner, int $ownerId) |
||
197 | |||
198 | /** |
||
199 | * Get text files by owner. |
||
200 | * |
||
201 | * @param string $owner |
||
202 | * @param int $ownerId |
||
203 | * |
||
204 | * @return Mediafile[] |
||
205 | */ |
||
206 | public static function getTextFiles(string $owner, int $ownerId) |
||
210 | |||
211 | /** |
||
212 | * Get other files by owner. |
||
213 | * |
||
214 | * @param string $owner |
||
215 | * @param int $ownerId |
||
216 | * |
||
217 | * @return Mediafile[] |
||
218 | */ |
||
219 | public static function getOtherFiles(string $owner, int $ownerId) |
||
223 | } |
||
224 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.