Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like S3EndpointMiddleware 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 S3EndpointMiddleware, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class S3EndpointMiddleware |
||
19 | { |
||
20 | private static $exclusions = [ |
||
21 | 'CreateBucket' => true, |
||
22 | 'DeleteBucket' => true, |
||
23 | 'ListBuckets' => true, |
||
24 | ]; |
||
25 | |||
26 | const NO_PATTERN = 0; |
||
27 | const DUALSTACK = 1; |
||
28 | const ACCELERATE = 2; |
||
29 | const ACCELERATE_DUALSTACK = 3; |
||
30 | const PATH_STYLE = 4; |
||
31 | const HOST_STYLE = 5; |
||
32 | |||
33 | /** @var bool */ |
||
34 | private $accelerateByDefault; |
||
35 | /** @var bool */ |
||
36 | private $dualStackByDefault; |
||
37 | /** @var bool */ |
||
38 | private $pathStyleByDefault; |
||
39 | /** @var string */ |
||
40 | private $region; |
||
41 | /** @var callable */ |
||
42 | private $nextHandler; |
||
43 | |||
44 | /** |
||
45 | * Create a middleware wrapper function |
||
46 | * |
||
47 | * @param string $region |
||
48 | * @param array $options |
||
49 | * |
||
50 | * @return callable |
||
51 | */ |
||
52 | public static function wrap($region, array $options) |
||
58 | |||
59 | public function __construct( |
||
73 | |||
74 | public function __invoke(CommandInterface $command, RequestInterface $request) |
||
105 | |||
106 | private static function isRequestHostStyleCompatible( |
||
116 | |||
117 | private function endpointPatternDecider( |
||
146 | |||
147 | private function canAccelerate(CommandInterface $command) |
||
152 | |||
153 | private function getBucketStyleHost(CommandInterface $command, $host) |
||
162 | |||
163 | View Code Duplication | private function applyHostStyleEndpoint( |
|
180 | |||
181 | private function applyDualStackEndpoint( |
||
197 | |||
198 | private function getDualStackHost() |
||
202 | |||
203 | View Code Duplication | private function applyAccelerateEndpoint( |
|
218 | |||
219 | private function getAccelerateHost(CommandInterface $command, $pattern) |
||
223 | |||
224 | private function getBucketlessPath($path, CommandInterface $command) |
||
229 | } |
||
230 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.