GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — packet_price_editor ( d1862f...6d17fd )
by
unknown
09:54
created

PacketPriceEditAction   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 197
Duplicated Lines 7.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
c 1
b 0
f 0
lcom 1
cbo 1
dl 14
loc 197
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 2
A getParentCategories() 0 18 4
B getCalculator() 0 26 4
A checkAndRound() 0 13 3
D editPrices() 14 89 15

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace app\modules\shop\actions;
3
4
use Yii;
5
use yii\base\Action;
6
use yii\web\Response;
7
use app\modules\shop\models\Category;
8
use app\modules\shop\models\Currency;
9
use app\modules\shop\models\Product;
10
11
class PacketPriceEditAction extends Action
12
{
13
	protected $pRound = []; // params of round
14
15
	public function run()
16
	{
17
		if (false === Yii::$app->request->isAjax) {
18
            throw new NotFoundHttpException();
19
        }
20
        ini_set('max_execution_time', 0);
21
        Yii::$app->response->format = Response::FORMAT_JSON;
22
23
        Yii::$app->log->flushInterval = 100;
24
        Yii::$app->log->traceLevel = 0;
25
        Yii::$app->log->targets = [
26
        	new yii\log\FileTarget([
27
	            'levels' => ['error'],
28
	            'exportInterval' => 100,
29
	        ])
30
        ];
31
32
        return $this->editPrices(
33
        	Yii::$app->request->post('items'),
34
        	Yii::$app->request->post('context')
35
        );
36
	}
37
38
	/**
39
	* Gets products from selected categories
40
	* @param $list int[]
41
	* @return int[]
42
	*/
43
	protected function getParentCategories($list) {
44
		$incChild = Yii::$app->request->post('is_child_inc');
45
		$count = count($list);
46
47
		// read child cats
48
		if ($incChild) {
49
			for ($i = 0; $i < $count; $i++) {
50
				$cats = Category::getByParentId($list[$i]);
51
				foreach ($cats as $category) {
52
					$list[] = $category->id;
53
					$count ++;
54
				}
55
				unset($cats);
56
			}
57
		}
58
		
59
		return $list;
60
	}
61
62
	/**
63
	* Set the algorithm of calculation
64
	* @return float function(float, float)
0 ignored issues
show
Documentation introduced by
Should the return type not be \Closure?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
	*/
66
	protected function getCalculator() {
67
		$kind = Yii::$app->request->post('kind');
68
	 	$operation = Yii::$app->request->post('operation');
69
70
		if ($kind == 'fixed') { // fixed value
71
	 		if ($operation == 'inc')
72
	 			$calculator = function($subj, $value) {
73
	 				return $subj + $value;
74
	 			};
75
	 		else
76
	 			$calculator = function($subj, $value) {
77
	 				return $subj - $value;
78
	 			};
79
	 	} else { // percent value
80
	 		if ($operation == 'inc')
81
	 			$calculator = function($subj, $value) {
82
	 				return  $subj * (1 + $value / 100);
83
	 			};
84
	 		else
85
	 			$calculator = function($subj, $value) {
86
	 				return  $subj * (1 - $value / 100);
87
	 			};
88
	 	}
89
90
	 	return $calculator;
91
	}
92
93
	/**
94
	* To compare with zero and round
95
	* @param &$price float - $price can be rounded
96
	* @return bool
97
	*/
98
	protected function checkAndRound(&$price) {
99
		if ($price >= 0) {
100
	 		if ($this->pRound['is_round'])
101
	 			$price = round(
102
	 				$price,
103
	 				$this->pRound['round_val']
104
	 			);
105
 		} else {
106
 			return false;
107
 		}
108
109
 		return true;
110
	}
111
112
	/**
113
	* Main function. Change prices and saving it
114
	* @param $data int[]
115
	* @param $context string
116
	* @return mixed[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,integer|array>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
	*/
118
	protected function editPrices($data, $context) {
119
		$calculator = $this->getCalculator();
120
		$selectedField = Yii::$app->request->post('apply_for');
121
		$value = Yii::$app->request->post('value');
122
		$type = Yii::$app->request->post('type');
123
		$currencyId = Yii::$app->request->post('currency_id');
124
		$this->pRound = [
125
			'is_round' => Yii::$app->request->post('is_round'),
126
			'round_val' => Yii::$app->request->post('round_val'),
127
		];
128
129
		$report = [
130
			'all' => 0,
131
			'success' => 0,
132
			'error' => 0,
133
			'skipped' => 0,
134
			'errors' => []
135
		];
136
137
		if ($context == 'backend-product')
138
			$sql = ['in', 'id', $data];
139
		else
140
			$sql = ['in', 'main_category_id', $this->getParentCategories($data)];
141
142
		$items = Product::find()
143
			->select(['id', 'name', 'currency_id', 'price', 'old_price'])
144
			->where($sql)
145
			->asArray()
146
			->all();
147
148
		foreach ($items as $item){
149
		 	if ($item['currency_id'] != $currencyId) {
150
		 		$report['skipped']++;
151
		 		continue;
152
		 	}
153
154
		 	// change prices
155
		 	$fError = false;
156
		 	$errorKey = '[' . $item['id'] . '] ' . $item['name'];
157
		 	$calcPrice = $item['price'];
158
			$calcOldPrice = $item['old_price'];
159
160
		 	if ($type == 'normal') {
161
			 	// price
162 View Code Duplication
			 	if ($selectedField == 'price' || $selectedField == 'all') {
163
			 		$calcPrice = $calculator($calcPrice, $value);
164
			 		if (!$this->checkAndRound($calcPrice)) {
165
			 			$fError = true;
166
			 			$report['errors'][$errorKey][Yii::t('app', 'Price')] = Yii::t('app', 'Сalculated value is less than zero');
167
				 	}
168
			 	}
169
			 	// old price
170 View Code Duplication
			 	if ($selectedField == 'old_price' || $selectedField == 'all') {
171
			 		$calcOldPrice = $calculator($calcOldPrice, $value);
172
			 		if (!$this->checkAndRound($calcOldPrice)) {
173
			 			$fError = true;
174
			 			$report['errors'][$errorKey][Yii::t('app', 'Old Price')] = Yii::t('app', 'Сalculated value is less than zero');
175
				 	}
176
			 	}
177
			 } else { // type == relative
178
			 	if ($selectedField == 'price') {
179
			 		$calcOldPrice = $calculator($calcPrice, $value);
180
			 		if (!$this->checkAndRound($calcOldPrice)) {
181
			 			$fError = true;
182
			 			$report['errors'][$errorKey][Yii::t('app', 'Old Price')] = Yii::t('app', 'Сalculated value is less than zero');
183
				 	}
184
			 	} else {
185
			 		$calcPrice = $calculator($calcOldPrice, $value);
186
			 		if (!$this->checkAndRound($calcPrice)) {
187
			 			$fError = true;
188
			 			$report['errors'][$errorKey][Yii::t('app', 'Price')] = Yii::t('app', 'Сalculated value is less than zero');
189
				 	}
190
				}
191
			 }
192
		 	
193
		 	$report['all']++;
194
		 	if ($fError) {
195
		 		$report['error']++;
196
		 	} else {
197
		 		$report['success']++;
198
	 			Product::updateAll(
199
		 			['price' => $calcPrice, 'old_price' => $calcOldPrice],
200
		 			['id' => $item['id']]
201
		 		);
202
		 	}
203
		}
204
205
		return $report;
206
	}
207
}