Conditions | 11 |
Paths | 22 |
Total Lines | 42 |
Code Lines | 22 |
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 get( $file, array $options = array() ) |
||
35 | { |
||
36 | $content = $file; |
||
37 | |||
38 | if( @is_resource( $file ) && ( $content = stream_get_contents( $file ) ) === false ) { |
||
39 | throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from stream' ) ); |
||
40 | } |
||
41 | |||
42 | if( @is_file( $file ) && ( $content = @file_get_contents( $file ) ) === false ) { |
||
43 | throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $file ) ); |
||
44 | } |
||
45 | |||
46 | |||
47 | $finfo = new \finfo( FILEINFO_MIME_TYPE ); |
||
48 | $mimetype = $finfo->buffer( $content ); |
||
49 | $mime = explode( '/', $mimetype ); |
||
50 | |||
51 | $type = ( $mime[0] === 'image' ? 'Image' : 'Application' ); |
||
52 | $name = ( isset( $options[ $mime[0] ]['name'] ) ? ucfirst( $options[ $mime[0] ]['name'] ) : 'Standard' ); |
||
53 | |||
54 | |||
55 | if( ctype_alnum( $name ) === false ) |
||
56 | { |
||
57 | $classname = is_string( $name ) ? '\\Aimeos\\MW\\Media\\' . $name : '<not a string>'; |
||
58 | throw new \Aimeos\MW\Container\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
||
59 | } |
||
60 | |||
61 | $iface = '\\Aimeos\\MW\\Media\\Iface'; |
||
62 | $classname = '\\Aimeos\\MW\\Media\\' . $type . '\\' . $name; |
||
63 | |||
64 | if( class_exists( $classname ) === false ) { |
||
65 | throw new \Aimeos\MW\Media\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
||
66 | } |
||
67 | |||
68 | $object = new $classname( $content, $mimetype, $options ); |
||
69 | |||
70 | if( !( $object instanceof $iface ) ) { |
||
71 | throw new \Aimeos\MW\Media\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) ); |
||
72 | } |
||
73 | |||
74 | return $object; |
||
75 | } |
||
76 | } |
||
77 |