1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Variables; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Graph\Node; |
14
|
|
|
use Lechimp\Dicto\Graph\Relation; |
15
|
|
|
use Lechimp\Dicto\Definition\ArgumentParser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Name is a property, right? |
19
|
|
|
*/ |
20
|
|
|
class In extends Property { |
21
|
|
|
static private $relations = ["contained in"]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdocs |
25
|
|
|
*/ |
26
|
29 |
|
public function name() { |
27
|
29 |
|
return "in"; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdocs |
32
|
|
|
*/ |
33
|
29 |
|
public function parse_as() { |
34
|
29 |
|
return $this->name(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdocs |
39
|
|
|
*/ |
40
|
1 |
|
public function fetch_arguments(ArgumentParser $parser) { |
41
|
1 |
|
$other = $parser->fetch_variable(); |
42
|
1 |
|
return array($other); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdocs |
47
|
|
|
*/ |
48
|
9 |
|
public function arguments_are_valid(array &$arguments) { |
49
|
9 |
|
if (count($arguments) != 1) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
9 |
|
return $arguments[0] instanceof Variable; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdocs |
57
|
|
|
*/ |
58
|
8 |
|
public function compile(array &$arguments, $negate = false) { |
59
|
8 |
|
$condition = $arguments[0]->compile(); |
60
|
8 |
|
if (!$negate) { |
61
|
|
View Code Duplication |
return function (Node $n) use ($condition) { |
|
|
|
|
62
|
|
|
$nodes = $n->related_nodes(function (Relation $r) use ($condition) { |
63
|
5 |
|
return in_array($r->type(), self::$relations); |
64
|
5 |
|
}); |
65
|
5 |
|
foreach ($nodes as $node) { |
66
|
5 |
|
if ($condition($node)) { |
67
|
3 |
|
return true; |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
3 |
|
return false; |
71
|
7 |
|
}; |
72
|
|
|
} |
73
|
|
|
else { |
74
|
|
View Code Duplication |
return function (Node $n) use ($condition) { |
|
|
|
|
75
|
1 |
|
$nodes = $n->related_nodes(function (Relation $r) use ($condition) { |
76
|
1 |
|
return in_array($r->type(), self::$relations); |
77
|
1 |
|
}); |
78
|
1 |
|
foreach ($nodes as $node) { |
79
|
1 |
|
if ($condition($node)) { |
80
|
1 |
|
return false; |
81
|
|
|
} |
82
|
1 |
|
} |
83
|
1 |
|
return true; |
84
|
1 |
|
}; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.