Conditions | 16 |
Paths | 131 |
Total Lines | 57 |
Code Lines | 30 |
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 = [] ) |
||
35 | { |
||
36 | if( is_resource( $file ) ) |
||
37 | { |
||
38 | if( ( $content = stream_get_contents( $file ) ) === false ) { |
||
39 | throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from stream' ) ); |
||
40 | } |
||
41 | } |
||
42 | elseif( $file instanceof \Psr\Http\Message\StreamInterface ) |
||
43 | { |
||
44 | $content = $file->getContents(); |
||
45 | } |
||
46 | elseif( is_string( $file ) ) |
||
47 | { |
||
48 | if( strpos( $file, "\0" ) === false && is_file( $file ) ) |
||
49 | { |
||
50 | if( ( $content = file_get_contents( $file ) ) === false ) { |
||
51 | throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $file ) ); |
||
52 | } |
||
53 | } |
||
54 | else |
||
55 | { |
||
56 | $content = $file; |
||
57 | } |
||
58 | } |
||
59 | else |
||
60 | { |
||
61 | throw new \Aimeos\MW\Media\Exception( 'Unsupported file parameter type' ); |
||
62 | } |
||
63 | |||
64 | |||
65 | $finfo = new \finfo( FILEINFO_MIME_TYPE ); |
||
66 | $mimetype = $finfo->buffer( $content ); |
||
67 | $mime = explode( '/', $mimetype ); |
||
68 | |||
69 | $type = $mime[0] === 'image' ? 'Image' : 'Application'; |
||
70 | $name = $type === 'Image' && extension_loaded( 'imagick' ) ? 'Imagick' : 'Standard'; |
||
71 | $name = ucfirst( $options[$mime[0]]['name'] ?? $name ); |
||
72 | |||
73 | if( in_array( $mimetype, ['image/svg', 'image/svg+xml'] ) |
||
74 | || in_array( $mimetype, ['application/gzip', 'application/x-gzip'] ) |
||
75 | && is_string( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), ['svg', 'svgz'] ) |
||
76 | ) { |
||
77 | $mimetype = 'image/svg+xml'; |
||
78 | $type = 'Image'; |
||
79 | $name = 'Svg'; |
||
80 | } |
||
81 | |||
82 | |||
83 | if( ctype_alnum( $name ) === false ) { |
||
84 | throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ) ); |
||
85 | } |
||
86 | |||
87 | $interface = \Aimeos\MW\Media\Iface::class; |
||
88 | $classname = '\Aimeos\MW\Media\\' . $type . '\\' . $name; |
||
89 | |||
90 | return \Aimeos\Utils::create( $classname, [$content, $mimetype, $options], $interface ); |
||
91 | } |
||
93 |