Factory::extend()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.5555
c 0
b 0
f 0
cc 5
nc 3
nop 2
1
<?php
2
/**
3
 * File copied from Waavi/Sanitizer https://github.com/waavi/sanitizer
4
 * Sanitization functionality to be customized within this project before a 1.0 release.
5
 */
6
7
namespace PerfectOblivion\Valid\Sanitizer\Laravel;
8
9
use Closure;
10
use InvalidArgumentException;
11
use PerfectOblivion\Valid\Sanitizer\Sanitizer;
12
use PerfectOblivion\Valid\Sanitizer\Contracts\Filter;
13
14
class Factory
15
{
16
    /** @var array */
17
    protected $customFilters;
18
19
    /**
20
     * Create a new Sanitizer factory instance.
21
     */
22
    public function __construct()
23
    {
24
        $this->customFilters = [];
25
    }
26
27
    /**
28
     * Create a new Sanitizer instance.
29
     *
30
     * @param  array  $data       Data to be sanitized
31
     * @param  array  $rules      Filters to be applied to the given data
32
     *
33
     * @return Sanitizer
34
     */
35
    public function make(array $data, array $rules)
36
    {
37
        $sanitizer = new Sanitizer($data, $rules, $this->customFilters);
38
39
        return $sanitizer;
40
    }
41
42
    /**
43
     *  Add a custom filters to all Sanitizers created with this Factory.
44
     *
45
     *  @param  string  $name  Name of the filter
46
     *  @param  mixed  $extension  Either the full class name of a Filter class implementing the Filter contract, or a Closure.
0 ignored issues
show
Bug introduced by
There is no parameter named $extension. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
     *
48
     *  @throws InvalidArgumentException
49
     */
50
    public function extend($name, $customFilter)
51
    {
52
        if (empty($name) || ! is_string($name)) {
53
            throw new InvalidArgumentException('The Sanitizer filter name must be a non-empty string.');
54
        }
55
56
        if (! ($customFilter instanceof Closure) && ! in_array(Filter::class, class_implements($customFilter))) {
57
            throw new InvalidArgumentException('Custom filter must be a Closure or a class implementing the PerfectOblivion\Valid\Sanitizer\Contracts\Filter interface.');
58
        }
59
60
        $this->customFilters[$name] = $customFilter;
61
    }
62
}
63