InvalidTypeExceptionTrait::getException()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.2
c 1
b 0
f 0
cc 4
eloc 8
nc 4
nop 4
crap 4
1
<?php
2
3
namespace ElasticsearchModule\Service;
4
5
use Exception;
6
7
/**
8
 * @author Pedro Alves <[email protected]>
9
 */
10
trait InvalidTypeExceptionTrait
11
{
12
    /**
13
     * @param string $name
14
     * @param string $classNameOrType
15
     * @param string $exceptionClass
16
     * @param mixed $var
17
     * @return Exception
18
     */
19 6
    private function getException($name, $classNameOrType, $exceptionClass, $var)
20
    {
21 6
        $message = (class_exists($classNameOrType) || interface_exists($classNameOrType)) ?
22 6
            "instance of %s" : "%s";
23 6
        return new $exceptionClass(sprintf(
24 6
            "The %s must be $message, %s given",
25 6
            $name,
26 6
            $classNameOrType,
27 6
            is_object($var) ? get_class($var) : gettype($var)
28 6
        ));
29
    }
30
}
31