Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

InvalidArgumentHelper::messageRegex()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 1
nop 3
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is inspired from PHPUnit\Util\InvalidArgumentHelper.
7
 */
8
9
namespace VGirol\JsonApiStructure\Exception;
10
11
/**
12
 * Factory for VGirol\JsonApiStructure\Exception\InvalidArgumentException
13
 *
14
 * @internal
15
 */
16
final class InvalidArgumentHelper
17
{
18
    /**
19
     * Creates a new instance of VGirol\JsonApiStructure\InvalidArgumentException with customized message.
20
     *
21
     * @param integer $argument
22
     * @param string  $type
23
     * @param mixed   $value
24
     *
25
     * @return InvalidArgumentException
26
     */
27 48
    public static function factory(int $argument, string $type, $value = null): InvalidArgumentException
28
    {
29 48
        return new InvalidArgumentException(static::message($argument, $type, $value));
30
    }
31
32
    /**
33
     * Format the message for the exception
34
     *
35
     * @param integer $argument
36
     * @param string  $type
37
     * @param mixed   $value
38
     *
39
     * @return string
40
     */
41 48
    public static function message(int $argument, string $type, $value = null): string
42
    {
43 48
        $stack = \debug_backtrace();
44
45 48
        return \sprintf(
46 48
            InvalidArgumentException::MESSAGE,
47 48
            $argument,
48 48
            $value !== null ? ' (' . \gettype($value) . '#' . \var_export($value, true) . ')' : ' (No Value)',
49 48
            $stack[4]['class'],
50 48
            $stack[4]['function'],
51 48
            $type
52
        );
53
    }
54
55
    /**
56
     * Get the message of the exception as a regular expression
57
     *
58
     * @param integer $argument
59
     * @param string  $type
60
     * @param mixed   $value
61
     *
62
     * @return string
63
     */
64 45
    public static function messageRegex(int $argument, string $type, $value = null): string
65
    {
66 45
        return \sprintf(
67 45
            '/' . \preg_quote(InvalidArgumentException::MESSAGE) . '/',
68 45
            $argument,
69 45
            ($value !== null) ?
70 45
                ' \(' . \gettype($value) . '#' . \preg_quote(\var_export($value, true)) . '\)' : '[\s\S]*',
71 45
            '.*',
72 45
            '.*',
73 45
            \preg_quote($type)
74
        );
75
    }
76
}
77