Completed
Push — master ( 8507e2...2d694e )
by Lars
01:39
created

TypeCheckSimple   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A throwException() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arrayy\TypeCheck;
6
7
final class TypeCheckSimple extends AbstractTypeCheck implements TypeCheckInterface
8
{
9
    /**
10
     * @param string|string[] $type
11
     * @param bool            $isNullable
12
     */
13 72
    public function __construct($type, bool $isNullable = false)
14
    {
15 72
        if (\is_array($type)) {
16 2
            foreach ($type as $typeTmp) {
17 2
                $this->types[] = $typeTmp;
18
            }
19
        } else {
20 72
            $this->types[] = $type;
21
        }
22
23 72
        $this->isNullable = $isNullable;
24 72
    }
25
26
    /**
27
     * @param string $expectedTypes
28
     * @param mixed  $value
29
     * @param string $type
30
     *
31
     * @return \TypeError
32
     */
33 21
    public function throwException($expectedTypes, $value, $type): \Throwable
34
    {
35 21
        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...
36
    }
37
}
38