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 SmartUpdateAction extends SwitchAction |
||
25 | { |
||
26 | const EVENT_BEFORE_FETCH_LOAD = 'beforeFetchLoad'; |
||
27 | const EVENT_BEFORE_FETCH = 'beforeFetch'; |
||
28 | |||
29 | /** |
||
30 | * @var array|\Closure additional data passed to view |
||
31 | */ |
||
32 | public $data = []; |
||
33 | |||
34 | /** |
||
35 | * @var string The view that represents current update action |
||
36 | */ |
||
37 | public $_view; |
||
38 | |||
39 | /** |
||
40 | * @var int|string ID of object to be viewed. Defaults to `$_GET['id']` |
||
41 | */ |
||
42 | protected $_id; |
||
43 | |||
44 | /** |
||
45 | * @var array additional data passed to model find method |
||
46 | */ |
||
47 | public $findOptions = []; |
||
48 | |||
49 | /** |
||
50 | * @var Model |
||
51 | */ |
||
52 | private $_searchModel; |
||
53 | |||
54 | /** |
||
55 | * @var \yii\data\ActiveDataProvider stores ActiveDataProvider after creating by [[getDataProvider]] |
||
56 | * @see getDataProvider() |
||
57 | */ |
||
58 | public $dataProvider; |
||
59 | |||
60 | /** |
||
61 | * Creates `ActiveDataProvider` with given options list, stores it to [[dataProvider]]. |
||
62 | * |
||
63 | * @throws BadRequestHttpException |
||
64 | * @throws \yii\base\InvalidConfigException when failed to generate `WHERE` condition |
||
65 | * @return ActiveDataProvider |
||
66 | */ |
||
67 | public function getDataProvider() |
||
68 | { |
||
69 | if ($this->dataProvider === null) { |
||
70 | $this->_id = $this->_id |
||
71 | ?: Yii::$app->request->post('selection') |
||
72 | ?: Yii::$app->request->get('selection') |
||
73 | ?: Yii::$app->request->get('id'); |
||
74 | |||
75 | $this->dataProvider = $this->getSearchModel()->search([], ['pagination' => false]); |
||
|
|||
76 | $this->dataProvider->query->andFilterWhere( |
||
77 | is_array($this->_id) ? ['in', 'id', $this->_id] : ['eq', 'id', $this->_id] |
||
78 | ); |
||
79 | |||
80 | if (empty($this->dataProvider->query->where)) { |
||
81 | throw new BadRequestHttpException('Where condition is empty!'); |
||
82 | } |
||
83 | |||
84 | $this->dataProvider->query->andFilterWhere($this->findOptions); |
||
85 | } |
||
86 | $limit = $this->dataProvider->query->limit; |
||
87 | $this->dataProvider->query->andFilterWhere($this->findOptions)->limit($limit ?? -1); |
||
88 | |||
89 | return $this->dataProvider; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param $model |
||
94 | */ |
||
95 | public function setSearchModel($model) |
||
99 | |||
100 | /** |
||
101 | * @return Model|SearchModelTrait |
||
102 | */ |
||
103 | public function getSearchModel() |
||
111 | |||
112 | /** {@inheritdoc} */ |
||
113 | protected function getDefaultRules() |
||
216 | |||
217 | /** |
||
218 | * @return string |
||
219 | */ |
||
220 | public function getView() |
||
224 | |||
225 | /** |
||
226 | * @param string $view |
||
227 | */ |
||
228 | public function setView($view) |
||
232 | |||
233 | /** |
||
234 | * Fetches models that will be edited. |
||
235 | * |
||
236 | * @throws BadRequestHttpException |
||
237 | * @return Model[] |
||
238 | */ |
||
239 | public function fetchModels() |
||
247 | |||
248 | public function beforeFetchLoad() |
||
252 | |||
253 | public function beforeFetch() |
||
257 | } |
||
258 |
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: