Passed
Push — master ( 986482...835c8a )
by Vladimir
10:20
created

Utils   F

Complexity

Total Complexity 101

Size/Duplication

Total Lines 604
Duplicated Lines 0 %

Test Coverage

Coverage 81.47%

Importance

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

27 Methods

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

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