|
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; |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
// now get the value of the field and turn error reporting back on |
|
50
|
2 |
|
$value = $subject->$field; |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.