Failed Conditions
Pull Request — master (#328)
by Šimon
04:02
created

Utils::chr()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Utils;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Error\InvariantViolation;
9
use GraphQL\Error\Warning;
10
use GraphQL\Language\AST\Node;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Type\Definition\WrappingType;
13
use InvalidArgumentException;
14
use Traversable;
15
use function array_keys;
16
use function array_map;
17
use function array_reduce;
18
use function array_shift;
19
use function array_slice;
20
use function array_values;
21
use function asort;
22
use function chr;
23
use function count;
24
use function dechex;
25
use function func_get_args;
26
use function func_num_args;
27
use function get_class;
28
use function gettype;
29
use function is_array;
30
use function is_int;
31
use function is_object;
32
use function is_scalar;
33
use function is_string;
34
use function json_encode;
35
use function levenshtein;
36
use function max;
37
use function mb_convert_encoding;
38
use function mb_strlen;
39
use function mb_substr;
40
use function method_exists;
41
use function ord;
42
use function pack;
43
use function preg_match;
44
use function property_exists;
45
use function range;
46
use function restore_error_handler;
47
use function set_error_handler;
48
use function sprintf;
49
use function strtolower;
50
use function unpack;
51
52
class Utils
53
{
54 115
    public static function undefined()
55
    {
56 115
        static $undefined;
57
58 115
        return $undefined ?: $undefined = new \stdClass();
59
    }
60
61
    /**
62
     * Check if the value is invalid
63
     *
64
     * @param mixed $value
65
     * @return bool
66
     */
67 56
    public static function isInvalid($value)
68
    {
69 56
        return self::undefined() === $value;
70
    }
71
72
    /**
73
     * @param object   $obj
74
     * @param mixed[]  $vars
75
     * @param string[] $requiredKeys
76
     *
77
     * @return object
78
     */
79 862
    public static function assign($obj, array $vars, array $requiredKeys = [])
80
    {
81 862
        foreach ($requiredKeys as $key) {
82 1
            if (! isset($vars[$key])) {
83 1
                throw new InvalidArgumentException(sprintf('Key %s is expected to be set and not to be null', $key));
84
            }
85
        }
86
87 861
        foreach ($vars as $key => $value) {
88 861
            if (! property_exists($obj, $key)) {
89
                $cls = get_class($obj);
90
                Warning::warn(
91
                    sprintf("Trying to set non-existing property '%s' on class '%s'", $key, $cls),
92
                    Warning::WARNING_ASSIGN
93
                );
94
            }
95 861
            $obj->{$key} = $value;
96
        }
97
98 861
        return $obj;
99
    }
100
101
    /**
102
     * @param mixed|Traversable $traversable
103
     * @return mixed|null
104
     */
105 430
    public static function find($traversable, callable $predicate)
106
    {
107 430
        self::invariant(
108 430
            is_array($traversable) || $traversable instanceof \Traversable,
109 430
            __METHOD__ . ' expects array or Traversable'
110
        );
111
112 430
        foreach ($traversable as $key => $value) {
113 218
            if ($predicate($value, $key)) {
114 218
                return $value;
115
            }
116
        }
117
118 274
        return null;
119
    }
120
121
    /**
122
     * @param mixed|Traversable $traversable
123
     * @return mixed[]
124
     * @throws \Exception
125
     */
126 124
    public static function filter($traversable, callable $predicate)
127
    {
128 124
        self::invariant(
129 124
            is_array($traversable) || $traversable instanceof \Traversable,
130 124
            __METHOD__ . ' expects array or Traversable'
131
        );
132
133 124
        $result = [];
134 124
        $assoc  = false;
135 124
        foreach ($traversable as $key => $value) {
136 124
            if (! $assoc && ! is_int($key)) {
137
                $assoc = true;
138
            }
139 124
            if (! $predicate($value, $key)) {
140 24
                continue;
141
            }
142
143 123
            $result[$key] = $value;
144
        }
145
146 124
        return $assoc ? $result : array_values($result);
147
    }
148
149
    /**
150
     * @param mixed|\Traversable $traversable
151
     * @return int[][]
152
     * @throws \Exception
153
     */
154 348
    public static function map($traversable, callable $fn)
155
    {
156 348
        self::invariant(
157 348
            is_array($traversable) || $traversable instanceof \Traversable,
158 348
            __METHOD__ . ' expects array or Traversable'
159
        );
160
161 348
        $map = [];
162 348
        foreach ($traversable as $key => $value) {
163 318
            $map[$key] = $fn($value, $key);
164
        }
165
166 346
        return $map;
167
    }
168
169
    /**
170
     * @param mixed|Traversable $traversable
171
     * @return mixed[]
172
     * @throws \Exception
173
     */
174
    public static function mapKeyValue($traversable, callable $fn)
175
    {
176
        self::invariant(
177
            is_array($traversable) || $traversable instanceof \Traversable,
178
            __METHOD__ . ' expects array or Traversable'
179
        );
180
181
        $map = [];
182
        foreach ($traversable as $key => $value) {
183
            list($newKey, $newValue) = $fn($value, $key);
184
            $map[$newKey] = $newValue;
185
        }
186
187
        return $map;
188
    }
189
190
    /**
191
     * @param mixed|Traversable $traversable
192
     * @return mixed[]
193
     * @throws \Exception
194
     */
195 108
    public static function keyMap($traversable, callable $keyFn)
196
    {
197 108
        self::invariant(
198 108
            is_array($traversable) || $traversable instanceof \Traversable,
199 108
            __METHOD__ . ' expects array or Traversable'
200
        );
201
202 108
        $map = [];
203 108
        foreach ($traversable as $key => $value) {
204 93
            $newKey = $keyFn($value, $key);
205 93
            if (! is_scalar($newKey)) {
206
                continue;
207
            }
208
209 93
            $map[$newKey] = $value;
210
        }
211
212 108
        return $map;
213
    }
214
215
    public static function each($traversable, callable $fn)
216
    {
217
        self::invariant(
218
            is_array($traversable) || $traversable instanceof \Traversable,
219
            __METHOD__ . ' expects array or Traversable'
220
        );
221
222
        foreach ($traversable as $key => $item) {
223
            $fn($item, $key);
224
        }
225
    }
226
227
    /**
228
     * Splits original traversable to several arrays with keys equal to $keyFn return
229
     *
230
     * E.g. Utils::groupBy([1, 2, 3, 4, 5], function($value) {return $value % 3}) will output:
231
     * [
232
     *    1 => [1, 4],
233
     *    2 => [2, 5],
234
     *    0 => [3],
235
     * ]
236
     *
237
     * $keyFn is also allowed to return array of keys. Then value will be added to all arrays with given keys
238
     *
239
     * @param mixed[]|Traversable $traversable
240
     * @return mixed[]
241
     */
242
    public static function groupBy($traversable, callable $keyFn)
243
    {
244
        self::invariant(
245
            is_array($traversable) || $traversable instanceof \Traversable,
246
            __METHOD__ . ' expects array or Traversable'
247
        );
248
249
        $grouped = [];
250
        foreach ($traversable as $key => $value) {
251
            $newKeys = (array) $keyFn($value, $key);
252
            foreach ($newKeys as $key) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
253
                $grouped[$key][] = $value;
254
            }
255
        }
256
257
        return $grouped;
258
    }
259
260
    /**
261
     * @param mixed[]|Traversable $traversable
262
     * @return mixed[][]
263
     */
264 77
    public static function keyValMap($traversable, callable $keyFn, callable $valFn)
265
    {
266 77
        $map = [];
267 77
        foreach ($traversable as $item) {
268 76
            $map[$keyFn($item)] = $valFn($item);
269
        }
270
271 75
        return $map;
272
    }
273
274
    /**
275
     * @param mixed[] $traversable
276
     * @return bool
277
     */
278 22
    public static function every($traversable, callable $predicate)
279
    {
280 22
        foreach ($traversable as $key => $value) {
281 22
            if (! $predicate($value, $key)) {
282 22
                return false;
283
            }
284
        }
285
286 20
        return true;
287
    }
288
289
    /**
290
     * @param bool   $test
291
     * @param string $message
292
     */
293 1129
    public static function invariant($test, $message = '')
294
    {
295 1129
        if (! $test) {
296 31
            if (func_num_args() > 2) {
297 3
                $args = func_get_args();
298 3
                array_shift($args);
299 3
                $message = sprintf(...$args);
300
            }
301
            // TODO switch to Error here
302 31
            throw new InvariantViolation($message);
303
        }
304 1125
    }
305
306
    /**
307
     * @param Type|mixed $var
308
     * @return string
309
     */
310 1006
    public static function getVariableType($var)
311
    {
312 1006
        if ($var instanceof Type) {
313
            // FIXME: Replace with schema printer call
314
            if ($var instanceof WrappingType) {
315
                $var = $var->getWrappedType(true);
316
            }
317
318
            return $var->name;
319
        }
320
321 1006
        return is_object($var) ? get_class($var) : gettype($var);
322
    }
323
324
    /**
325
     * @param mixed $var
326
     * @return string
327
     */
328 24
    public static function printSafeJson($var)
329
    {
330 24
        if ($var instanceof \stdClass) {
331
            $var = (array) $var;
332
        }
333 24
        if (is_array($var)) {
334 7
            return json_encode($var);
335
        }
336 18
        if ($var === '') {
337
            return '(empty string)';
338
        }
339 18
        if ($var === null) {
340 3
            return 'null';
341
        }
342 15
        if ($var === false) {
343
            return 'false';
344
        }
345 15
        if ($var === true) {
346 1
            return 'true';
347
        }
348 14
        if (is_string($var)) {
349 7
            return sprintf('"%s"', $var);
350
        }
351 7
        if (is_scalar($var)) {
352 7
            return (string) $var;
353
        }
354
355
        return gettype($var);
356
    }
357
358
    /**
359
     * @param Type|mixed $var
360
     * @return string
361
     */
362 804
    public static function printSafe($var)
363
    {
364 804
        if ($var instanceof Type) {
365 724
            return $var->toString();
366
        }
367 171
        if (is_object($var)) {
368 111
            if (method_exists($var, '__toString')) {
369
                return (string) $var;
370
            }
371
372 111
            return 'instance of ' . get_class($var);
373
        }
374 73
        if (is_array($var)) {
375 17
            return json_encode($var);
376
        }
377 67
        if ($var === '') {
378 9
            return '(empty string)';
379
        }
380 66
        if ($var === null) {
381 27
            return 'null';
382
        }
383 46
        if ($var === false) {
384 6
            return 'false';
385
        }
386 46
        if ($var === true) {
387 7
            return 'true';
388
        }
389 45
        if (is_string($var)) {
390 24
            return $var;
391
        }
392 23
        if (is_scalar($var)) {
393 23
            return (string) $var;
394
        }
395
396
        return gettype($var);
397
    }
398
399
    /**
400
     * UTF-8 compatible chr()
401
     *
402
     * @param string $ord
403
     * @param string $encoding
404
     * @return string
405
     */
406 23
    public static function chr($ord, $encoding = 'UTF-8')
407
    {
408 23
        if ($ord <= 255) {
409 20
            return chr($ord);
0 ignored issues
show
Bug introduced by
$ord of type string is incompatible with the type integer expected by parameter $ascii of chr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

409
            return chr(/** @scrutinizer ignore-type */ $ord);
Loading history...
410
        }
411 3
        if ($encoding === 'UCS-4BE') {
412 3
            return pack('N', $ord);
413
        }
414
415 3
        return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
416
    }
417
418
    /**
419
     * UTF-8 compatible ord()
420
     *
421
     * @param string $char
422
     * @param string $encoding
423
     * @return mixed
424
     */
425 5
    public static function ord($char, $encoding = 'UTF-8')
426
    {
427 5
        if (! $char && $char !== '0') {
428
            return 0;
429
        }
430 5
        if (! isset($char[1])) {
431
            return ord($char);
432
        }
433 5
        if ($encoding !== 'UCS-4BE') {
434 5
            $char = mb_convert_encoding($char, 'UCS-4BE', $encoding);
435
        }
436
437 5
        return unpack('N', $char)[1];
438
    }
439
440
    /**
441
     * Returns UTF-8 char code at given $positing of the $string
442
     *
443
     * @param string $string
444
     * @param int    $position
445
     * @return mixed
446
     */
447
    public static function charCodeAt($string, $position)
448
    {
449
        $char = mb_substr($string, $position, 1, 'UTF-8');
450
451
        return self::ord($char);
452
    }
453
454
    /**
455
     * @param int|null $code
456
     * @return string
457
     */
458 22
    public static function printCharCode($code)
459
    {
460 22
        if ($code === null) {
461 2
            return '<EOF>';
462
        }
463
464 20
        return $code < 0x007F
465
            // Trust JSON for ASCII.
466 18
            ? json_encode(self::chr($code))
467
            // Otherwise print the escaped form.
468 20
            : '"\\u' . dechex($code) . '"';
469
    }
470
471
    /**
472
     * Upholds the spec rules about naming.
473
     *
474
     * @param string $name
475
     * @throws Error
476
     */
477 26
    public static function assertValidName($name)
478
    {
479 26
        $error = self::isValidNameError($name);
480 25
        if ($error) {
481 2
            throw $error;
482
        }
483 23
    }
484
485
    /**
486
     * Returns an Error if a name is invalid.
487
     *
488
     * @param string    $name
489
     * @param Node|null $node
490
     * @return Error|null
491
     */
492 89
    public static function isValidNameError($name, $node = null)
493
    {
494 89
        self::invariant(is_string($name), 'Expected string');
495
496 88
        if (isset($name[1]) && $name[0] === '_' && $name[1] === '_') {
497 75
            return new Error(
498 75
                sprintf('Name "%s" must not begin with "__", which is reserved by ', $name) .
499 75
                'GraphQL introspection.',
500 75
                $node
501
            );
502
        }
503
504 87
        if (! preg_match('/^[_a-zA-Z][_a-zA-Z0-9]*$/', $name)) {
505 6
            return new Error(
506 6
                sprintf('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%s" does not.', $name),
507 6
                $node
508
            );
509
        }
510
511 86
        return null;
512
    }
513
514
    /**
515
     * Wraps original closure with PHP error handling (using set_error_handler).
516
     * Resulting closure will collect all PHP errors that occur during the call in $errors array.
517
     *
518
     * @param \ErrorException[] $errors
519
     * @return \Closure
520
     */
521
    public static function withErrorHandling(callable $fn, array &$errors)
522
    {
523
        return function () use ($fn, &$errors) {
524
            // Catch custom errors (to report them in query results)
525
            set_error_handler(function ($severity, $message, $file, $line) use (&$errors) {
526
                $errors[] = new \ErrorException($message, 0, $severity, $file, $line);
527
            });
528
529
            try {
530
                return $fn();
531
            } finally {
532
                restore_error_handler();
533
            }
534
        };
535
    }
536
537
    /**
538
     * @param string[] $items
539
     * @return string
540
     */
541 21
    public static function quotedOrList(array $items)
542
    {
543 21
        $items = array_map(
544
            function ($item) {
545 20
                return sprintf('"%s"', $item);
546 21
            },
547 21
            $items
548
        );
549
550 21
        return self::orList($items);
551
    }
552
553
    /**
554
     * @param string[] $items
555
     * @return string
556
     */
557 29
    public static function orList(array $items)
558
    {
559 29
        if (count($items) === 0) {
560 1
            throw new \LogicException('items must not need to be empty.');
561
        }
562 28
        $selected       = array_slice($items, 0, 5);
563 28
        $selectedLength = count($selected);
564 28
        $firstSelected  = $selected[0];
565
566 28
        if ($selectedLength === 1) {
567 17
            return $firstSelected;
568
        }
569
570 11
        return array_reduce(
571 11
            range(1, $selectedLength - 1),
572
            function ($list, $index) use ($selected, $selectedLength) {
573
                return $list .
574 11
                    ($selectedLength > 2 ? ', ' : ' ') .
575 11
                    ($index === $selectedLength - 1 ? 'or ' : '') .
576 11
                    $selected[$index];
577 11
            },
578 11
            $firstSelected
579
        );
580
    }
581
582
    /**
583
     * Given an invalid input string and a list of valid options, returns a filtered
584
     * list of valid options sorted based on their similarity with the input.
585
     *
586
     * Includes a custom alteration from Damerau-Levenshtein to treat case changes
587
     * as a single edit which helps identify mis-cased values with an edit distance
588
     * of 1
589
     * @param string   $input
590
     * @param string[] $options
591
     * @return string[]
592
     */
593 42
    public static function suggestionList($input, array $options)
594
    {
595 42
        $optionsByDistance = [];
596 42
        $inputThreshold    = mb_strlen($input) / 2;
597 42
        foreach ($options as $option) {
598 41
            if ($input === $option) {
599 1
                $distance = 0;
600
            } else {
601 41
                $distance = (strtolower($input) === strtolower($option)
602 4
                    ? 1
603 41
                    : levenshtein($input, $option));
604
            }
605 41
            $threshold = max($inputThreshold, mb_strlen($option) / 2, 1);
606 41
            if ($distance > $threshold) {
607 38
                continue;
608
            }
609
610 18
            $optionsByDistance[$option] = $distance;
611
        }
612
613 42
        asort($optionsByDistance);
614
615 42
        return array_keys($optionsByDistance);
616
    }
617
}
618