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

Closures   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
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 30
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
     * @return bool|null the filtered $value
22
     *
23
     * @throws FilterException
24
     */
25
    public static function filter($value, bool $allowNull = false)
26
    {
27
        if ($allowNull === true && $value === null) {
28
            return null;
29
        }
30
31
        if ($value instanceof \Closure) {
32
            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...
33
        }
34
35
        if (is_callable($value)) {
36
            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...
37
        }
38
39
        throw new FilterException('Value "' . var_export($value, true) . '" is not Closure or $allowNull is not set');
40
    }
41
}
42