Conditions | 13 |
Paths | 60 |
Total Lines | 42 |
Code Lines | 25 |
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 |
||
87 | // a) trouble if the given string accidentally matches a file path, and |
||
88 | // b) security implications because of the above. |
||
89 | // use with caution and never with user input! |
||
90 | # if(\is_string($in) && \is_file($in) && \is_readable($in)){ |
||
91 | # return new Stream(\fopen($in, 'r')); |
||
92 | # } |
||
93 | |||
94 | if(is_scalar($in)){ |
||
95 | return create_stream((string)$in); |
||
96 | } |
||
97 | |||
98 | $type = gettype($in); |
||
99 | |||
100 | if($type === 'resource'){ |
||
101 | return new Stream($in); |
||
102 | } |
||
103 | elseif($type === 'object'){ |
||
104 | |||
105 | if($in instanceof StreamInterface){ |
||
106 | return $in; |
||
107 | } |
||
108 | elseif(method_exists($in, '__toString')){ |
||
109 | return create_stream((string)$in); |
||
110 | } |
||
111 | |||
112 | } |
||
113 | |||
114 | throw new InvalidArgumentException('Invalid resource type: '.$type); |
||
115 | } |
||
116 |