| Total Complexity | 42 |
| Total Lines | 302 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HandlesAqlGrammar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HandlesAqlGrammar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait HandlesAqlGrammar |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Available predicate operators. |
||
| 14 | * |
||
| 15 | * @var array<string, int> |
||
| 16 | */ |
||
| 17 | protected array $comparisonOperators = [ |
||
| 18 | '==' => 1, |
||
| 19 | '!=' => 1, |
||
| 20 | '<' => 1, |
||
| 21 | '>' => 1, |
||
| 22 | '<=' => 1, |
||
| 23 | '>=' => 1, |
||
| 24 | 'IN' => 1, |
||
| 25 | 'NOT IN' => 1, |
||
| 26 | 'LIKE' => 1, |
||
| 27 | '~' => 1, |
||
| 28 | '!~' => 1, |
||
| 29 | 'ALL ==' => 1, |
||
| 30 | 'ALL !=' => 1, |
||
| 31 | 'ALL <' => 1, |
||
| 32 | 'ALL >' => 1, |
||
| 33 | 'ALL <=' => 1, |
||
| 34 | 'ALL >=' => 1, |
||
| 35 | 'ALL IN' => 1, |
||
| 36 | 'ANY ==' => 1, |
||
| 37 | 'ANY !=' => 1, |
||
| 38 | 'ANY <' => 1, |
||
| 39 | 'ANY >' => 1, |
||
| 40 | 'ANY <=' => 1, |
||
| 41 | 'ANY >=' => 1, |
||
| 42 | 'ANY IN' => 1, |
||
| 43 | 'NONE ==' => 1, |
||
| 44 | 'NONE !=' => 1, |
||
| 45 | 'NONE <' => 1, |
||
| 46 | 'NONE >' => 1, |
||
| 47 | 'NONE <=' => 1, |
||
| 48 | 'NONE >=' => 1, |
||
| 49 | 'NONE IN' => 1, |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array|int[] |
||
| 54 | */ |
||
| 55 | protected array $arithmeticOperators = [ |
||
| 56 | '+' => 1, |
||
| 57 | '-' => 1, |
||
| 58 | '*' => 1, |
||
| 59 | '/' => 1, |
||
| 60 | '%' => 1, |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array|int[] |
||
| 65 | */ |
||
| 66 | protected array $logicalOperators = [ |
||
| 67 | 'AND' => 1, |
||
| 68 | '&&' => 1, |
||
| 69 | 'OR' => 1, |
||
| 70 | '||' => 1, |
||
| 71 | 'NOT' => 1, |
||
| 72 | '!' => 1, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | protected string $rangeOperator = '..'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the format for database stored dates. |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function getDateFormat(): string |
||
| 83 | { |
||
| 84 | return 'Y-m-d\TH:i:s.vp'; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function isBind($value): bool |
||
| 88 | { |
||
| 89 | if (is_string($value) && preg_match('/^@?[0-9]{4}_' . json_encode($value) . '_[0-9_]+$/', $value)) { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | return false; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the appropriate query parameter place-holder for a value. |
||
| 98 | * |
||
| 99 | * @param mixed $value |
||
| 100 | */ |
||
| 101 | public function parameter($value): string |
||
| 102 | { |
||
| 103 | return $this->isExpression($value) ? $this->getValue($value) : (string) $value; |
||
| 104 | } |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Quote the given string literal. |
||
| 109 | * |
||
| 110 | * @param string|array $value |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function quoteString($value) |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Wrap a value in keyword identifiers. |
||
| 125 | * |
||
| 126 | * @param Array<mixed>|Expression|string $value |
||
| 127 | * @param bool $prefixAlias |
||
| 128 | * @return string|array |
||
| 129 | * |
||
| 130 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 131 | */ |
||
| 132 | public function wrap($value, $prefixAlias = false) |
||
| 133 | { |
||
| 134 | if ($this->isExpression($value)) { |
||
| 135 | return $this->getValue($value); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (is_array($value)) { |
||
| 139 | foreach($value as $key => $subvalue) { |
||
| 140 | $value[$key] = $this->wrap($subvalue, $prefixAlias); |
||
| 141 | } |
||
| 142 | return $value; |
||
| 143 | } |
||
| 144 | |||
| 145 | // If the value being wrapped has a column alias we will need to separate out |
||
| 146 | // the pieces so we can wrap each of the segments of the expression on its |
||
| 147 | // own, and then join these both back together using the "as" connector. |
||
| 148 | if (is_string($value) && stripos($value, ' as ') !== false) { |
||
| 149 | return $this->wrapAliasedValue($value, $prefixAlias); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this->wrapSegments(explode('.', $value)); |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Wrap a table in keyword identifiers. |
||
| 158 | * |
||
| 159 | * @param \Illuminate\Database\Query\Expression|string $table |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function wrapTable($table) |
||
| 163 | { |
||
| 164 | if (!$this->isExpression($table)) { |
||
| 165 | return $this->wrap($this->tablePrefix . $table, true); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $this->getValue($table); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Wrap a single string in keyword identifiers. |
||
| 173 | * |
||
| 174 | * @param string $value |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | protected function wrapValue($value) |
||
| 178 | { |
||
| 179 | $postfix = ''; |
||
| 180 | if ($value === 'groupsVariable') { |
||
| 181 | $postfix = '[*]'; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($value === '*') { |
||
| 185 | return $value; |
||
| 186 | } |
||
| 187 | |||
| 188 | return '`' . str_replace('`', '``', $value) . '`' . $postfix; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Wrap a subquery single string in braces. |
||
| 193 | */ |
||
| 194 | public function wrapSubquery(string $subquery): string |
||
| 197 | } |
||
| 198 | |||
| 199 | public function generateAqlObject(array $data): string |
||
| 200 | { |
||
| 201 | $data = Arr::undot($data); |
||
| 202 | |||
| 203 | return $this->generateAqlObjectString($data); |
||
| 204 | } |
||
| 205 | |||
| 206 | protected function generateAqlObjectString(array $data): string |
||
| 207 | { |
||
| 208 | foreach($data as $key => $value) { |
||
| 209 | $prefix = $key . ': '; |
||
| 210 | if (is_numeric($key)) { |
||
| 211 | $prefix = ''; |
||
| 212 | } |
||
| 213 | |||
| 214 | if (is_array($value)) { |
||
| 215 | $data[$key] = $prefix . $this->generateAqlObjectString($value); |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | |||
| 219 | if ($value instanceof Expression) { |
||
| 220 | $data[$key] = $prefix . $value->getValue($this); |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | $data[$key] = $prefix . $value; |
||
| 225 | } |
||
| 226 | |||
| 227 | $returnString = implode(', ', $data); |
||
| 228 | |||
| 229 | if (array_is_list($data)) { |
||
| 230 | return '[' . $returnString . ']'; |
||
| 231 | } |
||
| 232 | |||
| 233 | return '{' . $returnString . '}'; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Substitute the given bindings into the given raw AQL query. |
||
| 238 | * |
||
| 239 | * @param string $sql |
||
| 240 | * @param array $bindings |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function substituteBindingsIntoRawSql($sql, $bindings) |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Determine if the given string is a JSON selector. |
||
| 263 | * |
||
| 264 | * @param string $value |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function isJsonSelector($value) |
||
| 274 | } |
||
| 275 | |||
| 276 | public function convertJsonFields($data): mixed |
||
| 291 | } |
||
| 292 | |||
| 293 | public function convertJsonValuesToDotNotation(array $fields): array |
||
| 301 | } |
||
| 302 | |||
| 303 | public function convertJsonKeysToDotNotation(array $fields): array |
||
| 314 |