CallbackSpecification   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 11 3
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