Closures   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
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 28
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
     * The return value is \Closure, as expected by the \TraderInteractive\Filterer class.
15
     *
16
     * @param mixed $value       the value to filter to a closure function
17
     * @param bool  $allowNull   Set to true if NULL values are allowed. The filtered result of a NULL value is NULL
18
     *
19
     * @return \Closure|null the filtered $value
20
     *
21
     * @throws FilterException
22
     */
23
    public static function filter($value, bool $allowNull = false)
24
    {
25
        if ($allowNull === true && $value === null) {
26
            return null;
27
        }
28
29
        if ($value instanceof \Closure) {
30
            return $value;
31
        }
32
33
        if (is_callable($value)) {
34
            return \Closure::fromCallable($value);
35
        }
36
37
        throw new FilterException('Value "' . var_export($value, true) . '" is not Closure or $allowNull is not set');
38
    }
39
}
40