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 | * @param IRequest $request |
||
| 139 | * @param string $testToken |
||
| 140 | */ |
||
| 141 | public function setCurrentInterfaceFromRequest(IRequest $request, string $testToken = ''): void { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $instance |
||
| 174 | * |
||
| 175 | * @return int |
||
| 176 | * @throws RemoteNotFoundException |
||
| 177 | */ |
||
| 178 | public function getInterfaceFromInstance(string $instance): int { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * |
||
| 186 | */ |
||
| 187 | public function setCurrentInterfaceFromInstance(string $instance): void { |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param bool $useString |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getInterfaces(bool $useString = false): array { |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * use this only if interface must be defined. If not, use getLocalInstance() |
||
| 226 | * |
||
| 227 | * @throws UnknownInterfaceException |
||
| 228 | */ |
||
| 229 | public function getCloudInstance(): string { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * @throws UnknownInterfaceException |
||
| 251 | */ |
||
| 252 | public function getCloudPath(string $route = '', array $args = []): string { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * should be used when unsure about the used Interface |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getLocalInstance(): string { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | private function getTestingInstance(): string { |
||
| 318 | |||
| 319 | } |
||
| 320 | |||
| 321 |