UnexpectedTypeException::forType()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 1
nop 4
crap 2
1
<?php
2
3
/**
4
 * This file is part of the phpcommon/comparison package.
5
 *
6
 * (c) Marcos Passos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace PhpCommon\Comparison;
13
14
use Exception;
15
use InvalidArgumentException;
16
17
/**
18
 * Thrown when a value does not match an expected type.
19
 *
20
 * @author Marcos Passos <[email protected]>
21
 */
22 View Code Duplication
class UnexpectedTypeException extends InvalidArgumentException
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * Creates a new exception for the given value type.
26
     *
27
     * @param string         $type  The name of the expected type.
28
     * @param mixed          $value The specified value.
29
     * @param int            $code  The exception code.
30
     * @param Exception|null $cause The exception that caused this exception.
31
     *
32
     * @return UnexpectedTypeException The new exception.
33
     */
34 2
    public static function forType($type, $value, $code = 0, Exception $cause = null)
35
    {
36 2
        return new self(
37 2
            sprintf(
38 2
                'Expected value of type "%s", given "%s".',
39 2
                $type,
40 2
                is_object($value) ? get_class($value) : gettype($value)
41 2
            ),
42 2
            $code,
43
            $cause
44 2
        );
45
    }
46
}
47