Completed
Push — master ( f7852e...e666ea )
by Richard
06:08
created

In   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 32.35 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 22
loc 68
ccs 30
cts 31
cp 0.9677
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
B compile() 22 29 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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