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 OwnerAlbum 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 getAlbum() |
||
86 | |||
87 | /** |
||
88 | * Get all albums by owner. |
||
89 | * |
||
90 | * @param string $owner |
||
91 | * @param int $ownerId |
||
92 | * @param string $ownerAttribute |
||
93 | * |
||
94 | * @return Album[] |
||
95 | */ |
||
96 | View Code Duplication | public static function getAlbums(string $owner, int $ownerId, string $ownerAttribute = null) |
|
104 | |||
105 | /** |
||
106 | * Get all albums 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 getAlbumsQuery(array $args = []): ActiveQuery |
||
119 | |||
120 | /** |
||
121 | * Get image albums by owner. |
||
122 | * |
||
123 | * @param string $owner |
||
124 | * @param int $ownerId |
||
125 | * |
||
126 | * @return Album[] |
||
127 | */ |
||
128 | public static function getImageAlbums(string $owner, int $ownerId) |
||
132 | |||
133 | /** |
||
134 | * Get audio albums by owner. |
||
135 | * |
||
136 | * @param string $owner |
||
137 | * @param int $ownerId |
||
138 | * |
||
139 | * @return Album[] |
||
140 | */ |
||
141 | public static function getAudioAlbums(string $owner, int $ownerId) |
||
145 | |||
146 | /** |
||
147 | * Get video albums by owner. |
||
148 | * |
||
149 | * @param string $owner |
||
150 | * @param int $ownerId |
||
151 | * |
||
152 | * @return Album[] |
||
153 | */ |
||
154 | public static function getVideoAlbums(string $owner, int $ownerId) |
||
158 | |||
159 | /** |
||
160 | * Get application albums by owner. |
||
161 | * |
||
162 | * @param string $owner |
||
163 | * @param int $ownerId |
||
164 | * |
||
165 | * @return Album[] |
||
166 | */ |
||
167 | public static function getAppAlbums(string $owner, int $ownerId) |
||
171 | |||
172 | /** |
||
173 | * Get text albums by owner. |
||
174 | * |
||
175 | * @param string $owner |
||
176 | * @param int $ownerId |
||
177 | * |
||
178 | * @return Album[] |
||
179 | */ |
||
180 | public static function getTextAlbums(string $owner, int $ownerId) |
||
184 | |||
185 | /** |
||
186 | * Get other albums by owner. |
||
187 | * |
||
188 | * @param string $owner |
||
189 | * @param int $ownerId |
||
190 | * |
||
191 | * @return Album[] |
||
192 | */ |
||
193 | public static function getOtherAlbums(string $owner, int $ownerId) |
||
197 | } |
||
198 |
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.