Conditions | 15 |
Paths | 650 |
Total Lines | 61 |
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 |
||
89 | public function addElement(array $e):MultipartStream{ |
||
90 | |||
91 | if($this->built){ |
||
92 | throw new RuntimeException('Stream already built'); |
||
93 | } |
||
94 | |||
95 | $e = array_merge(['filename' => null, 'headers' => []], $e); |
||
96 | |||
97 | foreach(['contents', 'name'] as $key){ |
||
98 | if(!isset($e[$key])){ |
||
99 | throw new InvalidArgumentException('A "'.$key.'" element is required'); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | $e['contents'] = Psr17\create_stream_from_input($e['contents']); |
||
104 | |||
105 | if(empty($e['filename'])){ |
||
106 | $uri = $e['contents']->getMetadata('uri'); |
||
107 | |||
108 | if(substr($uri, 0, 6) !== 'php://'){ |
||
109 | $e['filename'] = $uri; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $hasFilename = $e['filename'] === '0' || $e['filename']; |
||
114 | |||
115 | // Set a default content-disposition header if none was provided |
||
116 | if(!$this->hasHeader($e['headers'], 'content-disposition')){ |
||
117 | $e['headers']['Content-Disposition'] = 'form-data; name="'.$e['name'].'"'.($hasFilename ? '; filename="'.basename($e['filename']).'"' : ''); |
||
118 | } |
||
119 | |||
120 | // Set a default content-length header if none was provided |
||
121 | if(!$this->hasHeader($e['headers'], 'content-length')){ |
||
122 | $length = $e['contents']->getSize(); |
||
123 | |||
124 | if($length){ |
||
125 | $e['headers']['Content-Length'] = $length; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // Set a default Content-Type if none was supplied |
||
130 | if(!$this->hasHeader($e['headers'], 'content-type') && $hasFilename){ |
||
131 | $type = MIMETYPES[pathinfo($e['filename'], PATHINFO_EXTENSION)] ?? null; |
||
132 | |||
133 | if($type){ |
||
134 | $e['headers']['Content-Type'] = $type; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | $this->stream->write('--'.$this->boundary."\r\n"); |
||
140 | |||
141 | foreach($e['headers'] as $key => $value){ |
||
142 | $this->stream->write($key.': '.$value."\r\n"); |
||
143 | } |
||
144 | |||
145 | $this->stream->write("\r\n".$e['contents']->getContents()."\r\n"); |
||
146 | |||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
206 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.