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 |
||
| 18 | class ServiceFactory {
|
||
| 19 | /** |
||
| 20 | * Creates and initializes service instance from a given configuration. |
||
| 21 | * |
||
| 22 | * @access public |
||
| 23 | * @param $config configuration of the service |
||
| 24 | * @return object the initialized service instance |
||
| 25 | */ |
||
| 26 | public static function createService(array $config){
|
||
| 27 | $args = array(); |
||
| 28 | //build arguments for constructor |
||
| 29 | if(isset($config['constructor_arg'])){
|
||
| 30 | foreach($config['constructor_arg'] as $arg){
|
||
| 31 | $args[] = self::buildArg($arg); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | return self::buildAndModify($config, $args); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Builds an arbitrary service/object instance from a config-obect. |
||
| 39 | * |
||
| 40 | * @static |
||
| 41 | * @access protected |
||
| 42 | * @param object $config configuration object to build a service instance from. |
||
| 43 | * @param array $arguments arguments for the service constructor |
||
| 44 | * @param string $defaultClass class to create instance for if none is set in config |
||
| 45 | * @return object build and modified srvice instance |
||
| 46 | */ |
||
| 47 | public static function doBuild($config, $arguments, $defaultClass = false){
|
||
| 48 | $cls = isset($config["class"]) ? trim((string)$config["class"]) : (string)$defaultClass; |
||
| 49 | if($cls != ''){
|
||
| 50 | try {
|
||
| 51 | $constructor = isset($config["constructor"])?(string)$config["constructor"]:""; |
||
| 52 | if($constructor != '' && Test::assertMethod($cls, $constructor)){
|
||
| 53 | $service = call_user_func_array(array($cls, $constructor), $arguments); |
||
| 54 | }else{
|
||
| 55 | $service = self::build($cls, $arguments); |
||
|
|
|||
| 56 | } |
||
| 57 | }catch(\Exception $e){
|
||
| 58 | throw new \RuntimeException('Could not create Service "'.$cls.'" -> '.$e->getMessage());
|
||
| 59 | } |
||
| 60 | } |
||
| 61 | if(is_object($service)){
|
||
| 62 | return $service; |
||
| 63 | } |
||
| 64 | throw new \RuntimeException('Could not create Service "'.$cls.'". Class does not exist.');
|
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Utility function to build an object instance for given class with given constructor-arguments. |
||
| 70 | * |
||
| 71 | * @see GenericBuilder |
||
| 72 | * @static |
||
| 73 | * @access protected |
||
| 74 | * @param object $className name of class to build instance for. |
||
| 75 | * @param array $arguments arguments for the constructor |
||
| 76 | * @return object build and modified srvice instance |
||
| 77 | */ |
||
| 78 | public static function build($className, $arguments){
|
||
| 81 | |||
| 82 | /** |
||
| 83 | * Builds single argument (to call a method with later) from a config-obect. |
||
| 84 | * |
||
| 85 | * @access protected |
||
| 86 | * @param object $config configuration object to create argument from. |
||
| 87 | * @return mixed build argument |
||
| 88 | */ |
||
| 89 | View Code Duplication | protected function buildArg($config){
|
|
| 116 | |||
| 117 | /** |
||
| 118 | * Builds and modifies an arbitrary service/object instance from a config-obect. |
||
| 119 | * |
||
| 120 | * @see XMLContext::doBuild |
||
| 121 | * @see PEIP\Factory\ServiceFactory::modifyService |
||
| 122 | * @implements \PEIP\INF\Context\Context |
||
| 123 | * @access public |
||
| 124 | * @param object $config configuration array to build a service instance from. |
||
| 125 | * @param array $arguments arguments for the service constructor |
||
| 126 | * @param string $defaultClass class to create instance for if none is set in config |
||
| 127 | * @return object build and modified srvice instance |
||
| 128 | */ |
||
| 129 | public static function buildAndModify(array $config, $arguments, $defaultClass = ""){
|
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Modifies a service instance from configuration. |
||
| 157 | * - Sets properties on the instance. |
||
| 158 | * -- Calls a public setter method if exists. |
||
| 159 | * -- Else sets a public property if exists. |
||
| 160 | * - Calls methods on the instance. |
||
| 161 | * - Registers listeners to events on the instance |
||
| 162 | * |
||
| 163 | * @access protected |
||
| 164 | * @param object $service the service instance to modify |
||
| 165 | * @param object $config configuration to get the modification instructions from. |
||
| 166 | * @return object the modificated service |
||
| 167 | */ |
||
| 168 | protected function modifyService($service, $config){
|
||
| 209 | } |
||
| 210 | |||
| 211 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: