Passed
Push — master ( a085c9...e54398 )
by Tim
02:26
created

Assert::valueToString()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 21
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 43
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Assert;
6
7
use BadMethodCallException; // Requires ext-spl
8
use DateTime; // requires ext-date
9
use DateTimeImmutable; // requires ext-date
10
use InvalidArgumentException; // Requires ext-spl
11
use SimpleSAML\Assert\Assert as BaseAssert;
12
use SimpleSAML\Assert\AssertionFailedException;
13
use Throwable;
14
15
use function array_pop;
16
use function array_unshift;
17
use function call_user_func_array;
18
use function end;
19
use function enum_exists;
0 ignored issues
show
introduced by
The function enum_exists was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use function function_exists;
21
use function get_class;
22
use function is_object;
23
use function is_resource;
24
use function is_string;
25
use function is_subclass_of;
26
use function lcfirst;
27
use function method_exists;
28
use function preg_match; // Requires ext-pcre
29
use function strval;
30
31
/**
32
 * SimpleSAML\SAML2\Assert\Assert wrapper class
33
 *
34
 * @package simplesamlphp/saml2
35
 *
36
 * @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
37
 * @method static void validURI(mixed $value, string $message = '', string $exception = '')
38
 * @method static void validEntityID(mixed $value, string $message = '', string $exception = '')
39
 * @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
40
 * @method static void nullOrValidURI(mixed $value, string $message = '', string $exception = '')
41
 * @method static void nullOrValidEntityID(mixed $value, string $message = '', string $exception = '')
42
 * @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
43
 * @method static void allValidURI(mixed $value, string $message = '', string $exception = '')
44
 * @method static void allValidEntityID(mixed $value, string $message = '', string $exception = '')
45
 */
46
final class Assert
47
{
48
    use CustomAssertionTrait;
49
50
51
    /**
52
     * @param string $name
53
     * @param array<mixed> $arguments
54
     */
55
    public static function __callStatic(string $name, array $arguments): void
56
    {
57
        // Handle Exception-parameter
58
        $exception = AssertionFailedException::class;
59
60
        $last = end($arguments);
61
        if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) {
62
            $exception = $last;
63
            array_pop($arguments);
64
        }
65
66
        try {
67
            if (method_exists(static::class, $name)) {
68
                call_user_func_array([static::class, $name], $arguments);
69
                return;
70
            } elseif (preg_match('/^nullOr(.*)$/i', $name, $matches)) {
71
                $method = lcfirst($matches[1]);
72
                if (method_exists(static::class, $method)) {
73
                    call_user_func_array([static::class, 'nullOr'], [[static::class, $method], $arguments]);
74
                } elseif (method_exists(BaseAssert::class, $method)) {
75
                    call_user_func_array([static::class, 'nullOr'], [[BaseAssert::class, $method], $arguments]);
76
                } else {
77
                    throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
78
                }
79
            } elseif (preg_match('/^all(.*)$/i', $name, $matches)) {
80
                $method = lcfirst($matches[1]);
81
                if (method_exists(static::class, $method)) {
82
                    call_user_func_array([static::class, 'all'], [[static::class, $method], $arguments]);
83
                } elseif (method_exists(BaseAssert::class, $method)) {
84
                    call_user_func_array([static::class, 'all'], [[BaseAssert::class, $method], $arguments]);
85
                } else {
86
                    throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
87
                }
88
            } else {
89
                throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $name));
90
            }
91
        } catch (InvalidArgumentException $e) {
92
            throw new $exception($e->getMessage());
93
        }
94
    }
95
96
97
    /**
98
     * Handle nullOr* for either Webmozart or for our custom assertions
99
     *
100
     * @param callable $method
101
     * @param array<mixed> $arguments
102
     * @return void
103
     */
104
    private static function nullOr(callable $method, array $arguments): void
105
    {
106
        $value = reset($arguments);
107
        ($value === null) || call_user_func_array($method, $arguments);
108
    }
109
110
111
    /**
112
     * all* for our custom assertions
113
     *
114
     * @param callable $method
115
     * @param array<mixed> $arguments
116
     * @return void
117
     */
118
    private static function all(callable $method, array $arguments): void
119
    {
120
        $values = array_pop($arguments);
121
        foreach ($values as $value) {
122
            $tmp = $arguments;
123
            array_unshift($tmp, $value);
124
            call_user_func_array($method, $tmp);
125
        }
126
    }
127
128
129
    /**
130
     * @param mixed $value
131
     *
132
     * @return string
133
     */
134
    protected static function valueToString(mixed $value): string
135
    {
136
        if (is_resource($value)) {
137
            return 'resource';
138
        }
139
140
        if (null === $value) {
141
            return 'null';
142
        }
143
144
        if (true === $value) {
145
            return 'true';
146
        }
147
148
        if (false === $value) {
149
            return 'false';
150
        }
151
152
        if (is_array($value)) {
153
            return 'array';
154
        }
155
156
        if (is_object($value)) {
157
            if (method_exists($value, '__toString')) {
158
                return $value::class . ': ' . self::valueToString($value->__toString());
159
            }
160
161
            if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
162
                return $value::class . ': ' . self::valueToString($value->format('c'));
163
            }
164
165
            if (function_exists('enum_exists') && enum_exists(get_class($value))) {
166
                return get_class($value) . '::' . $value->name;
167
            }
168
169
            return $value::class;
170
        }
171
172
        if (is_string($value)) {
173
            return '"' . $value . '"';
174
        }
175
176
        return strval($value);
177
    }
178
}
179