Conditions | 6 |
Paths | 8 |
Total Lines | 59 |
Code Lines | 14 |
Lines | 59 |
Ratio | 100 % |
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 createClient( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths, $path, $name = null ) |
||
35 | { |
||
36 | if( ctype_alnum( $path ) === false ) |
||
37 | { |
||
38 | $path = ( is_string( $path ) ? $path : '<not a string>' ); |
||
39 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid client "%1$s"', $path ), 400 ); |
||
40 | } |
||
41 | |||
42 | /** client/jsonapi/locale/name |
||
43 | * Class name of the used locale client implementation |
||
44 | * |
||
45 | * Each default JSON API client can be replace by an alternative imlementation. |
||
46 | * To use this implementation, you have to set the last part of the class |
||
47 | * name as configuration value so the client factory knows which class it |
||
48 | * has to instantiate. |
||
49 | * |
||
50 | * For example, if the name of the default class is |
||
51 | * |
||
52 | * \Aimeos\Client\JsonApi\Locale\Standard |
||
53 | * |
||
54 | * and you want to replace it with your own version named |
||
55 | * |
||
56 | * \Aimeos\Client\JsonApi\Locale\Mylocale |
||
57 | * |
||
58 | * then you have to set the this configuration option: |
||
59 | * |
||
60 | * client/jsonapi/locale/name = Mylocale |
||
61 | * |
||
62 | * The value is the last part of your own class name and it's case sensitive, |
||
63 | * so take care that the configuration value is exactly named like the last |
||
64 | * part of the class name. |
||
65 | * |
||
66 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
67 | * characters are possible! You should always start the last part of the class |
||
68 | * name with an upper case character and continue only with lower case characters |
||
69 | * or numbers. Avoid chamel case names like "MyLocale"! |
||
70 | * |
||
71 | * @param string Last part of the class name |
||
72 | * @since 2017.03 |
||
73 | * @category Developer |
||
74 | */ |
||
75 | if( $name === null ) { |
||
76 | $name = $context->getConfig()->get( 'client/jsonapi/locale/name', 'Standard' ); |
||
77 | } |
||
78 | |||
79 | if( ctype_alnum( $name ) === false ) |
||
80 | { |
||
81 | $classname = is_string( $name ) ? '\\Aimeos\\Client\\JsonApi\\Locale\\' . $name : '<not a string>'; |
||
82 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
||
83 | } |
||
84 | |||
85 | $view = $context->getView(); |
||
86 | $iface = '\\Aimeos\\Client\\JsonApi\\Iface'; |
||
87 | $classname = '\\Aimeos\\Client\\JsonApi\\Locale\\' . $name; |
||
88 | |||
89 | $client = self::createClientBase( $classname, $iface, $context, $view, $templatePaths, $path ); |
||
90 | |||
91 | return self::addClientDecorators( $client, $context, $view, $templatePaths, $path ); |
||
92 | } |
||
93 | |||
96 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.