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 ServiceFactory 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 ServiceFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class ServiceFactory |
||
20 | { |
||
21 | /** |
||
22 | * Creates and initializes service instance from a given configuration. |
||
23 | * |
||
24 | * @param $config configuration of the service |
||
25 | * |
||
26 | * @return object the initialized service instance |
||
27 | */ |
||
28 | public static function createService(array $config) |
||
40 | |||
41 | /** |
||
42 | * Builds an arbitrary service/object instance from a config-obect. |
||
43 | * |
||
44 | * @static |
||
45 | * |
||
46 | * @param object $config configuration object to build a service instance from. |
||
47 | * @param array $arguments arguments for the service constructor |
||
48 | * @param string $defaultClass class to create instance for if none is set in config |
||
49 | * |
||
50 | * @return object build and modified srvice instance |
||
51 | */ |
||
52 | public static function doBuild($config, $arguments, $defaultClass = false) |
||
72 | |||
73 | /** |
||
74 | * Utility function to build an object instance for given class with given constructor-arguments. |
||
75 | * |
||
76 | * @see GenericBuilder |
||
77 | * @static |
||
78 | * |
||
79 | * @param string $className name of class to build instance for. |
||
80 | * @param array $arguments arguments for the constructor |
||
81 | * |
||
82 | * @return object build and modified srvice instance |
||
83 | */ |
||
84 | public static function build($className, $arguments) |
||
88 | |||
89 | /** |
||
90 | * Builds single argument (to call a method with later) from a config-obect. |
||
91 | * |
||
92 | * @param object $config configuration object to create argument from. |
||
93 | * |
||
94 | * @return mixed build argument |
||
95 | */ |
||
96 | protected static function buildArg($config) |
||
125 | |||
126 | /** |
||
127 | * Builds and modifies an arbitrary service/object instance from a config-obect. |
||
128 | * |
||
129 | * @see XMLContext::doBuild |
||
130 | * @see PEIP\Factory\ServiceFactory::modifyService |
||
131 | * @implements \PEIP\INF\Context\Context |
||
132 | * |
||
133 | * @param object $config configuration array to build a service instance from. |
||
134 | * @param array $arguments arguments for the service constructor |
||
135 | * @param string $defaultClass class to create instance for if none is set in config |
||
136 | * |
||
137 | * @return object build and modified srvice instance |
||
138 | */ |
||
139 | public static function buildAndModify(array $config, $arguments, $defaultClass = '') |
||
164 | |||
165 | /** |
||
166 | * Modifies a service instance from configuration. |
||
167 | * - Sets properties on the instance. |
||
168 | * -- Calls a public setter method if exists. |
||
169 | * -- Else sets a public property if exists. |
||
170 | * - Calls methods on the instance. |
||
171 | * - Registers listeners to events on the instance. |
||
172 | * |
||
173 | * @param object $service the service instance to modify |
||
174 | * @param object $config configuration to get the modification instructions from. |
||
175 | * |
||
176 | * @return object the modificated service |
||
177 | */ |
||
178 | protected function modifyService($service, $config) |
||
221 | } |
||
222 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: