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 |
||
33 | public static function create( \Aimeos\MShop\Context\Item\Iface $context, $name = null ) |
||
34 | { |
||
35 | /** controller/common/media/name |
||
36 | * Class name of the used media common controller implementation |
||
37 | * |
||
38 | * Each default common controller can be replace by an alternative imlementation. |
||
39 | * To use this implementation, you have to set the last part of the class |
||
40 | * name as configuration value so the controller factory knows which class it |
||
41 | * has to instantiate. |
||
42 | * |
||
43 | * For example, if the name of the default class is |
||
44 | * |
||
45 | * \Aimeos\Controller\Common\Media\Standard |
||
46 | * |
||
47 | * and you want to replace it with your own version named |
||
48 | * |
||
49 | * \Aimeos\Controller\Common\Media\Mymedia |
||
50 | * |
||
51 | * then you have to set the this configuration option: |
||
52 | * |
||
53 | * controller/common/media/name = Mymedia |
||
54 | * |
||
55 | * The value is the last part of your own class name and it's case sensitive, |
||
56 | * so take care that the configuration value is exactly named like the last |
||
57 | * part of the class name. |
||
58 | * |
||
59 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
60 | * characters are possible! You should always start the last part of the class |
||
61 | * name with an upper case character and continue only with lower case characters |
||
62 | * or numbers. Avoid chamel case names like "MyMedia"! |
||
63 | * |
||
64 | * @param string Last part of the class name |
||
65 | * @since 2016.01 |
||
66 | * @category Developer |
||
67 | */ |
||
68 | if( $name === null ) { |
||
69 | $name = $context->getConfig()->get( 'controller/common/media/name', 'Standard' ); |
||
70 | } |
||
71 | |||
72 | if( ctype_alnum( $name ) === false ) |
||
73 | { |
||
74 | $classname = is_string( $name ) ? '\Aimeos\Controller\Common\Media\\' . $name : '<not a string>'; |
||
75 | throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
||
76 | } |
||
77 | |||
78 | $iface = \Aimeos\Controller\Common\Media\Iface::class; |
||
79 | $classname = 'Aimeos\Controller\Common\Media\\' . $name; |
||
80 | |||
81 | if( isset( self::$objects[$classname] ) ) { |
||
82 | return self::$objects[$classname]; |
||
83 | } |
||
84 | |||
85 | if( class_exists( $classname ) === false ) { |
||
86 | throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
||
87 | } |
||
88 | |||
89 | $controller = new $classname( $context ); |
||
90 | |||
91 | if( !( $controller instanceof $iface ) ) { |
||
92 | throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) ); |
||
93 | } |
||
94 | |||
95 | return $controller; |
||
96 | } |
||
113 |