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

TypeCheckSimple   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A throwException() 0 4 1
A getTypesHelper() 0 20 5
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