Completed
Push — 2.x ( 90b2f3...6f029c )
by Paul
03:31
created

Extant   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 2
c 1
b 1
f 1
lcom 0
cbo 0
dl 0
loc 41
ccs 11
cts 11
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 27 2
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\Rule\Validate;
10
11
/**
12
 *
13
 * Validates that the the subject field exists, even if null.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class Extant
19
{
20
    /**
21
     *
22
     * Validates that the the subject field exists, even if null.
23
     *
24
     * @param object $subject The subject to be filtered.
25
     *
26
     * @param string $field The subject field name.
27
     *
28
     * @return bool True if valid, false if not.
29
     *
30
     */
31 2
    public function __invoke($subject, $field)
32
    {
33 2
        if (isset($subject->$field)) {
34 1
            return true;
35
        }
36
37
        // still, the property might exist and be null. using property_exists()
38
        // presumes that we have a non-magic-method object, which may not be the
39
        // case, so we have this hackish approach.
40
41
        // first, turn off error reporting entirely.
42 2
        $level = error_reporting(0);
43
44
        // now put error_get_last() into known state by addressing a nonexistent
45
        // variable with an unlikely name.
46 2
        $fake = __FILE__ . ':' . __CLASS__;
47 2
        $value = $$fake;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
        // now get the value of the field and turn error reporting back on
50 2
        $value = $subject->$field;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51 2
        error_reporting($level);
52
53
        // if the last error was on $field, then $field is nonexistent.
54 2
        $error = error_get_last();
55 2
        $property = substr($error['message'], -1 * strlen($field) - 1);
56 2
        return $property !== "\$$field";
57
    }
58
}
59