GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Dispatch::toNative()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 31
ccs 15
cts 16
cp 0.9375
rs 9.1111
c 0
b 0
f 0
cc 6
nc 10
nop 2
crap 6.0087
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of Underscore.php
6
 *
7
 * (c) Maxime Fabre <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Underscore;
14
15
use Closure;
16
use InvalidArgumentException;
17
18
/**
19
 * Dispatches methods and classes to various places.
20
 *
21
 * @see \Underscore\DispatchTest
22
 */
23
class Dispatch
24
{
25
    /**
26
     * The namespace containing the Type classes.
27
     */
28
    final public const TYPES = 'Underscore\Types\\';
29
30
    /**
31
     * An array of PHP types and what classes they map to.
32
     */
33
    protected static array $classmap = [
34
        'array'   => 'Arrays',
35
        'double'  => 'Number',
36
        'closure' => 'Functions',
37
        'float'   => 'Number',
38
        'integer' => 'Number',
39
        'NULL'    => 'Strings',
40
        'object'  => 'BaseObject',
41
        'real'    => 'Number',
42
        'string'  => 'Strings',
43
    ];
44
45
    /**
46
     * Compute the right class to call according to something's type.
47
     *
48
     * @param mixed $subject The subject of a class
49
     *
50
     * @return string Its fully qualified corresponding class
51
     */
52 35
    public static function toClass(mixed $subject) : string
53
    {
54 35
        $subjectType = \gettype($subject);
55 35
        if ($subject instanceof Closure) {
56 1
            $subjectType = 'closure';
57
        }
58
59
        // Return correct class
60 35
        if (\array_key_exists($subjectType, static::$classmap)) {
61 34
            return static::TYPES.static::$classmap[$subjectType];
62
        }
63
64 1
        throw new InvalidArgumentException('The type '.$subjectType.' is not supported');
65
    }
66
67
    /**
68
     * Defer a method to native PHP.
69
     *
70
     * @param  string  $class  The class
71
     * @param string $method The method
72
     *
73
     * @return bool|string The correct function to call
74
     */
75 13
    public static function toNative(string $class, string $method) : bool|string
76
    {
77
        // Aliased native function
78 13
        $native = Method::getNative($method);
79 13
        if ($native) {
80 2
            return $native;
81
        }
82
83
        // Transform class to php function prefix
84
        switch ($class) {
85 12
            case static::TYPES.'Arrays':
86 10
                $prefix = 'array_';
87 10
                break;
88
89 6
            case static::TYPES.'Strings':
90 6
                $prefix = 'str_';
91 6
                break;
92
        }
93
94
        // If no function prefix found, return false
95 12
        if (!isset($prefix)) {
96
            return false;
97
        }
98
99
        // Native function
100 12
        $function = $prefix.$method;
101 12
        if (\function_exists($function)) {
102 8
            return $function;
103
        }
104
105 4
        return false;
106
    }
107
}
108