| Conditions | 11 |
| Paths | 5 |
| Total Lines | 45 |
| 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 |
||
| 153 | public function preview(): string |
||
| 154 | { |
||
| 155 | if (!$this->hasValues()) { |
||
| 156 | return $this->stmt->queryString; |
||
| 157 | } |
||
| 158 | |||
| 159 | $escape = function ($value) { |
||
| 160 | if (null === $value) { |
||
| 161 | return 'NULL'; |
||
| 162 | } |
||
| 163 | $value = $this->toScalar($value); |
||
| 164 | $type = gettype($value); |
||
| 165 | switch ($type) { |
||
| 166 | case 'boolean': |
||
| 167 | return (int) $value; |
||
| 168 | case 'double': |
||
| 169 | case 'integer': |
||
| 170 | return $value; |
||
| 171 | default: |
||
| 172 | return (string) "'" . addslashes($value) . "'"; |
||
| 173 | } |
||
| 174 | }; |
||
| 175 | |||
| 176 | $keywords = []; |
||
| 177 | $preview = $this->stmt->queryString; |
||
| 178 | |||
| 179 | # Case of question mark placeholders |
||
| 180 | if ($this->hasAnonymousPlaceholders()) { |
||
| 181 | foreach ($this->values as $value) { |
||
| 182 | $preview = preg_replace("/([\?])/", $escape($value), $preview, 1); |
||
| 183 | } |
||
| 184 | } # Case of named placeholders |
||
| 185 | else { |
||
| 186 | foreach ($this->values as $key => $value) { |
||
| 187 | if (!in_array($key, $keywords, true)) { |
||
| 188 | $keywords[] = $key; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | foreach ($keywords as $keyword) { |
||
| 192 | $pattern = "/(\:\b" . $keyword . "\b)/i"; |
||
| 193 | $preview = preg_replace($pattern, $escape($this->values[$keyword]), $preview); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | return $preview; |
||
| 197 | } |
||
| 198 | |||
| 215 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needlebut will only accept an array as$haystack.