SanitizesInput::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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
trait SanitizesInput
10
{
11
    /**
12
     *  Sanitize input before validating.
13
     *
14
     *  Kept for backwards compatibility with Laravel <= 5.5
15
     */
16
    public function validate()
17
    {
18
        $this->sanitize();
19
        parent::validate();
20
    }
21
22
    /**
23
     *  Sanitize input before validating.
24
     *
25
     *  Compatible with Laravel 5.6+
26
     */
27
    public function validateResolved()
28
    {
29
        $this->sanitize();
30
        parent::validateResolved();
31
    }
32
33
    /**
34
     *  Sanitize this request's input.
35
     */
36
    public function sanitize()
37
    {
38
        $this->addCustomFilters();
39
        $this->sanitizer = app('sanitizer')->make($this->input(), $this->filters());
0 ignored issues
show
Bug introduced by
The property sanitizer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like input() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
        $this->replace($this->sanitizer->sanitize());
0 ignored issues
show
Bug introduced by
It seems like replace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
    }
42
43
    /**
44
     *  Add custom fields to the Sanitizer.
45
     */
46
    public function addCustomFilters()
47
    {
48
        foreach ($this->customFilters() as $name => $filter) {
49
            app('sanitizer')->extend($name, $filter);
50
        }
51
    }
52
53
    /**
54
     *  Filters to be applied to the input.
55
     *
56
     *  @return array
57
     */
58
    public function filters()
59
    {
60
        return [];
61
    }
62
63
    /**
64
     *  Custom Filters to be applied to the input.
65
     *
66
     *  @return array
67
     */
68
    public function customFilters()
69
    {
70
        return [];
71
    }
72
}
73