ValueFilter::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter;
10
11
use Aura\Filter\Locator\Locator;
12
use Aura\Filter\Locator\SanitizeLocator;
13
use Aura\Filter\Locator\ValidateLocator;
14
15
/**
16
 *
17
 * A filter for individual values.
18
 *
19
 * @package Aura.Filter
20
 *
21
 */
22
class ValueFilter
23
{
24
    /**
25
     *
26
     * A pesudo-subject to hold the value being filtered.
27
     *
28
     * @var object
29
     *
30
     */
31
    protected $subject;
32
33
    /**
34
     *
35
     * Constructor.
36
     *
37
     * @param ValidateLocator $validate_locator A locator for "validate" rules.
38
     *
39
     * @param SanitizeLocator $sanitize_locator A locator for "sanitize" rules.
40
     *
41
     * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
42
     *
43
     */
44 4
    public function __construct(
45
        ValidateLocator $validate_locator,
46
        SanitizeLocator $sanitize_locator
47
    ) {
48 4
        $this->validate_locator = $validate_locator;
0 ignored issues
show
Bug introduced by
The property validate_locator 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...
49 4
        $this->sanitize_locator = $sanitize_locator;
0 ignored issues
show
Bug introduced by
The property sanitize_locator 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...
50 4
        $this->subject = (object) array('value' => null);
51 4
    }
52
53
    /**
54
     *
55
     * Validates a value using a rule.
56
     *
57
     * @param mixed $value The value to validate.
58
     *
59
     * @param string $rule The rule name.
60
     *
61
     * @param ...$args Arguments to pass to the rule.
62
     *
63
     * @return bool True on success, false on failure.
64
     *
65
     */
66 2
    public function validate($value, $rule)
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68 2
        $this->subject->value = $value;
69 2
        return $this->apply($this->validate_locator, func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
    }
71
72
    /**
73
     *
74
     * Sanitizes a value in place using a rule.
75
     *
76
     * @param mixed $value The value to sanitize.
77
     *
78
     * @param string $rule The rule name.
79
     *
80
     * @param ...$args Arguments to pass to the rule.
81
     *
82
     * @return bool True on success, false on failure.
83
     *
84
     */
85 2
    public function sanitize(&$value, $rule)
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87 2
        $this->subject->value =& $value;
88 2
        return $this->apply($this->sanitize_locator, func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
    }
90
91
    /**
92
     *
93
     * Applies a rule.
94
     *
95
     * @param Locator $locator A rule locator.
96
     *
97
     * @param string $args Arugments for the rule.
98
     *
99
     * @return bool True on success, false on failure.
100
     *
101
     */
102 2
    protected function apply(Locator $locator, $args)
103
    {
104 2
        array_shift($args); // remove $value
105 2
        $rule = array_shift($args); // remove $rule
106 2
        $rule = $locator->get($rule); // create rule object
107
108 2
        array_unshift($args, 'value'); // add field name on $this->subject
109 2
        array_unshift($args, $this->subject); // add $this->subject
110 2
        return call_user_func_array($rule, $args);
111
    }
112
}
113