| Total Complexity | 42 |
| Total Lines | 156 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BillingRegistryService 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 BillingRegistryService, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 22 | final class BillingRegistryService implements BillingRegistryServiceInterface |
||
| 23 | { |
||
| 24 | public function __construct(private readonly BillingRegistryInterface $registry) |
||
| 26 | } |
||
| 27 | |||
| 28 | public function getRepresentationsByType(string $representationClass): array |
||
| 29 | { |
||
| 30 | if (!class_exists($representationClass) && !interface_exists($representationClass)) { |
||
| 31 | throw new InvalidRepresentationException("Class '$representationClass' does not exist"); |
||
| 32 | } |
||
| 33 | |||
| 34 | if (class_exists($representationClass) && !is_subclass_of($representationClass, RepresentationInterface::class)) { |
||
| 35 | throw new InvalidBehaviorException( |
||
| 36 | sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass) |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | $representations = []; |
||
| 41 | foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
||
| 42 | foreach ($priceTypeDefinition->documentRepresentation() as $representation) { |
||
| 43 | if ($representation instanceof $representationClass) { |
||
| 44 | $representations[] = $representation; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | return $representations; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function getTariffDefinitionByName(string $tariffName): ?TariffTypeDefinitionInterface |
||
| 53 | { |
||
| 54 | foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) { |
||
| 55 | if ($tariffName === $tariffTypeDefinition->tariffType()->name) { |
||
| 56 | return $tariffTypeDefinition; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | return null; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function hasBehaviour(TariffTypeDefinitionInterface $tariffTypeDefinition, string $behaviorClassWrapper): bool |
||
| 63 | { |
||
| 64 | return $tariffTypeDefinition->hasBehavior($behaviorClassWrapper); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface { |
||
| 68 | $type = $this->convertStringTypeToType($type); |
||
| 69 | |||
| 70 | foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
||
| 71 | if ($priceTypeDefinition->hasType($type)) { |
||
| 72 | return $priceTypeDefinition->createQuantityFormatter($data); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | throw new QuantityFormatterNotFoundException('Quantity formatter not found'); |
||
| 77 | } |
||
| 78 | |||
| 79 | private function convertStringTypeToType(string $type): TypeInterface |
||
| 80 | { |
||
| 81 | return Type::anyId($type); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface |
||
| 85 | { |
||
| 86 | if (!class_exists($behaviorClassWrapper)) { |
||
| 87 | throw new InvalidBehaviorException( |
||
| 88 | sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper) |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) { |
||
| 93 | throw new InvalidBehaviorException( |
||
| 94 | sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper) |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | $billingType = $this->convertStringTypeToType($type); |
||
| 99 | |||
| 100 | foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
||
| 101 | if ($priceTypeDefinition->hasType($billingType)) { |
||
| 102 | $behavior = $this->findBehaviorInPriceType($priceTypeDefinition, $behaviorClassWrapper); |
||
| 103 | |||
| 104 | if ($behavior) { |
||
| 105 | return $behavior; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | throw new BehaviorNotFoundException( |
||
| 111 | sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type), |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | private function findBehaviorInPriceType( |
||
| 116 | PriceTypeDefinitionInterface $priceTypeDefinition, |
||
| 117 | string $behaviorClassWrapper |
||
| 118 | ): ?BehaviorInterface { |
||
| 119 | foreach ($priceTypeDefinition->withBehaviors() as $behavior) { |
||
| 120 | if ($behavior instanceof $behaviorClassWrapper) { |
||
| 121 | return $behavior; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return null; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getBehaviors(string $behaviorClassWrapper): \Generator |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getAggregate(string $type): AggregateInterface |
||
| 148 | { |
||
| 149 | $type = $this->convertStringTypeToType($type); |
||
| 150 | |||
| 151 | foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
||
| 152 | if ($priceTypeDefinition->hasType($type)) { |
||
| 153 | return $priceTypeDefinition->getAggregate(); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | throw new AggregateNotFoundException('Aggregate was not found'); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function findTariffTypeDefinitionByBehavior(BehaviorInterface $behavior): TariffTypeDefinitionInterface |
||
| 171 | } |
||
| 172 | |||
| 173 | public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator |
||
| 174 | { |
||
| 175 | foreach ($this->registry->priceTypes() as $priceTypeDefinition) { |
||
| 176 | if ($priceTypeDefinition->hasBehavior($behaviorClassWrapper)) { |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 182 |
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