Total Complexity | 69 |
Total Lines | 511 |
Duplicated Lines | 0 % |
Changes | 28 | ||
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 | protected array $progressivePrice = []; |
||
35 | |||
36 | /** |
||
37 | * @Given reseller :reseller |
||
38 | */ |
||
39 | public function reseller($reseller) |
||
40 | { |
||
41 | $this->builder->buildReseller($reseller); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @Given customer :customer |
||
46 | */ |
||
47 | public function customer($customer) |
||
48 | { |
||
49 | $this->builder->buildCustomer($customer); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @Given manager :manager |
||
54 | */ |
||
55 | public function manager($manager) |
||
56 | { |
||
57 | $this->builder->buildManager($manager); |
||
|
|||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @Given /^(\S+ )?(\S+) tariff plan (\S+)/ |
||
62 | */ |
||
63 | public function plan($prefix, $type, $plan) |
||
64 | { |
||
65 | $prefix = strtr($prefix, ' ', '_'); |
||
66 | $grouping = $prefix === 'grouping_'; |
||
67 | $type = $grouping ? $type : $prefix.$type; |
||
68 | $this->builder->buildPlan($plan, $type, $grouping); |
||
69 | } |
||
70 | |||
71 | protected function fullPrice(array $data) |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) for target (.+)$/ |
||
81 | */ |
||
82 | public function priceWithTarget($type, $price, $currency, $unit, $target) |
||
83 | { |
||
84 | return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'target')); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+)$/ |
||
89 | */ |
||
90 | public function priceWithPrepaid($type, $price, $currency, $unit, $prepaid) |
||
91 | { |
||
92 | return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'prepaid')); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+) prepaid (\S+) for target (\S+)$/ |
||
97 | */ |
||
98 | public function priceWithPrepaidAndTarget($type, $price, $currency, $unit, $prepaid, $target) |
||
99 | { |
||
100 | return $this->fullPrice(compact('type', 'price', 'currency', 'unit', 'prepaid', 'target')); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @Given /price for (\S+) is +(\S+) (\S+) per (\S+)$/ |
||
105 | */ |
||
106 | public function price($type, $price, $currency, $unit) |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @Given /price for (\S+) is +(\S+) (\S+) per 1 (\S+) and (\S+) (\S+) per 2 (\S+) for target (\S+)/ |
||
113 | */ |
||
114 | public function enumPrice($type, $price, $currency, $unit, $price2, $currency2, $unit2, $target) |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @Given /progressive price for (\S+) is +(\S+) (\S+) per (\S+) (\S+) (\S+) (\S+)$/ |
||
123 | */ |
||
124 | public function progressivePrice($type, $price, $currency, $unit, $sign, $quantity, $perUnit): void |
||
148 | ] |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @Given /^remove and recreate tariff plan (\S+)/ |
||
155 | */ |
||
156 | public function recreatePlan($plan) |
||
157 | { |
||
158 | $this->builder->recreatePlan($plan); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @Given /sale target (\S+) by plan (\S+) at (\S+)/ |
||
163 | */ |
||
164 | public function sale($target, $plan, $time): void |
||
165 | { |
||
166 | $this->saleTime = $this->prepareTime($time); |
||
167 | $this->builder->buildSale($target, $plan, $this->saleTime); |
||
168 | } |
||
169 | /** |
||
170 | * @When /^sale close is requested for target "([^"]*)" at "([^"]*)", assuming current time is "([^"]*)"$/ |
||
171 | */ |
||
172 | public function saleClose(string $target, string $time, ?string $wallTime) |
||
173 | { |
||
174 | throw new PendingException(); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @Then /^target "([^"]*)" has exactly (\d+) sale for customer$/ |
||
179 | */ |
||
180 | public function targetHasExactlyNSaleForCustomer(string $target, string $count) |
||
181 | { |
||
182 | // TODO: implement |
||
183 | // $sales = $this->builder->findSales(['target-name' => $target]); |
||
184 | |||
185 | Assert::assertCount($count, $sales); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @Given /purchase target (\S+) by plan (\S+) at ([-:\w\s]+)$/ |
||
190 | */ |
||
191 | public function purchaseTarget(string $target, string $plan, string $time): void |
||
192 | { |
||
193 | $time = $this->prepareTime($time); |
||
194 | $this->builder->buildPurchase($target, $plan, $time); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @Given /^purchase target "([^"]*)" by plan "([^"]*)" at "([^"]*)" with the following initial uses:$/ |
||
199 | */ |
||
200 | public function purchaseTargetWithInitialUses(string $target, string $plan, string $time, TableNode $usesTable): void |
||
201 | { |
||
202 | $time = $this->prepareTime($time); |
||
203 | $uses = array_map(static function (array $row) { |
||
204 | return [ |
||
205 | 'type' => $row['type'], |
||
206 | 'unit' => $row['unit'], |
||
207 | 'amount' => $row['amount'], |
||
208 | ]; |
||
209 | }, $usesTable->getColumnsHash()); |
||
210 | |||
211 | $this->mayFail( |
||
212 | fn() => $this->builder->buildPurchase($target, $plan, $time, $uses) |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @Given /resource consumption for (\S+) is +(\S+) (\S+) for target (\S+) at (.+)$/ |
||
218 | */ |
||
219 | public function setConsumption(string $type, int $amount, string $unit, string $target, string $time): void |
||
220 | { |
||
221 | $time = $this->prepareTime($time); |
||
222 | $this->builder->setConsumption($type, $amount, $unit, $target, $time); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @Given /recalculate autotariff for target (\S+)( +at (\S+))?$/ |
||
227 | */ |
||
228 | public function recalculateAutoTariff(string $target, string $time = null): void |
||
229 | { |
||
230 | $this->builder->clientSetAutoTariff($target, $time); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @Given /perform billing at (\S+)/ |
||
235 | */ |
||
236 | public function performBilling(string $time): void |
||
237 | { |
||
238 | $this->builder->performBilling($this->prepareTime($time)); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @Given /action for (\S+) is +(\S+) (\S+) +for target (.+?)( +at (\S+))?$/ |
||
243 | */ |
||
244 | public function setAction(string $type, int $amount, string $unit, string $target, string $at = null, string $time = null): void |
||
245 | { |
||
246 | $time = $this->prepareTime($time); |
||
247 | $this->builder->setAction($type, $amount, $unit, $target, $time); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @Given /perform calculation( at (\S+))?/ |
||
252 | */ |
||
253 | public function performCalculation(string $at = null, string $time = null): array |
||
254 | { |
||
255 | $this->charges = $this->builder->performCalculation($this->prepareTime($time)); |
||
256 | return $this->charges; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @Given /bill +for (\S+) is +(\S+) (\S+) per (\S+) (\S+) for target (.+?)( +at (.+))?$/ |
||
261 | */ |
||
262 | public function billWithTime($type, $sum, $currency, $quantity, $unit, $target, $at = null, $time = null) |
||
263 | { |
||
264 | $this->builder->flushEntitiesCacheByType('bill'); |
||
265 | |||
266 | $quantity = $this->prepareQuantity($quantity); |
||
267 | $sum = $this->prepareSum($sum, $quantity); |
||
268 | $time = $this->prepareTime($time); |
||
269 | $bill = $this->findBill([ |
||
270 | 'type' => $type, |
||
271 | 'target' => $target, |
||
272 | 'sum' => "$sum $currency", |
||
273 | 'quantity' => "$quantity $unit", |
||
274 | 'time' => $time, |
||
275 | ]); |
||
276 | Assert::assertSame($type, $bill->getType()->getName(), "Bill type mismatch: expected $type, got {$bill->getType()->getName()}"); |
||
277 | Assert::assertSame($target, $bill->getTarget()->getFullName(), "Bill target mismatch: expected $target, got {$bill->getTarget()->getFullName()}"); |
||
278 | Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount(), "Bill sum mismatch: expected $sum, got {$bill->getSum()->getAmount()}"); |
||
279 | Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode(), "Bill currency mismatch: expected $currency, got {$bill->getSum()->getCurrency()->getCode()}"); |
||
280 | Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity(), "Bill quantity mismatch: expected $quantity, got {$bill->getQuantity()->getQuantity()}"); |
||
281 | Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName()), "Bill unit mismatch: expected $unit, got {$bill->getQuantity()->getUnit()->getName()}"); |
||
282 | if ($time) { |
||
283 | Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime(), "Bill time mismatch: expected $time, got {$bill->getTime()->format(DATE_ATOM)}"); |
||
284 | } |
||
285 | } |
||
286 | |||
287 | public function findBill(array $params): BillInterface |
||
288 | { |
||
289 | $bills = $this->builder->findBills($params); |
||
290 | $this->bill = reset($bills); |
||
291 | $this->charges = $this->bill->getCharges(); |
||
292 | |||
293 | return $this->bill; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @Given /bills number is (\d+) for (\S+) for target (.+?)( +at (\S+))?$/ |
||
298 | */ |
||
299 | public function billsNumberWithTime($number, $type, $target, $at = null, $time = null) |
||
300 | { |
||
301 | $count = count($this->builder->findBills(array_filter([ |
||
302 | 'type' => $type, |
||
303 | 'target' => $target, |
||
304 | 'time' => $this->prepareTime($time), |
||
305 | ]))); |
||
306 | |||
307 | Assert::assertEquals($number, $count); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @Given /charges number is (\d+)/ |
||
312 | */ |
||
313 | public function chargesNumber($number) |
||
314 | { |
||
315 | Assert::assertEquals($number, count($this->charges)); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @Given /charge for (\S+) is +(\S+) (\S+) per (\S+) (\S+)$/ |
||
320 | */ |
||
321 | public function charge($type, $amount, $currency, $quantity, $unit) |
||
322 | { |
||
323 | $this->chargeWithTarget($type, $amount, $currency, $quantity, $unit, null); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @Given /charge for (\S+) is +(\S+) (\S+) per +(\S+) (\S+) +for target (.+?)( +at (\S+))?$/ |
||
328 | */ |
||
329 | public function chargeWithTarget($type, $amount, $currency, $quantity, $unit, $target, $at = null, $time = null) |
||
330 | { |
||
331 | $quantity = $this->prepareQuantity($quantity); |
||
332 | $amount = $this->prepareSum($amount, $quantity); |
||
333 | $charge = $this->findCharge($type, $target); |
||
334 | Assert::assertNotNull($charge); |
||
335 | Assert::assertSame($type, $charge->getType()->getName()); |
||
336 | Assert::assertSame($target, $charge->getTarget()->getFullName()); |
||
337 | Assert::assertEquals(bcmul($amount, 100), (int)$charge->getSum()->getAmount()); |
||
338 | Assert::assertSame($currency, $charge->getSum()->getCurrency()->getCode()); |
||
339 | Assert::assertEquals((float)$quantity, (float)$charge->getUsage()->getQuantity()); |
||
340 | Assert::assertEquals(strtolower($unit), strtolower($charge->getUsage()->getUnit()->getName())); |
||
341 | } |
||
342 | |||
343 | public function findCharge($type, $target): ?ChargeInterface |
||
344 | { |
||
345 | foreach ($this->charges as $charge) { |
||
346 | if ($charge->getType()->getName() !== $type) { |
||
347 | continue; |
||
348 | } |
||
349 | if ($charge->getTarget()->getFullName() !== $target) { |
||
350 | continue; |
||
351 | } |
||
352 | |||
353 | return $charge; |
||
354 | } |
||
355 | |||
356 | return null; |
||
357 | } |
||
358 | |||
359 | public function getNextCharge(): ChargeInterface |
||
360 | { |
||
361 | $charge = current($this->charges); |
||
362 | next($this->charges); |
||
363 | |||
364 | return $charge; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * @return string|false|null |
||
369 | */ |
||
370 | protected function prepareTime(string $time = null) |
||
371 | { |
||
372 | if ($time === null) { |
||
373 | return null; |
||
374 | } |
||
375 | |||
376 | if ($time === 'midnight second day of this month') { |
||
377 | return date('Y-m-02'); |
||
378 | } |
||
379 | if (strncmp($time, 'pY', 1) === 0) { |
||
380 | return date(substr($time, 1), strtotime('-1 year')); |
||
381 | } |
||
382 | if (str_contains($time, 'nm')) { |
||
383 | $format = str_replace('nm', 'm', $time); |
||
384 | return date($format, strtotime('next month')); |
||
385 | } |
||
386 | if (str_contains($time, 'pm')) { |
||
387 | $time = str_replace('pm', 'm', $time); |
||
388 | $time = date($time, strtotime('-1 month')); |
||
389 | } |
||
390 | if (strncmp($time, 'Y', 1) === 0) { |
||
391 | return date($time); |
||
392 | } |
||
393 | |||
394 | return $time; |
||
395 | } |
||
396 | |||
397 | private function prepareQuantity($quantity) |
||
398 | { |
||
399 | if ($quantity[0] === 's') { |
||
400 | return $this->getSaleQuantity(); |
||
401 | } |
||
402 | |||
403 | return $quantity; |
||
404 | } |
||
405 | |||
406 | private function prepareSum($sum, $quantity) |
||
407 | { |
||
408 | if ($sum[0] === 's') { |
||
409 | $sum = round(substr($sum, 1) * $quantity*100)/100; |
||
410 | } |
||
411 | |||
412 | return $sum; |
||
413 | } |
||
414 | |||
415 | public function getSaleQuantity() |
||
416 | { |
||
417 | return $this->days2quantity(new DateTimeImmutable($this->saleTime)); |
||
418 | } |
||
419 | |||
420 | private function days2quantity(DateTimeImmutable $from) |
||
421 | { |
||
422 | $till = new DateTimeImmutable('first day of next month midnight'); |
||
423 | $diff = $from->diff($till); |
||
424 | if ($diff->m) { |
||
425 | return 1; |
||
426 | } |
||
427 | |||
428 | return $diff->d/date('t'); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @When /^tariff plan change is requested for target "([^"]*)" to plan "([^"]*)" at "([^"]*)"$/ |
||
433 | */ |
||
434 | public function tariffPlanChangeIsRequestedForTarget(string $target, string $planName, string $date) |
||
435 | { |
||
436 | $this->mayFail(fn () => $this->builder->targetChangePlan($target, $planName, $this->prepareTime($date))); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @When /^tariff plan change is requested for target "([^"]*)" to plan "([^"]*)" at "([^"]*)", assuming current time is "([^"]*)"$/ |
||
441 | */ |
||
442 | public function tariffPlanChangeIsRequestedForTargetAtSpecificTime(string $target, string $planName, string $date, ?string $wallTime = null) |
||
443 | { |
||
444 | $this->mayFail(fn () => $this->builder->targetChangePlan($target, $planName, $this->prepareTime($date), $this->prepareTime($wallTime))); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @Then /^target "([^"]*)" is sold to customer by plan "([^"]*)" since "([^"]*)"(?: till "([^"]*)")?$/ |
||
449 | */ |
||
450 | public function targetIsSoldToCustomerByPlanSinceTill(string $target, string $planName, string $saleDate, ?string $saleCloseDate = null) |
||
451 | { |
||
452 | $sales = $this->builder->findHistoricalSales([ |
||
453 | 'target' => $target, |
||
454 | ]); |
||
455 | |||
456 | $saleDateTime = new DateTimeImmutable('@' . strtotime($this->prepareTime($saleDate))); |
||
457 | $saleCloseDateTime = $saleCloseDate ? new DateTimeImmutable('@' . strtotime($this->prepareTime($saleCloseDate))) : null; |
||
458 | |||
459 | foreach ($sales as $sale) { |
||
460 | /** @noinspection PhpBooleanCanBeSimplifiedInspection */ |
||
461 | $saleExists = true |
||
462 | && str_contains($sale->getPlan()->getName(), $planName) |
||
463 | && $sale->getTime()->format(DATE_ATOM) === $saleDateTime->format(DATE_ATOM) |
||
464 | && ( |
||
465 | ($saleCloseDate === null && $sale->getCloseTime() === null) |
||
466 | || |
||
467 | ($saleCloseDate !== null && $sale->getCloseTime()->format(DATE_ATOM) === $saleCloseDateTime->format(DATE_ATOM)) |
||
468 | ); |
||
469 | |||
470 | if ($saleExists) { |
||
471 | return; |
||
472 | } |
||
473 | } |
||
474 | |||
475 | Assert::fail('Requested sale does not exist'); |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @Then /^target "([^"]*)" has exactly (\d+) sales for customer$/ |
||
480 | */ |
||
481 | public function targetHasExactlySalesForCustomer(string $target, int $count) |
||
482 | { |
||
483 | $sales = $this->builder->findHistoricalSales([ |
||
484 | 'target' => $target, |
||
485 | ]); |
||
486 | |||
487 | Assert::assertCount($count, $sales); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @Then /^caught error is "([^"]*)"$/ |
||
492 | */ |
||
493 | public function caughtErrorIs(string $errorMessage): void |
||
494 | { |
||
495 | $this->assertCaughtExceptionMatches(\Throwable::class, $errorMessage); |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * @Given /^target "([^"]*)"$/ |
||
500 | */ |
||
501 | public function target(string $target) |
||
502 | { |
||
503 | $this->builder->buildTarget($target); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @Then /^flush entities cache$/ |
||
508 | */ |
||
509 | public function flushEntitiesCache() |
||
510 | { |
||
511 | $this->builder->flushEntitiesCache(); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * @Given /^target "([^"]*)" has the following uses:$/ |
||
516 | */ |
||
517 | public function targetHasTheFollowingUses(string $target, TableNode $usesTable) |
||
531 | ); |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 |
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.