Utils   F
last analyzed

Complexity

Total Complexity 101

Size/Duplication

Total Lines 604
Duplicated Lines 0 %

Test Coverage

Coverage 81.89%

Importance

Changes 0
Metric Value
wmc 101
eloc 205
c 0
b 0
f 0
dl 0
loc 604
rs 2
ccs 190
cts 232
cp 0.8189

27 Methods

Rating   Name   Duplication   Size   Complexity  
A keyMap() 0 18 4
A orList() 0 22 5
A suggestionList() 0 23 5
A ord() 0 13 5
A assign() 0 20 5
A chr() 0 7 2
A every() 0 9 3
A withErrorHandling() 0 12 1
B filter() 0 21 7
B printSafe() 0 35 11
A groupBy() 0 16 4
A map() 0 13 3
A isValidNameError() 0 20 5
A invariant() 0 10 3
A assertValidName() 0 5 2
A mapKeyValue() 0 14 3
A charCodeAt() 0 5 1
A getVariableType() 0 12 4
A each() 0 9 3
A find() 0 14 4
A quotedOrList() 0 10 1
B printSafeJson() 0 28 9
A undefined() 0 5 2
A printCharCode() 0 11 3
A isInvalid() 0 3 1
A keyValMap() 0 8 2
A some() 0 9 3

How to fix   Complexity   

Complex Class

Complex classes like Utils 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 Utils, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Utils;
6
7
use ErrorException;
8
use Exception;
9
use GraphQL\Error\Error;
10
use GraphQL\Error\InvariantViolation;
11
use GraphQL\Error\Warning;
12
use GraphQL\Language\AST\Node;
13
use GraphQL\Type\Definition\Type;
14
use GraphQL\Type\Definition\WrappingType;
15
use InvalidArgumentException;
16
use LogicException;
17
use stdClass;
18
use Traversable;
19
use function array_keys;
20
use function array_map;
21
use function array_reduce;
22
use function array_shift;
23
use function array_slice;
24
use function array_values;
25
use function asort;
26
use function count;
27
use function dechex;
28
use function func_get_args;
29
use function func_num_args;
30
use function get_class;
31
use function gettype;
32
use function is_array;
33
use function is_int;
34
use function is_object;
35
use function is_scalar;
36
use function is_string;
37
use function json_encode;
38
use function levenshtein;
39
use function max;
40
use function mb_convert_encoding;
41
use function mb_strlen;
42
use function mb_substr;
43
use function method_exists;
44
use function ord;
45
use function pack;
46
use function preg_match;
47
use function property_exists;
48
use function range;
49
use function restore_error_handler;
50
use function set_error_handler;
51
use function sprintf;
52
use function strtolower;
53
use function unpack;
54
55
class Utils
56
{
57 384
    public static function undefined()
58
    {
59 384
        static $undefined;
60
61 384
        return $undefined ?: $undefined = new stdClass();
62
    }
63
64
    /**
65
     * Check if the value is invalid
66
     *
67
     * @param mixed $value
68
     *
69
     * @return bool
70
     */
71 75
    public static function isInvalid($value)
72
    {
73 75
        return self::undefined() === $value;
74
    }
75
76
    /**
77
     * @param object   $obj
78
     * @param mixed[]  $vars
79
     * @param string[] $requiredKeys
80
     *
81
     * @return object
82
     */
83 1012
    public static function assign($obj, array $vars, array $requiredKeys = [])
84
    {
85 1012
        foreach ($requiredKeys as $key) {
86 1
            if (! isset($vars[$key])) {
87 1
                throw new InvalidArgumentException(sprintf('Key %s is expected to be set and not to be null', $key));
88
            }
89
        }
90
91 1011
        foreach ($vars as $key => $value) {
92 1011
            if (! property_exists($obj, $key)) {
93
                $cls = get_class($obj);
94
                Warning::warn(
95
                    sprintf("Trying to set non-existing property '%s' on class '%s'", $key, $cls),
96
                    Warning::WARNING_ASSIGN
97
                );
98
            }
99 1011
            $obj->{$key} = $value;
100
        }
101
102 1011
        return $obj;
103
    }
104
105
    /**
106
     * @param mixed|Traversable $traversable
107
     *
108
     * @return mixed|null
109
     */
110 535
    public static function find($traversable, callable $predicate)
111
    {
112 535
        self::invariant(
113 535
            is_array($traversable) || $traversable instanceof Traversable,
114 535
            __METHOD__ . ' expects array or Traversable'
115
        );
116
117 535
        foreach ($traversable as $key => $value) {
118 238
            if ($predicate($value, $key)) {
119 238
                return $value;
120
            }
121
        }
122
123 371
        return null;
124
    }
125
126
    /**
127
     * @param mixed|Traversable $traversable
128
     *
129
     * @return mixed[]
130
     *
131
     * @throws Exception
132
     */
133 284
    public static function filter($traversable, callable $predicate)
134
    {
135 284
        self::invariant(
136 284
            is_array($traversable) || $traversable instanceof Traversable,
137 284
            __METHOD__ . ' expects array or Traversable'
138
        );
139
140 284
        $result = [];
141 284
        $assoc  = false;
142 284
        foreach ($traversable as $key => $value) {
143 252
            if (! $assoc && ! is_int($key)) {
144
                $assoc = true;
145
            }
146 252
            if (! $predicate($value, $key)) {
147 95
                continue;
148
            }
149
150 250
            $result[$key] = $value;
151
        }
152
153 284
        return $assoc ? $result : array_values($result);
154
    }
155
156
    /**
157
     * @param mixed|Traversable $traversable
158
     *
159
     * @return mixed[]
160
     *
161
     * @throws Exception
162
     */
163 399
    public static function map($traversable, callable $fn)
164
    {
165 399
        self::invariant(
166 399
            is_array($traversable) || $traversable instanceof Traversable,
167 399
            __METHOD__ . ' expects array or Traversable'
168
        );
169
170 399
        $map = [];
171 399
        foreach ($traversable as $key => $value) {
172 367
            $map[$key] = $fn($value, $key);
173
        }
174
175 397
        return $map;
176
    }
177
178
    /**
179
     * @param mixed|Traversable $traversable
180
     *
181
     * @return mixed[]
182
     *
183
     * @throws Exception
184
     */
185
    public static function mapKeyValue($traversable, callable $fn)
186
    {
187
        self::invariant(
188
            is_array($traversable) || $traversable instanceof Traversable,
189
            __METHOD__ . ' expects array or Traversable'
190
        );
191
192
        $map = [];
193
        foreach ($traversable as $key => $value) {
194
            [$newKey, $newValue] = $fn($value, $key);
195
            $map[$newKey]        = $newValue;
196
        }
197
198
        return $map;
199
    }
200
201
    /**
202
     * @param mixed|Traversable $traversable
203
     *
204
     * @return mixed[]
205
     *
206
     * @throws Exception
207
     */
208 217
    public static function keyMap($traversable, callable $keyFn)
209
    {
210 217
        self::invariant(
211 217
            is_array($traversable) || $traversable instanceof Traversable,
212 217
            __METHOD__ . ' expects array or Traversable'
213
        );
214
215 217
        $map = [];
216 217
        foreach ($traversable as $key => $value) {
217 216
            $newKey = $keyFn($value, $key);
218 216
            if (! is_scalar($newKey)) {
219
                continue;
220
            }
221
222 216
            $map[$newKey] = $value;
223
        }
224
225 217
        return $map;
226
    }
227
228
    public static function each($traversable, callable $fn)
229
    {
230
        self::invariant(
231
            is_array($traversable) || $traversable instanceof Traversable,
232
            __METHOD__ . ' expects array or Traversable'
233
        );
234
235
        foreach ($traversable as $key => $item) {
236
            $fn($item, $key);
237
        }
238
    }
239
240
    /**
241
     * Splits original traversable to several arrays with keys equal to $keyFn return
242
     *
243
     * E.g. Utils::groupBy([1, 2, 3, 4, 5], function($value) {return $value % 3}) will output:
244
     * [
245
     *    1 => [1, 4],
246
     *    2 => [2, 5],
247
     *    0 => [3],
248
     * ]
249
     *
250
     * $keyFn is also allowed to return array of keys. Then value will be added to all arrays with given keys
251
     *
252
     * @param mixed[]|Traversable $traversable
253
     *
254
     * @return mixed[]
255
     */
256 146
    public static function groupBy($traversable, callable $keyFn)
257
    {
258 146
        self::invariant(
259 146
            is_array($traversable) || $traversable instanceof Traversable,
260 146
            __METHOD__ . ' expects array or Traversable'
261
        );
262
263 146
        $grouped = [];
264 146
        foreach ($traversable as $key => $value) {
265 28
            $newKeys = (array) $keyFn($value, $key);
266 28
            foreach ($newKeys as $newKey) {
267 28
                $grouped[$newKey][] = $value;
268
            }
269
        }
270
271 146
        return $grouped;
272
    }
273
274
    /**
275
     * @param mixed[]|Traversable $traversable
276
     *
277
     * @return mixed[][]
278
     */
279 181
    public static function keyValMap($traversable, callable $keyFn, callable $valFn)
280
    {
281 181
        $map = [];
282 181
        foreach ($traversable as $item) {
283 164
            $map[$keyFn($item)] = $valFn($item);
284
        }
285
286 180
        return $map;
287
    }
288
289
    /**
290
     * @param mixed[] $traversable
291
     *
292
     * @return bool
293
     */
294 81
    public static function every($traversable, callable $predicate)
295
    {
296 81
        foreach ($traversable as $key => $value) {
297 81
            if (! $predicate($value, $key)) {
298 81
                return false;
299
            }
300
        }
301
302 79
        return true;
303
    }
304
305
    /**
306
     * @param mixed[] $traversable
307
     *
308
     * @return bool
309
     */
310 7
    public static function some($traversable, callable $predicate)
311
    {
312 7
        foreach ($traversable as $key => $value) {
313 7
            if ($predicate($value, $key)) {
314 7
                return true;
315
            }
316
        }
317
318 2
        return false;
319
    }
320
321
    /**
322
     * @param bool   $test
323
     * @param string $message
324
     */
325 1272
    public static function invariant($test, $message = '')
326
    {
327 1272
        if (! $test) {
328 34
            if (func_num_args() > 2) {
329 7
                $args = func_get_args();
330 7
                array_shift($args);
331 7
                $message = sprintf(...$args);
332
            }
333
            // TODO switch to Error here
334 34
            throw new InvariantViolation($message);
335
        }
336 1268
    }
337
338
    /**
339
     * @param Type|mixed $var
340
     *
341
     * @return string
342
     */
343 1142
    public static function getVariableType($var)
344
    {
345 1142
        if ($var instanceof Type) {
346
            // FIXME: Replace with schema printer call
347
            if ($var instanceof WrappingType) {
348
                $var = $var->getWrappedType(true);
349
            }
350
351
            return $var->name;
352
        }
353
354 1142
        return is_object($var) ? get_class($var) : gettype($var);
355
    }
356
357
    /**
358
     * @param mixed $var
359
     *
360
     * @return string
361
     */
362 39
    public static function printSafeJson($var)
363
    {
364 39
        if ($var instanceof stdClass) {
365
            $var = (array) $var;
366
        }
367 39
        if (is_array($var)) {
368 16
            return json_encode($var);
369
        }
370 24
        if ($var === '') {
371
            return '(empty string)';
372
        }
373 24
        if ($var === null) {
374
            return 'null';
375
        }
376 24
        if ($var === false) {
377 1
            return 'false';
378
        }
379 24
        if ($var === true) {
380 1
            return 'true';
381
        }
382 23
        if (is_string($var)) {
383 16
            return sprintf('"%s"', $var);
384
        }
385 8
        if (is_scalar($var)) {
386 8
            return (string) $var;
387
        }
388
389
        return gettype($var);
390
    }
391
392
    /**
393
     * @param Type|mixed $var
394
     *
395
     * @return string
396
     */
397 278
    public static function printSafe($var)
398
    {
399 278
        if ($var instanceof Type) {
400 63
            return $var->toString();
401
        }
402 259
        if (is_object($var)) {
403 178
            if (method_exists($var, '__toString')) {
404
                return (string) $var;
405
            }
406
407 178
            return 'instance of ' . get_class($var);
408
        }
409 93
        if (is_array($var)) {
410 19
            return json_encode($var);
411
        }
412 84
        if ($var === '') {
413 12
            return '(empty string)';
414
        }
415 79
        if ($var === null) {
416 23
            return 'null';
417
        }
418 64
        if ($var === false) {
419 7
            return 'false';
420
        }
421 63
        if ($var === true) {
422 8
            return 'true';
423
        }
424 61
        if (is_string($var)) {
425 33
            return $var;
426
        }
427 31
        if (is_scalar($var)) {
428 31
            return (string) $var;
429
        }
430
431
        return gettype($var);
432
    }
433
434
    /**
435
     * UTF-8 compatible chr()
436
     *
437
     * @param string $ord
438
     * @param string $encoding
439
     *
440
     * @return string
441
     */
442 27
    public static function chr($ord, $encoding = 'UTF-8')
443
    {
444 27
        if ($encoding === 'UCS-4BE') {
445 27
            return pack('N', $ord);
446
        }
447
448 27
        return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
449
    }
450
451
    /**
452
     * UTF-8 compatible ord()
453
     *
454
     * @param string $char
455
     * @param string $encoding
456
     *
457
     * @return mixed
458
     */
459 5
    public static function ord($char, $encoding = 'UTF-8')
460
    {
461 5
        if (! $char && $char !== '0') {
462
            return 0;
463
        }
464 5
        if (! isset($char[1])) {
465
            return ord($char);
466
        }
467 5
        if ($encoding !== 'UCS-4BE') {
468 5
            $char = mb_convert_encoding($char, 'UCS-4BE', $encoding);
469
        }
470
471 5
        return unpack('N', $char)[1];
472
    }
473
474
    /**
475
     * Returns UTF-8 char code at given $positing of the $string
476
     *
477
     * @param string $string
478
     * @param int    $position
479
     *
480
     * @return mixed
481
     */
482
    public static function charCodeAt($string, $position)
483
    {
484
        $char = mb_substr($string, $position, 1, 'UTF-8');
485
486
        return self::ord($char);
487
    }
488
489
    /**
490
     * @param int|null $code
491
     *
492
     * @return string
493
     */
494 22
    public static function printCharCode($code)
495
    {
496 22
        if ($code === null) {
497 2
            return '<EOF>';
498
        }
499
500 20
        return $code < 0x007F
501
            // Trust JSON for ASCII.
502 18
            ? json_encode(self::chr($code))
503
            // Otherwise print the escaped form.
504 20
            : '"\\u' . dechex($code) . '"';
505
    }
506
507
    /**
508
     * Upholds the spec rules about naming.
509
     *
510
     * @param string $name
511
     *
512
     * @throws Error
513
     */
514 26
    public static function assertValidName($name)
515
    {
516 26
        $error = self::isValidNameError($name);
517 25
        if ($error) {
518 2
            throw $error;
519
        }
520 23
    }
521
522
    /**
523
     * Returns an Error if a name is invalid.
524
     *
525
     * @param string    $name
526
     * @param Node|null $node
527
     *
528
     * @return Error|null
529
     */
530 104
    public static function isValidNameError($name, $node = null)
531
    {
532 104
        self::invariant(is_string($name), 'Expected string');
533
534 103
        if (isset($name[1]) && $name[0] === '_' && $name[1] === '_') {
535 90
            return new Error(
536 90
                sprintf('Name "%s" must not begin with "__", which is reserved by ', $name) .
537 90
                'GraphQL introspection.',
538 90
                $node
539
            );
540
        }
541
542 102
        if (! preg_match('/^[_a-zA-Z][_a-zA-Z0-9]*$/', $name)) {
543 6
            return new Error(
544 6
                sprintf('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%s" does not.', $name),
545 6
                $node
546
            );
547
        }
548
549 101
        return null;
550
    }
551
552
    /**
553
     * Wraps original callable with PHP error handling (using set_error_handler).
554
     * Resulting callable will collect all PHP errors that occur during the call in $errors array.
555
     *
556
     * @param ErrorException[] $errors
557
     *
558
     * @return callable
559
     */
560
    public static function withErrorHandling(callable $fn, array &$errors)
561
    {
562
        return static function () use ($fn, &$errors) {
563
            // Catch custom errors (to report them in query results)
564
            set_error_handler(static function ($severity, $message, $file, $line) use (&$errors) {
565
                $errors[] = new ErrorException($message, 0, $severity, $file, $line);
566
            });
567
568
            try {
569
                return $fn();
570
            } finally {
571
                restore_error_handler();
572
            }
573
        };
574
    }
575
576
    /**
577
     * @param string[] $items
578
     *
579
     * @return string
580
     */
581 22
    public static function quotedOrList(array $items)
582
    {
583 22
        $items = array_map(
584
            static function ($item) {
585 21
                return sprintf('"%s"', $item);
586 22
            },
587 22
            $items
588
        );
589
590 22
        return self::orList($items);
591
    }
592
593
    /**
594
     * @param string[] $items
595
     *
596
     * @return string
597
     */
598 30
    public static function orList(array $items)
599
    {
600 30
        if (count($items) === 0) {
601 1
            throw new LogicException('items must not need to be empty.');
602
        }
603 29
        $selected       = array_slice($items, 0, 5);
604 29
        $selectedLength = count($selected);
605 29
        $firstSelected  = $selected[0];
606
607 29
        if ($selectedLength === 1) {
608 17
            return $firstSelected;
609
        }
610
611 12
        return array_reduce(
612 12
            range(1, $selectedLength - 1),
613
            static function ($list, $index) use ($selected, $selectedLength) {
614
                return $list .
615 12
                    ($selectedLength > 2 ? ', ' : ' ') .
616 12
                    ($index === $selectedLength - 1 ? 'or ' : '') .
617 12
                    $selected[$index];
618 12
            },
619 12
            $firstSelected
620
        );
621
    }
622
623
    /**
624
     * Given an invalid input string and a list of valid options, returns a filtered
625
     * list of valid options sorted based on their similarity with the input.
626
     *
627
     * Includes a custom alteration from Damerau-Levenshtein to treat case changes
628
     * as a single edit which helps identify mis-cased values with an edit distance
629
     * of 1
630
     *
631
     * @param string   $input
632
     * @param string[] $options
633
     *
634
     * @return string[]
635
     */
636 44
    public static function suggestionList($input, array $options)
637
    {
638 44
        $optionsByDistance = [];
639 44
        $inputThreshold    = mb_strlen($input) / 2;
640 44
        foreach ($options as $option) {
641 43
            if ($input === $option) {
642 1
                $distance = 0;
643
            } else {
644 43
                $distance = (strtolower($input) === strtolower($option)
645 4
                    ? 1
646 43
                    : levenshtein($input, $option));
647
            }
648 43
            $threshold = max($inputThreshold, mb_strlen($option) / 2, 1);
649 43
            if ($distance > $threshold) {
650 39
                continue;
651
            }
652
653 19
            $optionsByDistance[$option] = $distance;
654
        }
655
656 44
        asort($optionsByDistance);
657
658 44
        return array_keys($optionsByDistance);
659
    }
660
}
661