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

In   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 53
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A parse_as() 0 3 1
A fetch_arguments() 0 4 1
A arguments_are_valid() 0 6 2
A compile() 0 14 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