| Conditions | 18 |
| Paths | 97 |
| Total Lines | 72 |
| Code Lines | 34 |
| 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 |
||
| 54 | public function validateQueryStringArguments($queryString, string $type) |
||
| 55 | { |
||
| 56 | $filters = $this->getConfigItem($type); |
||
| 57 | $numericals = ['servers', 'zones']; |
||
| 58 | $strings = ['factions', 'brackets', 'dates']; |
||
| 59 | |||
| 60 | if (!empty($queryString)) { |
||
| 61 | $values = explode(',', $queryString); |
||
| 62 | |||
| 63 | // Run a check on the IDs provided to make sure they're valid and no naughty things are being passed |
||
| 64 | foreach ($values as $val) { |
||
| 65 | // If the query string should contain only numbers |
||
| 66 | if (in_array($type, $numericals)) { |
||
| 67 | if (!is_numeric($val)) { |
||
| 68 | throw new InvalidArgumentException("Non numerical ID detected. Only numerical IDs are accepted with this request."); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | if (in_array($type, $strings)) { |
||
| 72 | if (is_numeric($val)) { |
||
| 73 | throw new InvalidArgumentException("Numerical input detected. Only string inputs are accepted with this request."); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($type !== 'dates' && !in_array($val, $filters)) { |
||
| 78 | throw new InvalidArgumentException("Unrecognized {$type}. Please check the DATA you sent."); |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($type === 'dates') { |
||
| 82 | if (!$this->getDateValidationUtility()->validate($val, 'Y-m-d')) { |
||
| 83 | throw new InvalidArgumentException('Unrecognized date format. Dates must be in Y-m-d format.'); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | // Additional check for ordering of dates |
||
| 89 | if ($type === 'dates') { |
||
| 90 | if ($values[0] > $values[1]) { |
||
| 91 | throw new InvalidArgumentException('First date provided MUST come BEFORE second date.'); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // Allow brackets to have UNK as otherwise it's filtering out the queries |
||
| 96 | if ($type === 'brackets') { |
||
| 97 | $values[] = 'UNK'; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Format into strings comma separated for SQL |
||
| 101 | if (in_array($type, $strings)) { |
||
| 102 | $queryString = "'" . implode("','", $values) . "'"; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $queryString; |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($type === 'dates') { |
||
| 109 | return $queryString; |
||
| 110 | } |
||
| 111 | |||
| 112 | // Allow brackets to have UNK as otherwise it's filtering out the queries |
||
| 113 | if ($type === 'brackets') { |
||
| 114 | $values[] = 'UNK'; |
||
|
|
|||
| 115 | } |
||
| 116 | |||
| 117 | // If no query string was provided |
||
| 118 | $return = implode(',', $filters); |
||
| 119 | |||
| 120 | if (in_array($type, $strings)) { |
||
| 121 | $return = "'" . implode("','", $filters) . "'"; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $return; |
||
| 125 | } |
||
| 126 | |||
| 162 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.