Completed
Push — master ( e666ea...addc16 )
by Richard
05:13
created

In::compile()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 1
nop 1
crap 3
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 8
    public function arguments_are_valid(array &$arguments) {
49 8
        if (count($arguments) != 1) {
50
            return false;
51
        }
52 8
        return $arguments[0] instanceof Variable;
53
    }
54
55
    /**
56
     * @inheritdocs
57
     */
58 7
    public function compile(array &$arguments) {
59 7
        $condition = $arguments[0]->compile();
60
        return function (Node $n) use ($condition) {
61 5
            $nodes = $n->related_nodes(function (Relation $r) use ($condition) {
62 5
                return in_array($r->type(), self::$relations);
63 5
            });
64 5
            foreach ($nodes as $node) {
65 5
                if ($condition($node)) {
66 3
                    return true;
67
                }
68 3
            }
69 3
            return false;
70 7
        };
71
    }
72
}
73