Conditions | 7 |
Paths | 12 |
Total Lines | 63 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public static function create( \Aimeos\MShop\Context\Item\Iface $context, $name = null ) |
||
35 | { |
||
36 | /** controller/common/order/name |
||
37 | * Class name of the used order common controller implementation |
||
38 | * |
||
39 | * Each default common controller can be replace by an alternative imlementation. |
||
40 | * To use this implementation, you have to set the last part of the class |
||
41 | * name as configuration value so the controller factory knows which class it |
||
42 | * has to instantiate. |
||
43 | * |
||
44 | * For example, if the name of the default class is |
||
45 | * |
||
46 | * \Aimeos\Controller\Common\Order\Standard |
||
47 | * |
||
48 | * and you want to replace it with your own version named |
||
49 | * |
||
50 | * \Aimeos\Controller\Common\Order\Myorder |
||
51 | * |
||
52 | * then you have to set the this configuration option: |
||
53 | * |
||
54 | * controller/common/order/name = Myorder |
||
55 | * |
||
56 | * The value is the last part of your own class name and it's case sensitive, |
||
57 | * so take care that the configuration value is exactly named like the last |
||
58 | * part of the class name. |
||
59 | * |
||
60 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
61 | * characters are possible! You should always start the last part of the class |
||
62 | * name with an upper case character and continue only with lower case characters |
||
63 | * or numbers. Avoid chamel case names like "MyOrder"! |
||
64 | * |
||
65 | * @param string Last part of the class name |
||
66 | * @since 2014.07 |
||
67 | * @category Developer |
||
68 | */ |
||
69 | if( $name === null ) { |
||
70 | $name = $context->getConfig()->get( 'controller/common/order/name', 'Standard' ); |
||
71 | } |
||
72 | |||
73 | if( ctype_alnum( $name ) === false ) |
||
74 | { |
||
75 | $classname = is_string( $name ) ? '\Aimeos\Controller\Common\Order\\' . $name : '<not a string>'; |
||
76 | throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
||
77 | } |
||
78 | |||
79 | $iface = \Aimeos\Controller\Common\Order\Iface::class; |
||
80 | $classname = 'Aimeos\Controller\Common\Order\\' . $name; |
||
81 | |||
82 | if( isset( self::$objects[$classname] ) ) { |
||
83 | return self::$objects[$classname]; |
||
84 | } |
||
85 | |||
86 | if( class_exists( $classname ) === false ) { |
||
87 | throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
||
88 | } |
||
89 | |||
90 | $controller = new $classname( $context ); |
||
91 | |||
92 | if( !( $controller instanceof $iface ) ) { |
||
93 | throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) ); |
||
94 | } |
||
95 | |||
96 | return $controller; |
||
97 | } |
||
114 |