Completed
Push — master ( 59677e...d46b94 )
by Vincent
02:13
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 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 0
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\PhpunitException\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\PhpunitException\Exception\InvalidArgumentException with customized message.
20
     *
21
     * @param integer $argument
22
     * @param string  $type
23
     * @param mixed   $value
24
     *
25
     * @return InvalidArgumentException
26
     */
27 3
    public static function factory(int $argument, string $type, $value = null): InvalidArgumentException
28
    {
29 3
        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 6
    public static function message(int $argument, string $type, $value = null): string
42
    {
43 6
        $stack = \debug_backtrace();
44
45 6
        return \sprintf(
46 6
            InvalidArgumentException::MESSAGE,
47 6
            $argument,
48 6
            $value !== null ? ' (' . \gettype($value) . '#' . \var_export($value, true) . ')' : ' (No Value)',
49 6
            $stack[4]['class'],
50 6
            $stack[4]['function'],
51 6
            $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 12
    public static function messageRegex(int $argument, string $type, $value = null): string
65
    {
66 12
        return \sprintf(
67 12
            '/' . \preg_quote(InvalidArgumentException::MESSAGE) . '/',
68 12
            $argument,
69 12
            ($value !== null) ?
70 12
                \preg_quote(' (' . \gettype($value) . '#' . \var_export($value, true) . ')') : '[\s\S]*',
71 12
            '.*',
72 12
            '.*',
73 12
            \preg_quote($type)
74
        );
75
    }
76
}
77