Total Complexity | 66 |
Total Lines | 473 |
Duplicated Lines | 0 % |
Changes | 25 | ||
Bugs | 2 | Features | 1 |
Complex classes like BillingContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BillingContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class BillingContext extends BaseContext |
||
21 | { |
||
22 | use ExpectException { |
||
23 | mayFail as protected; |
||
24 | shouldFail as protected; |
||
25 | assertCaughtExceptionMatches as protected; |
||
26 | } |
||
27 | |||
28 | protected $saleTime; |
||
29 | |||
30 | protected $bill; |
||
31 | |||
32 | protected $charges = []; |
||
33 | |||
34 | /** |
||
35 | * @Given reseller :reseller |
||
36 | */ |
||
37 | public function reseller($reseller) |
||
38 | { |
||
39 | $this->builder->buildReseller($reseller); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @Given customer :customer |
||
44 | */ |
||
45 | public function customer($customer) |
||
46 | { |
||
47 | $this->builder->buildCustomer($customer); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @Given manager :manager |
||
52 | */ |
||
53 | public function manager($manager) |
||
54 | { |
||
55 | $this->builder->buildManager($manager); |
||
|
|||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @Given /^(\S+ )?(\S+) tariff plan (\S+)/ |
||
60 | */ |
||
61 | public function plan($prefix, $type, $plan) |
||
62 | { |
||
63 | $prefix = strtr($prefix, ' ', '_'); |
||
64 | $grouping = $prefix === 'grouping_'; |
||
65 | $type = $grouping ? $type : $prefix.$type; |
||
66 | $this->builder->buildPlan($plan, $type, $grouping); |
||
67 | } |
||
68 | |||
69 | protected function fullPrice(array $data) |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) for target (.+)$/ |
||
79 | */ |
||
80 | public function priceWithTarget($type, $price, $currency, $unit, $target) |
||
81 | { |
||
82 | return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'target')); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+)$/ |
||
87 | */ |
||
88 | public function priceWithPrepaid($type, $price, $currency, $unit, $prepaid) |
||
89 | { |
||
90 | return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'prepaid')); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+) for target (\S+)$/ |
||
95 | */ |
||
96 | public function priceWithPrepaidAndTarget($type, $price, $currency, $unit, $prepaid, $target) |
||
97 | { |
||
98 | return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'prepaid', 'target')); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+)$/ |
||
103 | */ |
||
104 | public function price($type, $price, $currency, $unit) |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @Given /price for (\S+) is +(\S+) (\S+) per 1 (\S+) and (\S+) (\S+) per 2 (\S+) for target (\S+)/ |
||
111 | */ |
||
112 | public function enumPrice($type, $price, $currency, $unit, $price2, $currency2, $unit2, $target) |
||
113 | { |
||
114 | $sums = [1 => $price, 2 => $price2]; |
||
115 | |||
116 | return $this->fullPrice(compact('type', 'sums', 'currency', 'unit', 'target')); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @Given /^remove and recreate tariff plan (\S+)/ |
||
121 | */ |
||
122 | public function recreatePlan($plan) |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @Given /sale target (\S+) by plan (\S+) at (\S+)/ |
||
129 | */ |
||
130 | public function sale($target, $plan, $time): void |
||
131 | { |
||
132 | $this->saleTime = $this->prepareTime($time); |
||
133 | $this->builder->buildSale($target, $plan, $this->saleTime); |
||
134 | } |
||
135 | /** |
||
136 | * @When /^sale close is requested for target "([^"]*)" at "([^"]*)", assuming current time is "([^"]*)"$/ |
||
137 | */ |
||
138 | public function saleClose(string $target, string $time, ?string $wallTime) |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @Then /^target "([^"]*)" has exactly (\d+) sale for customer$/ |
||
145 | */ |
||
146 | public function targetHasExactlyNSaleForCustomer(string $target, string $count) |
||
147 | { |
||
148 | // TODO: implement |
||
149 | // $sales = $this->builder->findSales(['target-name' => $target]); |
||
150 | |||
151 | Assert::assertCount($count, $sales); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @Given /purchase target (\S+) by plan (\S+) at (\S+)$/ |
||
156 | */ |
||
157 | public function purchaseTarget(string $target, string $plan, string $time): void |
||
158 | { |
||
159 | $time = $this->prepareTime($time); |
||
160 | $this->builder->buildPurchase($target, $plan, $time); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @Given /^purchase target "([^"]*)" by plan "([^"]*)" at "([^"]*)" with the following initial uses:$/ |
||
165 | */ |
||
166 | public function purchaseTargetWithInitialUses(string $target, string $plan, string $time, TableNode $usesTable): void |
||
167 | { |
||
168 | $time = $this->prepareTime($time); |
||
169 | $uses = array_map(static function (array $row) { |
||
170 | return [ |
||
171 | 'type' => $row['type'], |
||
172 | 'unit' => $row['unit'], |
||
173 | 'amount' => $row['amount'], |
||
174 | ]; |
||
175 | }, $usesTable->getColumnsHash()); |
||
176 | |||
177 | $this->mayFail( |
||
178 | fn() => $this->builder->buildPurchase($target, $plan, $time, $uses) |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @Given /resource consumption for (\S+) is +(\S+) (\S+) for target (\S+) at (.+)$/ |
||
184 | */ |
||
185 | public function setConsumption(string $type, int $amount, string $unit, string $target, string $time): void |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @Given /recalculate autotariff for target (\S+)( +at (\S+))?$/ |
||
193 | */ |
||
194 | public function recalculateAutoTariff(string $target, string $time = null): void |
||
195 | { |
||
196 | $this->builder->clientSetAutoTariff($target, $time); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @Given /perform billing at (\S+)/ |
||
201 | */ |
||
202 | public function performBilling(string $time): void |
||
203 | { |
||
204 | $this->builder->performBilling($this->prepareTime($time)); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @Given /action for (\S+) is +(\S+) (\S+) +for target (.+?)( +at (\S+))?$/ |
||
209 | */ |
||
210 | public function setAction(string $type, int $amount, string $unit, string $target, string $at = null, string $time = null): void |
||
211 | { |
||
212 | $time = $this->prepareTime($time); |
||
213 | $this->builder->setAction($type, $amount, $unit, $target, $time); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @Given /perform calculation( at (\S+))?/ |
||
218 | */ |
||
219 | public function performCalculation(string $at = null, string $time = null): array |
||
220 | { |
||
221 | $this->charges = $this->builder->performCalculation($this->prepareTime($time)); |
||
222 | return $this->charges; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @Given /bill +for (\S+) is +(\S+) (\S+) per (\S+) (\S+) for target (.+?)( +at (.+))?$/ |
||
227 | */ |
||
228 | public function billWithTime($type, $sum, $currency, $quantity, $unit, $target, $at = null, $time = null) |
||
229 | { |
||
230 | $this->builder->flushEntitiesCacheByType('bill'); |
||
231 | |||
232 | $quantity = $this->prepareQuantity($quantity); |
||
233 | $sum = $this->prepareSum($sum, $quantity); |
||
234 | $time = $this->prepareTime($time); |
||
235 | $bill = $this->findBill([ |
||
236 | 'type' => $type, |
||
237 | 'target' => $target, |
||
238 | 'sum' => "$sum $currency", |
||
239 | 'quantity' => "$quantity $unit", |
||
240 | 'time' => $time, |
||
241 | ]); |
||
242 | Assert::assertSame($type, $bill->getType()->getName(), "Bill type mismatch: expected $type, got {$bill->getType()->getName()}"); |
||
243 | Assert::assertSame($target, $bill->getTarget()->getFullName(), "Bill target mismatch: expected $target, got {$bill->getTarget()->getFullName()}"); |
||
244 | Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount(), "Bill sum mismatch: expected $sum, got {$bill->getSum()->getAmount()}"); |
||
245 | Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode(), "Bill currency mismatch: expected $currency, got {$bill->getSum()->getCurrency()->getCode()}"); |
||
246 | Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity(), "Bill quantity mismatch: expected $quantity, got {$bill->getQuantity()->getQuantity()}"); |
||
247 | Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName()), "Bill unit mismatch: expected $unit, got {$bill->getQuantity()->getUnit()->getName()}"); |
||
248 | if ($time) { |
||
249 | Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime(), "Bill time mismatch: expected $time, got {$bill->getTime()->format(DATE_ATOM)}"); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | public function findBill(array $params): BillInterface |
||
254 | { |
||
255 | $bills = $this->builder->findBills($params); |
||
256 | $this->bill = reset($bills); |
||
257 | $this->charges = $this->bill->getCharges(); |
||
258 | |||
259 | return $this->bill; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @Given /bills number is (\d+) for (\S+) for target (.+?)( +at (\S+))?$/ |
||
264 | */ |
||
265 | public function billsNumberWithTime($number, $type, $target, $at = null, $time = null) |
||
266 | { |
||
267 | $count = count($this->builder->findBills(array_filter([ |
||
268 | 'type' => $type, |
||
269 | 'target' => $target, |
||
270 | 'time' => $this->prepareTime($time), |
||
271 | ]))); |
||
272 | |||
273 | Assert::assertEquals($number, $count); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @Given /charges number is (\d+)/ |
||
278 | */ |
||
279 | public function chargesNumber($number) |
||
280 | { |
||
281 | Assert::assertEquals($number, count($this->charges)); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @Given /charge for (\S+) is +(\S+) (\S+) per (\S+) (\S+)$/ |
||
286 | */ |
||
287 | public function charge($type, $amount, $currency, $quantity, $unit) |
||
288 | { |
||
289 | $this->chargeWithTarget($type, $amount, $currency, $quantity, $unit, null); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @Given /charge for (\S+) is +(\S+) (\S+) per +(\S+) (\S+) +for target (.+?)( +at (\S+))?$/ |
||
294 | */ |
||
295 | public function chargeWithTarget($type, $amount, $currency, $quantity, $unit, $target, $at = null, $time = null) |
||
296 | { |
||
297 | $quantity = $this->prepareQuantity($quantity); |
||
298 | $amount = $this->prepareSum($amount, $quantity); |
||
299 | $charge = $this->findCharge($type, $target); |
||
300 | Assert::assertNotNull($charge); |
||
301 | Assert::assertSame($type, $charge->getType()->getName()); |
||
302 | Assert::assertSame($target, $charge->getTarget()->getFullName()); |
||
303 | Assert::assertEquals(bcmul($amount, 100), (int)$charge->getSum()->getAmount()); |
||
304 | Assert::assertSame($currency, $charge->getSum()->getCurrency()->getCode()); |
||
305 | Assert::assertEquals((float)$quantity, (float)$charge->getUsage()->getQuantity()); |
||
306 | Assert::assertEquals(strtolower($unit), strtolower($charge->getUsage()->getUnit()->getName())); |
||
307 | } |
||
308 | |||
309 | public function findCharge($type, $target): ?ChargeInterface |
||
310 | { |
||
311 | foreach ($this->charges as $charge) { |
||
312 | if ($charge->getType()->getName() !== $type) { |
||
313 | continue; |
||
314 | } |
||
315 | if ($charge->getTarget()->getFullName() !== $target) { |
||
316 | continue; |
||
317 | } |
||
318 | |||
319 | return $charge; |
||
320 | } |
||
321 | |||
322 | return null; |
||
323 | } |
||
324 | |||
325 | public function getNextCharge(): ChargeInterface |
||
326 | { |
||
327 | $charge = current($this->charges); |
||
328 | next($this->charges); |
||
329 | |||
330 | return $charge; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @return string|false|null |
||
335 | */ |
||
336 | protected function prepareTime(string $time = null) |
||
337 | { |
||
338 | if ($time === null) { |
||
339 | return null; |
||
340 | } |
||
341 | |||
342 | if ($time === 'midnight second day of this month') { |
||
343 | return date('Y-m-02'); |
||
344 | } |
||
345 | if (strncmp($time, 'pY', 1) === 0) { |
||
346 | return date(substr($time, 1), strtotime('-1 year')); |
||
347 | } |
||
348 | if (str_contains($time, 'pm')) { |
||
349 | $time = str_replace('pm', 'm', $time); |
||
350 | $time = date($time, strtotime('-1 month')); |
||
351 | } |
||
352 | if (strncmp($time, 'Y', 1) === 0) { |
||
353 | return date($time); |
||
354 | } |
||
355 | |||
356 | return $time; |
||
357 | } |
||
358 | |||
359 | private function prepareQuantity($quantity) |
||
360 | { |
||
361 | if ($quantity[0] === 's') { |
||
362 | return $this->getSaleQuantity(); |
||
363 | } |
||
364 | |||
365 | return $quantity; |
||
366 | } |
||
367 | |||
368 | private function prepareSum($sum, $quantity) |
||
369 | { |
||
370 | if ($sum[0] === 's') { |
||
371 | $sum = round(substr($sum, 1) * $quantity*100)/100; |
||
372 | } |
||
373 | |||
374 | return $sum; |
||
375 | } |
||
376 | |||
377 | public function getSaleQuantity() |
||
378 | { |
||
379 | return $this->days2quantity(new DateTimeImmutable($this->saleTime)); |
||
380 | } |
||
381 | |||
382 | private function days2quantity(DateTimeImmutable $from) |
||
383 | { |
||
384 | $till = new DateTimeImmutable('first day of next month midnight'); |
||
385 | $diff = $from->diff($till); |
||
386 | if ($diff->m) { |
||
387 | return 1; |
||
388 | } |
||
389 | |||
390 | return $diff->d/date('t'); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @When /^tariff plan change is requested for target "([^"]*)" to plan "([^"]*)" at "([^"]*)"$/ |
||
395 | */ |
||
396 | public function tariffPlanChangeIsRequestedForTarget(string $target, string $planName, string $date) |
||
397 | { |
||
398 | $this->mayFail(fn () => $this->builder->targetChangePlan($target, $planName, $this->prepareTime($date))); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @When /^tariff plan change is requested for target "([^"]*)" to plan "([^"]*)" at "([^"]*)", assuming current time is "([^"]*)"$/ |
||
403 | */ |
||
404 | public function tariffPlanChangeIsRequestedForTargetAtSpecificTime(string $target, string $planName, string $date, ?string $wallTime = null) |
||
405 | { |
||
406 | $this->mayFail(fn () => $this->builder->targetChangePlan($target, $planName, $this->prepareTime($date), $this->prepareTime($wallTime))); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @Then /^target "([^"]*)" is sold to customer by plan "([^"]*)" since "([^"]*)"(?: till "([^"]*)")?$/ |
||
411 | */ |
||
412 | public function targetIsSoldToCustomerByPlanSinceTill(string $target, string $planName, string $saleDate, ?string $saleCloseDate = null) |
||
413 | { |
||
414 | $sales = $this->builder->findHistoricalSales([ |
||
415 | 'target' => $target, |
||
416 | ]); |
||
417 | |||
418 | $saleDateTime = new DateTimeImmutable('@' . strtotime($saleDate)); |
||
419 | $saleCloseDateTime = new DateTimeImmutable('@' . ($saleCloseDate ? strtotime($saleCloseDate) : time())); |
||
420 | |||
421 | foreach ($sales as $sale) { |
||
422 | /** @noinspection PhpBooleanCanBeSimplifiedInspection */ |
||
423 | $saleExists = true |
||
424 | && str_contains($sale->getPlan()->getName(), $planName) |
||
425 | && $sale->getTime()->format(DATE_ATOM) === $saleDateTime->format(DATE_ATOM) |
||
426 | && ( |
||
427 | ($saleCloseDate === null && $sale->getCloseTime() === null) |
||
428 | || |
||
429 | ($saleCloseDate !== null && $sale->getCloseTime()->format(DATE_ATOM) === $saleCloseDateTime->format(DATE_ATOM)) |
||
430 | ); |
||
431 | |||
432 | if ($saleExists) { |
||
433 | return; |
||
434 | } |
||
435 | } |
||
436 | |||
437 | Assert::fail('Requested sale does not exist'); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * @Then /^target "([^"]*)" has exactly (\d+) sales for customer$/ |
||
442 | */ |
||
443 | public function targetHasExactlySalesForCustomer(string $target, int $count) |
||
444 | { |
||
445 | $sales = $this->builder->findHistoricalSales([ |
||
446 | 'target' => $target, |
||
447 | ]); |
||
448 | |||
449 | Assert::assertCount($count, $sales); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @Then /^caught error is "([^"]*)"$/ |
||
454 | */ |
||
455 | public function caughtErrorIs(string $errorMessage): void |
||
456 | { |
||
457 | $this->assertCaughtExceptionMatches(\Throwable::class, $errorMessage); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * @Given /^target "([^"]*)"$/ |
||
462 | */ |
||
463 | public function target(string $target) |
||
464 | { |
||
465 | $this->builder->buildTarget($target); |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * @Then /^flush entities cache$/ |
||
470 | */ |
||
471 | public function flushEntitiesCache() |
||
472 | { |
||
473 | $this->builder->flushEntitiesCache(); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @Given /^target "([^"]*)" has the following uses:$/ |
||
478 | */ |
||
479 | public function targetHasTheFollowingUses(string $target, TableNode $usesTable) |
||
493 | ); |
||
494 | } |
||
495 | } |
||
496 | } |
||
497 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.