| Total Complexity | 53 |
| Total Lines | 415 |
| Duplicated Lines | 0 % |
| Changes | 19 | ||
| 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 |
||
| 41 | class FeatureContext implements Context |
||
| 42 | { |
||
| 43 | private const PROGRESSIVE_SIGN_LESS = 'less'; |
||
| 44 | private const PROGRESSIVE_SIGN_LESS_EQUAL = 'less_equal'; |
||
| 45 | private const PROGRESSIVE_SIGN_GREAT = 'great'; |
||
| 46 | private const PROGRESSIVE_SIGN_GREAT_EQUAL = 'great_equal'; |
||
| 47 | private const PROGRESSIVE_SIGN_EQUAL = 'equal'; |
||
| 48 | private const PROGRESSIVE_SIGNS = [ |
||
| 49 | self::PROGRESSIVE_SIGN_LESS => ProgressivePrice::SIGN_LESS, |
||
| 50 | self::PROGRESSIVE_SIGN_LESS_EQUAL => ProgressivePrice::SIGN_LESS_EQUAL, |
||
| 51 | self::PROGRESSIVE_SIGN_GREAT => ProgressivePrice::SIGN_GREATER, |
||
| 52 | self::PROGRESSIVE_SIGN_GREAT_EQUAL => ProgressivePrice::SIGN_GREATER_EQUAL, |
||
| 53 | self::PROGRESSIVE_SIGN_EQUAL => ProgressivePrice::SIGN_EQUAL, |
||
| 54 | ]; |
||
| 55 | protected $engine; |
||
| 56 | |||
| 57 | /** @var Customer */ |
||
| 58 | protected $customer; |
||
| 59 | /** |
||
| 60 | * @var \hiqdev\php\billing\price\PriceInterface|\hiqdev\php\billing\charge\FormulaChargeModifierTrait |
||
|
|
|||
| 61 | * |
||
| 62 | * TODO: FormulaChargeModifierTrait::setFormula() must be moved to interface |
||
| 63 | */ |
||
| 64 | protected $price; |
||
| 65 | |||
| 66 | /** @var string */ |
||
| 67 | protected $formula; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \hiqdev\php\billing\action\ActionInterface|\hiqdev\php\billing\action\AbstractAction |
||
| 71 | */ |
||
| 72 | protected $action; |
||
| 73 | /** |
||
| 74 | * @var ChargeInterface[] |
||
| 75 | */ |
||
| 76 | protected $charges; |
||
| 77 | |||
| 78 | /** @var \Money\MoneyParser */ |
||
| 79 | protected $moneyParser; |
||
| 80 | |||
| 81 | /** @var string */ |
||
| 82 | protected $expectedError; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initializes context. |
||
| 86 | */ |
||
| 87 | public function __construct() |
||
| 88 | { |
||
| 89 | error_reporting(E_ALL & ~E_DEPRECATED); |
||
| 90 | date_default_timezone_set('UTC'); |
||
| 91 | $this->customer = new Customer(null, 'somebody'); |
||
| 92 | $this->moneyParser = new DecimalMoneyParser(new ISOCurrencies()); |
||
| 93 | $this->plan = new Plan(null, 'plan', $this->customer); |
||
| 94 | $this->sale = new Sale(null, Target::any(), $this->customer, $this->plan, new DateTimeImmutable('2000-01-01')); |
||
| 95 | $this->billing = SimpleBilling::fromSale($this->sale); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @Given /(\S+) (\S+) price is ([0-9.]+) (\w+) per (\w+)(?: includes ([\d.]+))?/ |
||
| 100 | */ |
||
| 101 | public function priceIs($target, $type, $sum, $currency, $unit, $quantity = 0) |
||
| 102 | { |
||
| 103 | $type = new Type(Type::ANY, $type); |
||
| 104 | $target = new Target(Target::ANY, $target); |
||
| 105 | $quantity = Quantity::create($unit, $quantity); |
||
| 106 | $sum = $this->moneyParser->parse($sum, new Currency($currency)); |
||
| 107 | $this->setPrice(new SinglePrice(null, $type, $target, null, $quantity, $sum)); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @Given /(\S+) progressive price for (\S+) is +(\S+) (\S+) per (\S+) (\S+) (\S+) (\S+)$/ |
||
| 112 | */ |
||
| 113 | public function progressivePrice($target, $type, $price, $currency, $unit, $sign, $quantity, $perUnit): void |
||
| 136 | ] |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @Given /(\S+) progressive price for (\S+) is +(\S+) (\S+) per (\S+) (\S+) (\S+) (\S+) and (\S+) (\S+) (\S+)$/ |
||
| 143 | */ |
||
| 144 | public function progressivePriceWithInterval( |
||
| 145 | $target, |
||
| 146 | $type, |
||
| 147 | $price, |
||
| 148 | $currency, |
||
| 149 | $unit, |
||
| 150 | $signFrom, |
||
| 151 | $quantityFrom, |
||
| 152 | $perUnit, |
||
| 153 | $signTill, |
||
| 154 | $quantityTill, |
||
| 155 | $perUnit1 |
||
| 156 | ) { |
||
| 157 | if (empty($this->progressivePrice[$type])) { |
||
| 158 | $this->progressivePrice[$type] = [ |
||
| 159 | 'target' => $target, |
||
| 160 | 'unit' => $unit, |
||
| 161 | 'condition' =>[ |
||
| 162 | [ |
||
| 163 | 'price' => $price, |
||
| 164 | 'currency' => $currency, |
||
| 165 | 'sign_from' => self::PROGRESSIVE_SIGNS[$signFrom], |
||
| 166 | 'value_from' => $quantityFrom, |
||
| 167 | 'sign_till' => self::PROGRESSIVE_SIGNS[$signTill], |
||
| 168 | 'value_till' => $quantityTill, |
||
| 169 | ], |
||
| 170 | ] , |
||
| 171 | ]; |
||
| 172 | } else { |
||
| 173 | array_push( |
||
| 174 | $this->progressivePrice[$type]['condition'], |
||
| 175 | [ |
||
| 176 | 'price' => $price, |
||
| 177 | 'currency' => $currency, |
||
| 178 | 'sign_from' => self::PROGRESSIVE_SIGNS[$signFrom], |
||
| 179 | 'value_from' => $quantityFrom, |
||
| 180 | 'sign_till' => self::PROGRESSIVE_SIGNS[$signTill], |
||
| 181 | 'value_till' => $quantityTill, |
||
| 182 | ] |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @Given /^create progressive price/ |
||
| 189 | */ |
||
| 190 | public function createProgressivePrices() |
||
| 191 | { |
||
| 192 | foreach ($this->progressivePrice as $type => $price) { |
||
| 193 | $type = new Type(Type::ANY, $type); |
||
| 194 | $target = new Target(Target::ANY, $price['target']); |
||
| 195 | $quantity = Quantity::create($price['unit'], 1); |
||
| 196 | $this->setPrice(new ProgressivePrice(null, $type, $target, $quantity, $price['condition'])); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @Given /sale close time is ([0-9.-]+)/ |
||
| 202 | */ |
||
| 203 | public function setActionCloseTime($closeTime): void |
||
| 204 | { |
||
| 205 | $this->sale->close(new DateTimeImmutable($closeTime)); |
||
| 206 | } |
||
| 207 | |||
| 208 | private function setPrice($price) |
||
| 209 | { |
||
| 210 | $this->price = $price; |
||
| 211 | $ref = new \ReflectionClass($this->plan); |
||
| 212 | $prop = $ref->getProperty('prices'); |
||
| 213 | $prop->setAccessible(true); |
||
| 214 | $prop->setValue($this->plan, [$price]); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @Given /action is (\S+) ([\w_,]+)(?: ([0-9.]+) (\S+))?(?: in (.+))?/ |
||
| 219 | */ |
||
| 220 | public function actionIs(string $target, string $type, float $amount, string $unit, ?string $date = null): void |
||
| 221 | { |
||
| 222 | $type = new Type(Type::ANY, $type); |
||
| 223 | $target = new Target(Target::ANY, $target); |
||
| 224 | $time = new DateTimeImmutable($date); |
||
| 225 | if ($this->sale->getCloseTime() instanceof DateTimeImmutable) { |
||
| 226 | $amount = $amount * $this->getFractionOfMonth( |
||
| 227 | $time, $time, $this->sale->getCloseTime() |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | $quantity = Quantity::create($unit, $amount); |
||
| 231 | |||
| 232 | $this->action = new Action(null, $type, $target, $quantity, $this->customer, $time); |
||
| 233 | } |
||
| 234 | |||
| 235 | private function getFractionOfMonth(DateTimeImmutable $month, DateTimeImmutable $startTime, DateTimeImmutable $endTime): float |
||
| 236 | { |
||
| 237 | // SQL function: days2quantity() |
||
| 238 | |||
| 239 | $month = $month->modify('first day of this month 00:00'); |
||
| 240 | if ($startTime < $month) { |
||
| 241 | $startTime = $month; |
||
| 242 | } |
||
| 243 | if ($endTime > $month->modify('first day of next month 00:00')) { |
||
| 244 | $endTime = $month->modify('first day of next month 00:00'); |
||
| 245 | } |
||
| 246 | |||
| 247 | $secondsInMonth = $month->format('t') * 24 * 60 * 60; |
||
| 248 | |||
| 249 | return ($endTime->getTimestamp() - $startTime->getTimestamp()) / $secondsInMonth; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @Given /formula is (.+)/ |
||
| 254 | */ |
||
| 255 | public function formulaIs(string $formula): void |
||
| 256 | { |
||
| 257 | $this->formula = $formula; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Given /formula continues (.+)/ |
||
| 262 | */ |
||
| 263 | public function formulaContinues(string $formula): void |
||
| 264 | { |
||
| 265 | $this->formula .= "\n" . $formula; |
||
| 266 | } |
||
| 267 | |||
| 268 | protected function getFormulaEngine() |
||
| 269 | { |
||
| 270 | if ($this->engine === null) { |
||
| 271 | $this->engine = new FormulaEngine(new ArrayCachePool()); |
||
| 272 | } |
||
| 273 | |||
| 274 | return $this->engine; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @When /action date is (.+)/ |
||
| 279 | * @throws Exception |
||
| 280 | */ |
||
| 281 | public function actionDateIs(string $date): void |
||
| 282 | { |
||
| 283 | $this->action->setTime(new DateTimeImmutable($date)); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Given /^client rejected service at (.+)$/ |
||
| 288 | */ |
||
| 289 | public function actionCloseDateIs(string $close_date): void |
||
| 290 | { |
||
| 291 | $this->sale->close(new DateTimeImmutable($close_date)); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @Then /^error is$/m |
||
| 296 | */ |
||
| 297 | public function multilineErrorIs(\Behat\Gherkin\Node\PyStringNode $value) |
||
| 298 | { |
||
| 299 | $this->expectedError = $value->getRaw(); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Then /^error is (.+)$/ |
||
| 304 | * |
||
| 305 | * @param string $error |
||
| 306 | */ |
||
| 307 | public function errorIs($error): void |
||
| 308 | { |
||
| 309 | $this->expectedError = $error; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @Then /^(\w+) charge is ?(?: with ?)?$/ |
||
| 314 | */ |
||
| 315 | public function emptyCharge(string $numeral): void |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @Then /^(\w+) charge is (\S+) +(-?[0-9.]+) ([A-Z]{3})(?: for ([\d.]+)? (\w+)?)?(?: with (.+)?)?$/ |
||
| 322 | */ |
||
| 323 | public function chargeWithSum($numeral, $type = null, $sum = null, $currency = null, $qty = null, $unit = null, $events = null): void |
||
| 324 | { |
||
| 325 | $this->chargeIs($numeral, $type, $sum, $currency, null, $qty, $unit, $events); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @Then /^(\w+) charge is (\S+) +(-?[0-9.]+) ([A-Z]{3}) reason ([\w]+)(?: with (.+)?)?$/ |
||
| 330 | */ |
||
| 331 | public function chargeWithReason($numeral, $type = null, $sum = null, $currency = null, $reason = null, $events = null): void |
||
| 332 | { |
||
| 333 | $this->chargeIs($numeral, $type, $sum, $currency, $reason, null, null, $events); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function chargeIs($numeral, $type = null, $sum = null, $currency = null, $reason = null, $qty = null, $unit = null, $events = null): void |
||
| 337 | { |
||
| 338 | $no = $this->ensureNo($numeral); |
||
| 339 | if ($no === 0) { |
||
| 340 | $this->calculatePrice(); |
||
| 341 | } |
||
| 342 | $this->assertCharge($this->charges[$no] ?? null, $type, $sum, $currency, $reason, $qty, $unit, $events); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @When /^calculating charges$/ |
||
| 347 | */ |
||
| 348 | public function calculatePrice(): void |
||
| 349 | { |
||
| 350 | $this->expectError(function () { |
||
| 351 | if ($this->formula !== null) { |
||
| 352 | $this->price->setModifier($this->getFormulaEngine()->build($this->formula)); |
||
| 353 | } |
||
| 354 | $this->charges = $this->billing->calculateCharges($this->action); |
||
| 355 | }); |
||
| 356 | } |
||
| 357 | |||
| 358 | public function expectError(Closure $closure): void |
||
| 359 | { |
||
| 360 | try { |
||
| 361 | call_user_func($closure); |
||
| 362 | } catch (Exception $e) { |
||
| 363 | if ($this->isExpectedError($e)) { |
||
| 364 | $this->expectedError = null; |
||
| 365 | } else { |
||
| 366 | throw $e; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | if ($this->expectedError) { |
||
| 370 | throw new Exception('failed receive expected exception'); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | protected function isExpectedError(Exception $e): bool |
||
| 375 | { |
||
| 376 | return str_starts_with($e->getMessage(), $this->expectedError); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param ChargeInterface|Charge|null $charge |
||
| 381 | * @param string|null $type |
||
| 382 | * @param string|null $sum |
||
| 383 | * @param string|null $currency |
||
| 384 | * @param string|null $reason |
||
| 385 | * @param string|null $qty |
||
| 386 | * @param string|null $unit |
||
| 387 | * @param string|null $events |
||
| 388 | */ |
||
| 389 | public function assertCharge($charge, $type, $sum, $currency, $reason, $qty, $unit, $events): void |
||
| 390 | { |
||
| 391 | if (empty($type) && empty($sum) && empty($currency)) { |
||
| 392 | is_null($charge) || Assert::assertSame('0', $charge->getSum()->getAmount()); |
||
| 393 | return; |
||
| 394 | } |
||
| 395 | Assert::assertInstanceOf(Charge::class, $charge); |
||
| 396 | Assert::assertSame($type, $this->normalizeType($charge->getType()->getName()), sprintf( |
||
| 397 | 'Charge type %s does not match expected %s', $type, $this->normalizeType($charge->getType()->getName()) |
||
| 398 | )); |
||
| 399 | $money = $this->moneyParser->parse($sum, new Currency($currency)); |
||
| 400 | Assert::assertTrue($money->equals($charge->getSum()), sprintf( |
||
| 401 | 'Charge sum %s does not match expected %s', $charge->getSum()->getAmount(), $money->getAmount() |
||
| 402 | )); |
||
| 403 | if ($reason !== null) { |
||
| 404 | Assert::assertSame($reason, $charge->getComment(), |
||
| 405 | sprintf('Charge comment %s does not match expected %s', $charge->getComment(), $reason) |
||
| 406 | ); |
||
| 407 | } |
||
| 408 | if ($qty !== null && $unit !== null) { |
||
| 409 | Assert::assertEqualsWithDelta($qty, $charge->getUsage()->getQuantity(), 1e-7, |
||
| 410 | sprintf('Charge quantity "%s" does not match expected "%s"', $charge->getUsage()->getQuantity(), $qty) |
||
| 411 | ); |
||
| 412 | Assert::assertSame($unit, $charge->getUsage()->getUnit()->getName(), |
||
| 413 | sprintf('Charge unit "%s" does not match expected "%s"', $charge->getUsage()->getUnit()->getName(), $unit) |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | if ($events !== null) { |
||
| 417 | $storedEvents = $charge->releaseEvents(); |
||
| 418 | foreach (array_map('trim', explode(',', $events)) as $eventClass) { |
||
| 419 | foreach ($storedEvents as $storedEvent) { |
||
| 420 | $eventReflection = new \ReflectionObject($storedEvent); |
||
| 421 | if ($eventReflection->getShortName() === $eventClass) { |
||
| 422 | continue 2; |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | Assert::fail(sprintf('Event of class %s is not present is charge', $eventClass)); |
||
| 427 | } |
||
| 428 | } else { |
||
| 429 | Assert::assertEmpty($charge->releaseEvents(), 'Failed asserting that charge does not have events'); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | private function normalizeType($string): string |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | private function ensureNo(string $numeral): int |
||
| 448 | { |
||
| 449 | $formatter = new NumberFormatter('en_EN', NumberFormatter::SPELLOUT); |
||
| 450 | $result = $formatter->parse($numeral); |
||
| 451 | if ($result === false) { |
||
| 452 | throw new Exception("Wrong numeral '$numeral'"); |
||
| 453 | } |
||
| 454 | |||
| 455 | return --$result; |
||
| 456 | } |
||
| 457 | } |
||
| 458 |
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