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 Swagger 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 Swagger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Swagger extends AbstractDocumentableObject |
||
14 | { |
||
15 | |||
16 | private $swagger = '2.0'; |
||
17 | private $host; |
||
18 | private $basePath; |
||
19 | |||
20 | /** |
||
21 | * @var Info $Info |
||
22 | */ |
||
23 | private $info; |
||
24 | private $schemes = array(); |
||
25 | private $consumes = array(); |
||
26 | private $produces = array(); |
||
27 | |||
28 | /** |
||
29 | * @var \SwaggerGen\Swagger\Path[] $Paths |
||
30 | */ |
||
31 | private $paths = array(); |
||
32 | |||
33 | /** |
||
34 | * @var \SwaggerGen\Swagger\Schema[] $definitions |
||
35 | */ |
||
36 | private $definitions = array(); |
||
37 | |||
38 | /** |
||
39 | * @var \SwaggerGen\Swagger\IParameter[] $parameters |
||
40 | */ |
||
41 | private $parameters = array(); |
||
42 | |||
43 | /** |
||
44 | * @var \SwaggerGen\Swagger\Response[] $responses |
||
45 | */ |
||
46 | private $responses = array(); |
||
47 | |||
48 | /** |
||
49 | * @var Tag[] $Tags |
||
50 | */ |
||
51 | private $tags = array(); |
||
52 | |||
53 | /** |
||
54 | * Default tag for new endpoints/operations. Set by the api command. |
||
55 | * @var Tag |
||
56 | */ |
||
57 | private $defaultTag = null; |
||
58 | private $securityDefinitions = array(); |
||
59 | private $security = array(); |
||
60 | |||
61 | /** |
||
62 | * @inheritDoc |
||
63 | * @param string $host |
||
64 | * @param string $basePath |
||
65 | */ |
||
66 | public function __construct($host = null, $basePath = null) |
||
75 | |||
76 | /** |
||
77 | * @inheritDoc |
||
78 | */ |
||
79 | protected function getSwagger() |
||
83 | |||
84 | /** |
||
85 | * Return all consumes |
||
86 | * @todo Deprecate in favour of a getConsume($name); |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getConsumes() |
||
93 | |||
94 | /** |
||
95 | * Return the named security if it exists. Otherwise return FALSE |
||
96 | * @param string $name |
||
97 | * @return boolean|SecurityScheme |
||
98 | */ |
||
99 | public function getSecurity($name) |
||
107 | |||
108 | /** |
||
109 | * @param string $command |
||
110 | * @param string $data |
||
111 | * @return \SwaggerGen\Swagger\AbstractObject|boolean |
||
112 | */ |
||
113 | public function handleCommand($command, $data = null) |
||
260 | |||
261 | /** |
||
262 | * @inheritDoc |
||
263 | */ |
||
264 | public function toArray() |
||
304 | |||
305 | public function __toString() |
||
309 | |||
310 | /** |
||
311 | * Check if an operation with the given id exists. |
||
312 | * |
||
313 | * @param string $operationId |
||
314 | * @return boolean |
||
315 | */ |
||
316 | public function hasOperationId($operationId) |
||
326 | |||
327 | } |
||
328 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.