Complex classes like InterfaceService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 InterfaceService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class InterfaceService { |
||
| 50 | |||
| 51 | const IFACE0 = 1; |
||
| 52 | const IFACE1 = 2; |
||
| 53 | const IFACE2 = 3; |
||
| 54 | const IFACE3 = 4; |
||
| 55 | const IFACE4 = 5; |
||
| 56 | const IFACE_INTERNAL = 6; |
||
| 57 | const IFACE_FRONTAL = 7; |
||
| 58 | const IFACE_TEST = 99; |
||
| 59 | |||
| 60 | public static $LIST_IFACE = [ |
||
| 61 | self::IFACE_INTERNAL => 'internal', |
||
| 62 | self::IFACE_FRONTAL => 'frontal', |
||
| 63 | self::IFACE0 => 'iface0', |
||
| 64 | self::IFACE1 => 'iface1', |
||
| 65 | self::IFACE2 => 'iface2', |
||
| 66 | self::IFACE3 => 'iface3', |
||
| 67 | self::IFACE4 => 'iface4', |
||
| 68 | ]; |
||
| 69 | |||
| 70 | |||
| 71 | use TStringTools; |
||
| 72 | use TArrayTools; |
||
| 73 | |||
| 74 | |||
| 75 | /** @var IURLGenerator */ |
||
| 76 | private $urlGenerator; |
||
| 77 | |||
| 78 | /** @var RemoteRequest */ |
||
| 79 | private $remoteRequest; |
||
| 80 | |||
| 81 | /** @var ConfigService */ |
||
| 82 | private $configService; |
||
| 83 | |||
| 84 | |||
| 85 | /** @var int */ |
||
| 86 | private $currentInterface = 0; |
||
| 87 | |||
| 88 | /** @var int */ |
||
| 89 | private $outgoingInterface = 0; |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * InterfaceService constructor. |
||
| 94 | * |
||
| 95 | * @param IURLGenerator $urlGenerator |
||
| 96 | * @param RemoteRequest $remoteRequest |
||
| 97 | * @param ConfigService $configService |
||
| 98 | */ |
||
| 99 | public function __construct( |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * @param int $interface |
||
| 112 | */ |
||
| 113 | public function setCurrentInterface(int $interface): void { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return int |
||
| 119 | * @throws UnknownInterfaceException |
||
| 120 | */ |
||
| 121 | public function getCurrentInterface(): int { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function hasCurrentInterface(): bool { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @return bool |
||
| 139 | * @throws UnknownInterfaceException |
||
| 140 | */ |
||
| 141 | public function isCurrentInterfaceInternal(): bool { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param int $interface |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isInterfaceInternal(int $interface): bool { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @param IRequest $request |
||
| 174 | * @param string $testToken |
||
| 175 | */ |
||
| 176 | public function setCurrentInterfaceFromRequest(IRequest $request, string $testToken = ''): void { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $instance |
||
| 209 | * |
||
| 210 | * @return int |
||
| 211 | * @throws RemoteNotFoundException |
||
| 212 | */ |
||
| 213 | public function getInterfaceFromInstance(string $instance): int { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * |
||
| 221 | */ |
||
| 222 | public function setCurrentInterfaceFromInstance(string $instance): void { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @param bool $useString |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function getInterfaces(bool $useString = false): array { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * @param bool $useString |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function getInternalInterfaces(bool $useString = false): array { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * use this only if interface must be defined. If not, use getLocalInstance() |
||
| 291 | * |
||
| 292 | * @throws UnknownInterfaceException |
||
| 293 | */ |
||
| 294 | public function getCloudInstance(): string { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @throws UnknownInterfaceException |
||
| 316 | */ |
||
| 317 | public function getCloudPath(string $route = '', array $args = []): string { |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * should be used when unsure about the used Interface |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function getLocalInstance(): string { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function getTestingInstance(): string { |
||
| 383 | |||
| 384 | } |
||
| 385 | |||
| 386 |