InvalidArgumentException::validateArgument()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 16
rs 9.4285
1
<?php
2
namespace Crossjoin\Json\Exception;
3
4
/**
5
 * Class InvalidArgumentException
6
 *
7
 * @package Crossjoin\Json\Exception
8
 * @author Christoph Ziegenberg <[email protected]>
9
 */
10
class InvalidArgumentException extends \InvalidArgumentException implements JsonException
11
{
12
    const TYPE_INTEGER = 'integer';
13
    const TYPE_STRING  = 'string';
14
    const TYPE_BOOLEAN = 'boolean';
15
16
    /** @noinspection MoreThanThreeArgumentsInspection */
17
    /**
18
     * @param $expectedType
19
     * @param $argumentName
20
     * @param $currentValue
21
     * @param $code
22
     *
23
     * @return bool
24
     * @throws \Crossjoin\Json\Exception\InvalidArgumentException
25
     */
26
    public static function validateArgument($expectedType, $argumentName, $currentValue, $code)
27
    {
28
        if (gettype($currentValue) !== $expectedType) {
29
            throw new self(
30
                sprintf(
31
                    "%s expected for argument '%s'. Got '%s'.",
32
                    ucfirst(strtolower($expectedType)),
33
                    $argumentName,
34
                    gettype($currentValue)
35
                ),
36
                $code
37
            );
38
        }
39
40
        return true;
41
    }
42
}
43