Conditions | 14 |
Paths | 24 |
Total Lines | 108 |
Code Lines | 67 |
Lines | 29 |
Ratio | 26.85 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | public function run() |
||
|
|||
45 | { |
||
46 | app\backend\assets\FrontendEditingAsset::register($this->view); |
||
47 | |||
48 | $items = [ |
||
49 | [ |
||
50 | 'label' => Icon::show('dashboard') . ' ' . Yii::t('app', 'Backend'), |
||
51 | 'url' => ['/backend/'], |
||
52 | ] |
||
53 | ]; |
||
54 | |||
55 | switch (Yii::$app->requestedRoute) { |
||
56 | case 'shop/product/list': |
||
57 | if (isset($_GET['properties'])) { |
||
58 | $apply_if_params = []; |
||
59 | foreach ($_GET['properties'] as $property_id => $values) { |
||
60 | if (isset($values[0])) { |
||
61 | $apply_if_params[$property_id] = $values[0]; |
||
62 | } |
||
63 | } |
||
64 | if (Yii::$app->response->dynamic_content_trait === true) { |
||
65 | $items[] = [ |
||
66 | 'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Edit Dynamic Content'), |
||
67 | 'url' => [ |
||
68 | '/backend/dynamic-content/edit', |
||
69 | 'id' => Yii::$app->response->matched_dynamic_content_trait_model->id, |
||
70 | ], |
||
71 | ]; |
||
72 | } else { |
||
73 | if (isset($_GET['properties'], $_GET['last_category_id'])) { |
||
74 | $items[] = [ |
||
75 | 'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Add Dynamic Content'), |
||
76 | 'url' => [ |
||
77 | '/backend/dynamic-content/edit', |
||
78 | 'DynamicContent' => [ |
||
79 | 'apply_if_params' => Json::encode($apply_if_params), |
||
80 | 'apply_if_last_category_id' => $_GET['last_category_id'], |
||
81 | 'object_id' => Object::getForClass(app\modules\shop\models\Product::className())->id, |
||
82 | 'route' => 'shop/product/list', |
||
83 | ] |
||
84 | ], |
||
85 | ]; |
||
86 | |||
87 | } |
||
88 | |||
89 | } |
||
90 | View Code Duplication | } else { |
|
91 | // no properties selected - go to category edit page |
||
92 | |||
93 | if (isset($_GET['last_category_id'])) { |
||
94 | $reviewsLink = $this->getReviewEditParams("Category", intval($_GET['last_category_id'])); |
||
95 | $cat = app\modules\shop\models\Category::findById($_GET['last_category_id']); |
||
96 | $items[] = [ |
||
97 | 'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit category'), |
||
98 | 'url' => [ |
||
99 | '/shop/backend-category/edit', |
||
100 | 'id' => $cat->id, |
||
101 | 'parent_id' => $cat->parent_id, |
||
102 | ], |
||
103 | ]; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | break; |
||
108 | case 'shop/product/show': |
||
109 | if (isset($_GET['model_id'])) { |
||
110 | $reviewsLink = $this->getReviewEditParams("Product", intval($_GET['model_id'])); |
||
111 | $items[] = [ |
||
112 | 'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit product'), |
||
113 | 'url' => [ |
||
114 | '/shop/backend-product/edit', |
||
115 | 'id' => intval($_GET['model_id']) |
||
116 | ], |
||
117 | ]; |
||
118 | } |
||
119 | break; |
||
120 | |||
121 | case '/page/page/show': |
||
122 | case '/page/page/list': |
||
123 | View Code Duplication | if (isset($_GET['id'])) { |
|
124 | $page = app\modules\page\models\Page::findById($_GET['id']); |
||
125 | $reviewsLink = $this->getReviewEditParams("Page", $_GET['id']); |
||
126 | $items[] = [ |
||
127 | 'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit page'), |
||
128 | 'url' => [ |
||
129 | '/page/backend/edit', |
||
130 | 'id' => $page->id, |
||
131 | 'parent_id' =>$page->parent_id, |
||
132 | |||
133 | ], |
||
134 | ]; |
||
135 | } |
||
136 | break; |
||
137 | } |
||
138 | |||
139 | if (!empty($reviewsLink)) { |
||
140 | $items[] = $reviewsLink; |
||
141 | } |
||
142 | |||
143 | return $this->render( |
||
144 | 'floating-panel', |
||
145 | [ |
||
146 | 'items' => $items, |
||
147 | 'bottom' => $this->bottom, |
||
148 | ] |
||
149 | ); |
||
150 | |||
151 | } |
||
152 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: