|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of sensorario/resources repository |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Simone Gentili <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sensorario\Resources\Validators\Validators; |
|
13
|
|
|
|
|
14
|
|
|
use Sensorario\Resources\Resource; |
|
15
|
|
|
use Sensorario\Resources\Validators\Interfaces\Validator; |
|
16
|
|
|
|
|
17
|
|
|
final class MandatoryConditional implements Validator |
|
18
|
|
|
{ |
|
19
|
32 |
|
public function check(Resource $resource) |
|
20
|
|
|
{ |
|
21
|
32 |
|
foreach ($resource->mandatory() as $key => $value) { |
|
22
|
19 |
|
if (isset($value['when']['has_value'])) { |
|
23
|
4 |
|
$name = $value['when']['property']; |
|
24
|
4 |
|
$value = $value['when']['has_value']; |
|
25
|
|
|
|
|
26
|
4 |
|
if ($isArray = is_array($value)) { |
|
|
|
|
|
|
27
|
3 |
|
foreach ($value as $value) { |
|
28
|
3 |
|
if ($resource->get($name) === $value && $resource->hasNotProperty($key)) { |
|
29
|
3 |
|
self::exceptionMessage($key, $value, $key); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} else { |
|
33
|
1 |
|
if ($resource->get($name) === $value && $resource->hasNotProperty($key)) { |
|
34
|
1 |
|
self::exceptionMessage($name, $value, $key); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
30 |
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
private static function exceptionMessage($name, $value, $key) |
|
42
|
|
|
{ |
|
43
|
2 |
|
throw new \Sensorario\Resources\Exceptions\PropertyException( |
|
44
|
2 |
|
'When property `' . $name . '` ' |
|
45
|
2 |
|
. 'has value ' . '`' . $value . '` ' |
|
46
|
2 |
|
. 'also `' . $key . '` is mandatory' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
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.