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 |
||
24 | class Controller extends \yii\web\Controller |
||
25 | { |
||
26 | /** |
||
27 | * @var Cache|array|string the cache object or the application component ID of the cache object. |
||
28 | */ |
||
29 | protected $_cache = 'cache'; |
||
30 | |||
31 | public function setCache($cache) |
||
32 | { |
||
33 | $this->_cache = $cache; |
||
34 | } |
||
35 | |||
36 | public function getCache() |
||
37 | { |
||
38 | if (!is_object($this->_cache)) { |
||
39 | $this->_cache = Instance::ensure($this->_cache, Cache::className()); |
||
40 | } |
||
41 | |||
42 | return $this->_cache; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @var array internal actions. |
||
47 | */ |
||
48 | protected $_internalActions; |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 1 | View Code Duplication | public function behaviors() |
|
|||
54 | { |
||
55 | return [ |
||
56 | 'access' => [ |
||
57 | 1 | 'class' => AccessControl::className(), |
|
58 | 1 | 'only' => ['index'], |
|
59 | 'rules' => [ |
||
60 | [ |
||
61 | 1 | 'actions' => ['index'], |
|
62 | 1 | 'allow' => true, |
|
63 | 1 | 'roles' => ['@'], |
|
64 | 1 | ], |
|
65 | 1 | ], |
|
66 | 1 | ], |
|
67 | 1 | ]; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $submodel the submodel that will be added to the ClassName |
||
72 | * @return string Main Model class name |
||
73 | */ |
||
74 | public static function modelClassName() |
||
84 | |||
85 | /** |
||
86 | * @param array $config config to be used to create the [[Model]] |
||
87 | * @return ActiveRecord |
||
88 | */ |
||
89 | public static function newModel($config = [], $submodel = '') |
||
94 | |||
95 | /** |
||
96 | * @param array $config config to be used to create the [[Model]] |
||
97 | * @return ActiveRecord|SearchModelTrait Search Model object |
||
98 | */ |
||
99 | public static function searchModel($config = []) |
||
103 | |||
104 | /** |
||
105 | * @return string main model's formName() |
||
106 | */ |
||
107 | public static function formName() |
||
111 | |||
112 | /** |
||
113 | * @return string search model's formName() |
||
114 | */ |
||
115 | public static function searchFormName() |
||
119 | |||
120 | /** |
||
121 | * @param string $separator |
||
122 | * @return string Main model's camel2id'ed formName() |
||
123 | */ |
||
124 | public static function modelId($separator = '-') |
||
128 | |||
129 | /** |
||
130 | * Returns the module ID based on the namespace of the controller. |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public static function moduleId() |
||
137 | |||
138 | public static function controllerId() |
||
142 | |||
143 | /** |
||
144 | * @param int|array $condition scalar ID or array to be used for searching |
||
145 | * @param array $config config to be used to create the [[Model]] |
||
146 | * @throws NotFoundHttpException |
||
147 | * @return array|ActiveRecord|null|static |
||
148 | */ |
||
149 | public static function findModel($condition, $config = []) |
||
159 | |||
160 | public static function findModels($condition, $config = []) |
||
161 | { |
||
162 | $containsIntKeys = 0; |
||
163 | if (is_array($condition)) { |
||
164 | foreach (array_keys($condition) as $item) { |
||
165 | if (is_numeric($item)) { |
||
166 | $containsIntKeys = true; |
||
167 | break; |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | if (!is_array($condition) || $containsIntKeys) { |
||
173 | $condition = ['id' => $condition]; |
||
174 | } |
||
175 | $models = static::searchModel($config)->search([static::searchFormName() => $condition], ['pagination' => false])->getModels(); |
||
176 | if ($models === null) { |
||
177 | throw new NotFoundHttpException('The requested object not found.'); |
||
178 | } |
||
179 | |||
180 | return $models; |
||
181 | } |
||
182 | |||
183 | public static function renderJson($data) |
||
189 | |||
190 | public static function renderJsonp($data) |
||
196 | |||
197 | public function actionIndex() |
||
201 | |||
202 | public function setInternalAction($id, $action) |
||
203 | { |
||
204 | $this->_internalActions[$id] = $action; |
||
205 | } |
||
206 | |||
207 | public function hasInternalAction($id) |
||
211 | |||
212 | public function createAction($id) |
||
217 | |||
218 | /** |
||
219 | * Prepares array for building url to action based on given action id and parameters. |
||
220 | * |
||
221 | * @param string $action action id |
||
222 | * @param string|int|array $params ID of object to be action'ed or array of parameters |
||
223 | * @return array array suitable for Url::to |
||
224 | */ |
||
225 | public static function getActionUrl($action = 'index', $params = []) |
||
230 | |||
231 | /** |
||
232 | * Prepares array for building url to search with given filters. |
||
233 | */ |
||
234 | public static function getSearchUrl(array $params = []) |
||
238 | } |
||
239 |
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.