Total Complexity | 56 |
Total Lines | 388 |
Duplicated Lines | 0 % |
Changes | 20 | ||
Bugs | 3 | Features | 2 |
Complex classes like FeatureContext 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 FeatureContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class FeatureContext implements Context |
||
49 | { |
||
50 | protected $engine; |
||
51 | |||
52 | /** @var Customer */ |
||
53 | protected $customer; |
||
54 | /** |
||
55 | * @var \hiqdev\php\billing\price\PriceInterface|\hiqdev\php\billing\charge\FormulaChargeModifierTrait |
||
56 | * |
||
57 | * TODO: FormulaChargeModifierTrait::setFormula() must be moved to interface |
||
58 | */ |
||
59 | protected $price; |
||
60 | |||
61 | /** @var string */ |
||
62 | protected $formula; |
||
63 | |||
64 | /** |
||
65 | * @var \hiqdev\php\billing\action\ActionInterface|\hiqdev\php\billing\action\AbstractAction |
||
66 | */ |
||
67 | protected $action; |
||
68 | /** |
||
69 | * @var ChargeInterface[] |
||
70 | */ |
||
71 | protected $charges; |
||
72 | |||
73 | /** @var \Money\MoneyParser */ |
||
74 | protected $moneyParser; |
||
75 | |||
76 | /** @var string */ |
||
77 | protected $expectedError; |
||
78 | |||
79 | /** |
||
80 | * Initializes context. |
||
81 | */ |
||
82 | public function __construct() |
||
83 | { |
||
84 | error_reporting(E_ALL & ~E_DEPRECATED); |
||
85 | date_default_timezone_set('UTC'); |
||
86 | $this->customer = new Customer(null, 'somebody'); |
||
87 | $this->moneyParser = new DecimalMoneyParser(new ISOCurrencies()); |
||
88 | $this->plan = new Plan(null, 'plan', $this->customer); |
||
89 | $this->sale = new Sale(null, Target::any(), $this->customer, $this->plan, new DateTimeImmutable('2000-01-01')); |
||
90 | $this->billing = SimpleBilling::fromSale($this->sale); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @Given /(\S+) (\S+) price is ([0-9.]+) (\w+) per (\w+)(?: includes ([\d.]+))?/ |
||
95 | */ |
||
96 | public function priceIs($target, $type, $sum, $currency, $unit, $quantity = 0) |
||
97 | { |
||
98 | $type = new Type(Type::ANY, $type); |
||
99 | $target = new Target(Target::ANY, $target); |
||
100 | $quantity = Quantity::create($unit, $quantity); |
||
101 | $sum = $this->moneyParser->parse($sum, new Currency($currency)); |
||
102 | $this->setPrice(new SinglePrice(null, $type, $target, null, $quantity, $sum)); |
||
103 | } |
||
104 | |||
105 | protected array $progressivePrice = []; |
||
106 | /** |
||
107 | * @Given /(\S+) progressive price for (\S+) is +(\S+) (\S+) per (\S+) (\S+) (\S+) (\S+)$/ |
||
108 | */ |
||
109 | public function progressivePrice($target, $type, $price, $currency, $unit, $sign, $quantity, $perUnit): void |
||
126 | ]; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @Given /^build progressive price/ |
||
132 | */ |
||
133 | public function buildProgressivePrices() |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @Given /sale close time is ([0-9.-]+)?/ |
||
152 | */ |
||
153 | public function setActionCloseTime($closeTime): void |
||
154 | { |
||
155 | if ($closeTime === null) { |
||
156 | return; |
||
157 | } |
||
158 | |||
159 | $this->sale->close(new DateTimeImmutable($closeTime)); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @Given /sale time is (.+)$/ |
||
164 | */ |
||
165 | public function setSaleTime($time): void |
||
166 | { |
||
167 | $ref = new ReflectionClass($this->sale); |
||
168 | $prop = $ref->getProperty('time'); |
||
169 | $prop->setAccessible(true); |
||
170 | $prop->setValue($this->sale, new DateTimeImmutable($time)); |
||
171 | $prop->setAccessible(false); |
||
172 | } |
||
173 | |||
174 | private function setPrice($price) |
||
175 | { |
||
176 | $this->price = $price; |
||
177 | $ref = new ReflectionClass($this->plan); |
||
178 | $prop = $ref->getProperty('prices'); |
||
179 | $prop->setAccessible(true); |
||
180 | $prop->setValue($this->plan, [$price]); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @Given /action is (\S+) ([\w_,]+)(?: ([0-9.]+) (\S+))?(?: in (.+))?/ |
||
185 | */ |
||
186 | public function actionIs(string $target, string $type, float $amount, string $unit, ?string $date = null): void |
||
187 | { |
||
188 | $type = new Type(Type::ANY, $type); |
||
189 | $target = new Target(Target::ANY, $target); |
||
190 | $time = new DateTimeImmutable($date); |
||
191 | if ($this->sale->getCloseTime() instanceof DateTimeImmutable) { |
||
192 | $amount = $amount * $this->getFractionOfMonth( |
||
193 | $time, $time, $this->sale->getCloseTime() |
||
194 | ); |
||
195 | } |
||
196 | $quantity = Quantity::create($unit, $amount); |
||
197 | |||
198 | $this->action = new Action(null, $type, $target, $quantity, $this->customer, $time); |
||
199 | } |
||
200 | |||
201 | private function getFractionOfMonth(DateTimeImmutable $month, DateTimeImmutable $startTime, DateTimeImmutable $endTime): float |
||
202 | { |
||
203 | // SQL function: days2quantity() |
||
204 | |||
205 | $month = $month->modify('first day of this month 00:00'); |
||
206 | if ($startTime < $month) { |
||
207 | $startTime = $month; |
||
208 | } |
||
209 | if ($endTime > $month->modify('first day of next month 00:00')) { |
||
210 | $endTime = $month->modify('first day of next month 00:00'); |
||
211 | } |
||
212 | |||
213 | $secondsInMonth = $month->format('t') * 24 * 60 * 60; |
||
214 | |||
215 | return ($endTime->getTimestamp() - $startTime->getTimestamp()) / $secondsInMonth; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @Given /formula is (.+)/ |
||
220 | */ |
||
221 | public function formulaIs(string $formula): void |
||
222 | { |
||
223 | $this->formula = $formula; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @Given /formula continues (.+)/ |
||
228 | */ |
||
229 | public function formulaContinues(string $formula): void |
||
230 | { |
||
231 | $this->formula .= "\n" . $formula; |
||
232 | } |
||
233 | |||
234 | protected function getFormulaEngine() |
||
235 | { |
||
236 | if ($this->engine === null) { |
||
237 | $this->engine = new FormulaEngine(new ArrayCachePool()); |
||
238 | } |
||
239 | |||
240 | return $this->engine; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @When /action date is (.+)/ |
||
245 | * @throws Exception |
||
246 | */ |
||
247 | public function actionDateIs(string $date): void |
||
248 | { |
||
249 | $this->action->setTime(new DateTimeImmutable($date)); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @Given /^client rejected service at (.+)$/ |
||
254 | */ |
||
255 | public function actionCloseDateIs(string $close_date): void |
||
256 | { |
||
257 | $this->sale->close(new DateTimeImmutable($close_date)); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @Then /^error is$/m |
||
262 | */ |
||
263 | public function multilineErrorIs(\Behat\Gherkin\Node\PyStringNode $value) |
||
264 | { |
||
265 | $this->expectedError = $value->getRaw(); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @Then /^error is (.+)$/ |
||
270 | * |
||
271 | * @param string $error |
||
272 | */ |
||
273 | public function errorIs($error): void |
||
274 | { |
||
275 | $this->expectedError = $error; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @Then /^(\w+) charge is ?(?: with ?)?$/ |
||
280 | */ |
||
281 | public function emptyCharge(string $numeral): void |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @Then /^(\w+) charge is (\S+) +(-?[0-9.]+) ([A-Z]{3})(?: for ([\d.]+)? (\w+)?)?(?: with (.+)?)?$/ |
||
288 | */ |
||
289 | public function chargeWithSum($numeral, $type = null, $sum = null, $currency = null, $qty = null, $unit = null, $events = null): void |
||
290 | { |
||
291 | $this->chargeIs($numeral, $type, $sum, $currency, null, $qty, $unit, $events); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @Then /^(\w+) charge is (\S+) +(-?[0-9.]+) ([A-Z]{3}) reason ([\w]+)(?: with (.+)?)?$/ |
||
296 | */ |
||
297 | public function chargeWithReason($numeral, $type = null, $sum = null, $currency = null, $reason = null, $events = null): void |
||
298 | { |
||
299 | $this->chargeIs($numeral, $type, $sum, $currency, $reason, null, null, $events); |
||
300 | } |
||
301 | |||
302 | public function chargeIs($numeral, $type = null, $sum = null, $currency = null, $reason = null, $qty = null, $unit = null, $events = null): void |
||
303 | { |
||
304 | $no = $this->ensureNo($numeral); |
||
305 | if ($no === 0) { |
||
306 | $this->calculatePrice(); |
||
307 | } |
||
308 | $this->assertCharge($this->charges[$no] ?? null, $type, $sum, $currency, $reason, $qty, $unit, $events); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @When /^calculating charges$/ |
||
313 | */ |
||
314 | public function calculatePrice(): void |
||
315 | { |
||
316 | $this->expectError(function () { |
||
317 | if ($this->formula !== null) { |
||
318 | $this->price->setModifier($this->getFormulaEngine()->build($this->formula)); |
||
319 | } |
||
320 | $this->charges = $this->billing->calculateCharges($this->action); |
||
321 | }); |
||
322 | } |
||
323 | |||
324 | public function expectError(Closure $closure): void |
||
325 | { |
||
326 | try { |
||
327 | call_user_func($closure); |
||
328 | } catch (Exception $e) { |
||
329 | if ($this->isExpectedError($e)) { |
||
330 | $this->expectedError = null; |
||
331 | } else { |
||
332 | throw $e; |
||
333 | } |
||
334 | } |
||
335 | if ($this->expectedError) { |
||
336 | throw new Exception('failed receive expected exception'); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | protected function isExpectedError(Exception $e): bool |
||
341 | { |
||
342 | return str_starts_with($e->getMessage(), $this->expectedError); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param ChargeInterface|Charge|null $charge |
||
347 | * @param string|null $type |
||
348 | * @param string|null $sum |
||
349 | * @param string|null $currency |
||
350 | * @param string|null $reason |
||
351 | * @param string|null $qty |
||
352 | * @param string|null $unit |
||
353 | * @param string|null $events |
||
354 | */ |
||
355 | public function assertCharge($charge, $type, $sum, $currency, $reason, $qty, $unit, $events): void |
||
356 | { |
||
357 | if (empty($type) && empty($sum) && empty($currency)) { |
||
358 | is_null($charge) || Assert::assertSame('0', $charge->getSum()->getAmount()); |
||
359 | return; |
||
360 | } |
||
361 | Assert::assertInstanceOf(Charge::class, $charge); |
||
362 | Assert::assertSame($type, $this->normalizeType($charge->getType()->getName()), sprintf( |
||
363 | 'Charge type %s does not match expected %s', $type, $this->normalizeType($charge->getType()->getName()) |
||
364 | )); |
||
365 | $money = $this->moneyParser->parse($sum, new Currency($currency)); |
||
366 | Assert::assertTrue($money->equals($charge->getSum()), sprintf( |
||
367 | 'Charge sum %s does not match expected %s', $charge->getSum()->getAmount(), $money->getAmount() |
||
368 | )); |
||
369 | if ($reason !== null) { |
||
370 | Assert::assertSame($reason, $charge->getComment(), |
||
371 | sprintf('Charge comment %s does not match expected %s', $charge->getComment(), $reason) |
||
372 | ); |
||
373 | } |
||
374 | if ($qty !== null && $unit !== null) { |
||
375 | Assert::assertEqualsWithDelta($qty, $charge->getUsage()->getQuantity(), 1e-7, |
||
376 | sprintf('Charge quantity "%s" does not match expected "%s"', $charge->getUsage()->getQuantity(), $qty) |
||
377 | ); |
||
378 | Assert::assertSame($unit, $charge->getUsage()->getUnit()->getName(), |
||
379 | sprintf('Charge unit "%s" does not match expected "%s"', $charge->getUsage()->getUnit()->getName(), $unit) |
||
380 | ); |
||
381 | } |
||
382 | if ($events !== null) { |
||
383 | $storedEvents = $charge->releaseEvents(); |
||
384 | foreach (array_map('trim', explode(',', $events)) as $eventClass) { |
||
385 | foreach ($storedEvents as $storedEvent) { |
||
386 | $eventReflection = new \ReflectionObject($storedEvent); |
||
387 | if ($eventReflection->getShortName() === $eventClass) { |
||
388 | continue 2; |
||
389 | } |
||
390 | } |
||
391 | |||
392 | Assert::fail(sprintf('Event of class %s is not present is charge', $eventClass)); |
||
393 | } |
||
394 | } else { |
||
395 | Assert::assertEmpty($charge->releaseEvents(), 'Failed asserting that charge does not have events'); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | private function normalizeType($string): string |
||
400 | { |
||
401 | switch ($string) { |
||
402 | case 'discount,discount': |
||
403 | return 'discount'; |
||
404 | case 'monthly,leasing': |
||
405 | return 'leasing'; |
||
406 | case 'monthly,installment': |
||
407 | return 'installment'; |
||
408 | default: |
||
409 | return $string; |
||
410 | } |
||
411 | } |
||
412 | |||
413 | private function ensureNo(string $numeral): int |
||
414 | { |
||
415 | $formatter = new NumberFormatter('en_EN', NumberFormatter::SPELLOUT); |
||
416 | $result = $formatter->parse($numeral); |
||
417 | if ($result === false) { |
||
418 | throw new Exception("Wrong numeral '$numeral'"); |
||
419 | } |
||
420 | |||
421 | return --$result; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @Given /^progressive price calculation steps are (.*)$/ |
||
426 | */ |
||
427 | public function progressivePriceCalculationStepsAre($explanation) |
||
436 | } |
||
437 | } |
||
438 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths