Completed
Push — master ( be043b...2df975 )
by Lars
01:40
created

TypeCheckSimple::getTypesHelper()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 20
ccs 8
cts 11
cp 0.7272
crap 5.5069
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arrayy\TypeCheck;
6
7
class TypeCheckSimple extends AbstractTypeCheck implements TypeCheckInterface
8
{
9
    /**
10
     * @param string|string[] $type
11
     * @param bool            $isNullable
12
     */
13 73
    public function __construct($type, bool $isNullable = false)
14
    {
15 73
        $this->getTypesHelper($type);
16
17 73
        $this->isNullable = $isNullable;
18 73
    }
19
20
    /**
21
     * @param string $expectedTypes
22
     * @param mixed  $value
23
     * @param string $type
24
     *
25
     * @return \TypeError
26
     */
27 22
    public function throwException($expectedTypes, $value, $type): \Throwable
28
    {
29 22
        throw new \TypeError("Invalid type: expected to be of type {{$expectedTypes}}, instead got value `" . \print_r($value, true) . "` with type {{$type}}.");
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with "Invalid type: expected ...` with type {{$type}}.".

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
30
    }
31
32
    /**
33
     * @param mixed $type
34
     */
35 73
    protected function getTypesHelper($type)
36
    {
37 73
        if (\is_array($type)) {
38
            foreach ($type as $typeTmp) {
39
                $this->getTypesHelper($typeTmp);
40
            }
41
42
            return;
43
        }
44
45 73
        if (\strpos($type, '|') !== false) {
46 3
            $typesTmp = \explode('|', $type);
47
48 3
            foreach ($typesTmp as $typeTmp) {
49 3
                $this->types[] = $typeTmp;
50
            }
51
        } else {
52 73
            $this->types[] = $type;
53
        }
54 73
    }
55
}
56