Failed Conditions
Pull Request — master (#102)
by
unknown
01:37
created

Closures   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A filter() 0 15 5
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
/**
8
 * A collection of filters for Closures.
9
 */
10
class Closures
11
{
12
    /**
13
     * Filters $value to a Closure strictly.
14
     *
15
     * $value must be a bool or 'true' or 'false' disregarding case and whitespace.
16
     *
17
     * The return value is \Closure, as expected by the \TraderInteractive\Filterer class.
18
     *
19
     * @param mixed $value       the value to filter to a boolean
20
     * @param bool  $allowNull   Set to true if NULL values are allowed. The filtered result of a NULL value is NULL
21
     *
22
     * @return bool|null the filtered $value
23
     *
24
     * @throws FilterException
25
     */
26
    public static function filter($value, bool $allowNull = false)
27
    {
28
        if ($allowNull === true && $value === null) {
29
            return null;
30
        }
31
32
        if ($value instanceof \Closure) {
33
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type Closure which is incompatible with the documented return type boolean|null.
Loading history...
34
        }
35
36
        if (is_callable($value)) {
37
            return \Closure::fromCallable($value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Closure::fromCallable($value) returns the type Closure which is incompatible with the documented return type boolean|null.
Loading history...
38
        }
39
40
        throw new FilterException('Value "' . var_export($value, true) . '" is not Closure or $allowNull is not set');
41
    }
42
}
43