CallbackSpecification::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\Specification\Helper;
4
5
use BenTools\Specification\Specification;
6
use function BenTools\Specification\reject;
7
8
final class CallbackSpecification extends Specification
9
{
10
    /**
11
     * @var callable
12
     */
13
    private $callback;
14
15
    /**
16
     * CallbackSpecification constructor.
17
     *
18
     * @param callable    $callback
19
     * @param null|string $name
20
     */
21
    protected function __construct(callable $callback, ?string $name)
22
    {
23
        $this->callback = $callback;
24
        $this->label = $name;
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function validate(): void
31
    {
32
        $callback = $this->callback;
33
        $result = $callback();
34
        if (!is_bool($result)) {
35
            throw new \TypeError("The result of a callback should be of boolean type.");
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with 'The result of a callbac...ld be of boolean 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
        if (false === $result) {
38
            reject($this);
39
        }
40
    }
41
}
42