| Total Complexity | 58 |
| Total Lines | 258 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Factory 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 Factory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Factory |
||
| 25 | { |
||
| 26 | private $entities = []; |
||
| 27 | |||
| 28 | private $factories = []; |
||
| 29 | |||
| 30 | protected $moneyParser; |
||
| 31 | |||
| 32 | public function __construct(array $factories) |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getMoney($data) |
||
| 39 | { |
||
| 40 | return $this->get('money', $data); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function parseMoney($str) |
||
| 50 | ]; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function createMoney($data) |
||
| 54 | { |
||
| 55 | return $this->moneyParser->parse($data['amount'], $data['currency']); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getCurrency($data) |
||
| 59 | { |
||
| 60 | return new Currency($data); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function getQuantity($data) |
||
| 64 | { |
||
| 65 | return $this->get('quantity', $data); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function parseQuantity($str) |
||
| 69 | { |
||
| 70 | [$quantity, $unit] = explode(' ', $str); |
||
| 71 | |||
| 72 | return [ |
||
| 73 | 'quantity' => $quantity, |
||
| 74 | 'unit' => $unit, |
||
| 75 | ]; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function createQuantity($data) |
||
| 79 | { |
||
| 80 | return Quantity::create($data['unit'], $data['quantity']); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function createUnit($data) |
||
| 84 | { |
||
| 85 | return Unit::create($data['name']); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getType($data) |
||
| 89 | { |
||
| 90 | return $this->get('type', $data); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getTarget($data) |
||
| 94 | { |
||
| 95 | return $this->get('target', $data); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function getPlan($data) |
||
| 101 | } |
||
| 102 | |||
| 103 | public function getCustomer($data) |
||
| 104 | { |
||
| 105 | return $this->get('customer', $data); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function get(string $entity, $data) |
||
| 109 | { |
||
| 110 | if (is_scalar($data)) { |
||
| 111 | $data = $this->parse($entity, $data); |
||
| 112 | } |
||
| 113 | |||
| 114 | $keys = $this->extractKeys($entity, $data); |
||
| 115 | |||
| 116 | $res = $this->find($entity, $keys) ?: $this->create($entity, $data); |
||
| 117 | |||
| 118 | foreach ($keys as $key) { |
||
| 119 | $this->entities[$entity][$key] = $res; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $res; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function parse(string $entity, $str) |
||
| 126 | { |
||
| 127 | $method = $this->getMethod($entity, 'parse'); |
||
| 128 | |||
| 129 | return $method ? $this->{$method}($str) : $this->parseByUnique($entity, $str); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function parseByUnique(string $entity, $str) |
||
| 133 | { |
||
| 134 | $keys = $this->getEntityUniqueKeys($entity); |
||
| 135 | if (count($keys) === 1) { |
||
| 136 | return [reset($keys) => $str]; |
||
| 137 | } |
||
| 138 | |||
| 139 | return ['id' => $str]; |
||
| 140 | } |
||
| 141 | |||
| 142 | public function find(string $entity, array $keys) |
||
| 143 | { |
||
| 144 | foreach ($keys as $key) { |
||
| 145 | if (!empty($this->entities[$entity][$key])) { |
||
| 146 | return $this->entities[$entity][$key]; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return null; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function create(string $entity, $data) |
||
| 154 | { |
||
| 155 | $method = $this->getMethod($entity, 'create'); |
||
| 156 | if ($method) { |
||
| 157 | return $this->{$method}($data); |
||
| 158 | } |
||
| 159 | |||
| 160 | if (empty($this->factories[$entity])) { |
||
| 161 | throw new FactoryNotFoundException($entity); |
||
|
|
|||
| 162 | } |
||
| 163 | |||
| 164 | $factory = $this->factories[$entity]; |
||
| 165 | |||
| 166 | return $factory->create($this->createDto($entity, $data)); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function createDto(string $entity, array $data) |
||
| 170 | { |
||
| 171 | $class = $this->getDtoClass($entity); |
||
| 172 | $dto = new $class(); |
||
| 173 | |||
| 174 | foreach ($data as $key => $value) { |
||
| 175 | $dto->{$key} = $this->prepareValue($entity, $key, $value); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $dto; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function getDtoClass(string $entity) |
||
| 182 | { |
||
| 183 | return $this->getEntityClass($entity) . 'CreationDto'; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function prepareValue($entity, $key, $value) |
||
| 187 | { |
||
| 188 | $method = $this->getPrepareMethod($entity, $key); |
||
| 189 | |||
| 190 | return $method ? $this->{$method}($value) : $value; |
||
| 191 | } |
||
| 192 | |||
| 193 | private function getMethod(string $entity, string $op) |
||
| 198 | } |
||
| 199 | |||
| 200 | private function getPrepareMethod(string $entity, string $key) |
||
| 222 | } |
||
| 223 | |||
| 224 | public function getEntityClass(string $entity) |
||
| 225 | { |
||
| 226 | $parts = explode('\\', __NAMESPACE__); |
||
| 227 | array_pop($parts); |
||
| 228 | $parts[] = $entity; |
||
| 229 | $parts[] = ucfirst($entity); |
||
| 230 | |||
| 231 | return implode('\\', $parts); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function extractKeys(string $entity, $data) |
||
| 235 | { |
||
| 236 | $id = $data['id'] ?? null; |
||
| 237 | $unique = $this->extractUnique($entity, $data); |
||
| 238 | |||
| 239 | return array_filter(['id' => $id, 'unique' => $unique]); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function extractUnique(string $entity, $data) |
||
| 258 | } |
||
| 259 | |||
| 260 | public function getEntityUniqueKeys(string $entity): array |
||
| 282 | } |
||
| 283 | } |
||
| 284 |
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