Completed
Push — master ( 9971b9...3d6cb8 )
by Richard
06:14
created

DependOn   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 125
Duplicated Lines 33.6 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 97.98%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 42
loc 125
wmc 16
lcom 1
cbo 9
ccs 97
cts 99
cp 0.9798
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A register_listeners() 0 7 1
A register_method_call_listener() 19 19 2
A register_func_call_listener() 23 23 3
B register_global_listener() 0 22 4
A register_array_dim_fetch_listener() 0 21 4
A register_error_suppressor_listener() 0 16 1

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 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\Rules;
12
13
use Lechimp\Dicto\Variables\Variable;
14
use Lechimp\Dicto\Indexer\Location;
15
use Lechimp\Dicto\Indexer\Insert;
16
use Lechimp\Dicto\Indexer\ListenerRegistry;
17
use PhpParser\Node as N;
18
19
/**
20
 * A class or function is considered do depend on something if its body
21
 * of definition makes use of the thing. Language constructs, files or globals
22
 * can't depend on anything.
23
 */
24
class DependOn extends Relation {
25
    /**
26
     * @inheritdoc
27
     */
28 71
    public function name() {
29 71
        return "depend on";
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 10
    public function register_listeners(ListenerRegistry $registry) {
36 10
        $this->register_method_call_listener($registry);
37 10
        $this->register_func_call_listener($registry);
38 10
        $this->register_global_listener($registry);
39 10
        $this->register_array_dim_fetch_listener($registry);
40 10
        $this->register_error_suppressor_listener($registry);
41 10
    }
42
43 10 View Code Duplication
    protected function register_method_call_listener(ListenerRegistry $registry) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44 10
        $registry->on_enter_misc
45 10
            ( array(N\Expr\MethodCall::class)
46
            , function(Insert $insert, Location $location, N\Expr\MethodCall $node) {
47
                // The 'name' could also be a variable like in $this->$method();
48 2
                if (is_string($node->name)) {
49 2
                    $name_id = $insert->name
50 2
                        ( $node->name
51 2
                        , Variable::METHOD_TYPE
52 2
                        );
53 2
                    $this->insert_relation_into
54 2
                        ( $insert
55 2
                        , $location
56 2
                        , $name_id
57 2
                        , $node->getAttribute("startLine")
58 2
                        );
59 2
                }
60 10
            });
61 10
    }
62
63 10 View Code Duplication
    protected function register_func_call_listener(ListenerRegistry $registry) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64 10
        $registry->on_enter_misc
65 10
            ( array(N\Expr\FuncCall::class)
66
            , function(Insert $insert, Location $location, N\Expr\FuncCall $node) {
67
                // Omit calls to closures, we would not be able to
68
                // analyze them anyway atm.
69
                // Omit functions in arrays, we would not be able to
70
                // analyze them anyway atm.
71 1
                if (!($node->name instanceof N\Expr\Variable ||
72 1
                      $node->name instanceof N\Expr\ArrayDimFetch)) {
73 1
                    $name_id = $insert->name
74 1
                        ( $node->name->parts[0]
75 1
                        , Variable::FUNCTION_TYPE
76 1
                        );
77 1
                    $this->insert_relation_into
78 1
                        ( $insert
79 1
                        , $location
80 1
                        , $name_id
81 1
                        , $node->getAttribute("startLine")
82 1
                        );
83 1
                }
84 10
            });
85 10
    }
86
87 10
    protected function register_global_listener(ListenerRegistry $registry) {
88 10
        $registry->on_enter_misc
89 10
            ( array(N\Stmt\Global_::class)
90
            , function(Insert $insert, Location $location, N\Stmt\Global_ $node) {
91 2
                foreach ($node->vars as $var) {
92 2
                    if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) {
93
                        throw new \RuntimeException(
94
                            "Expected Variable with string name, found: ".print_r($var, true));
95
                    }
96 2
                    $name_id = $insert->name
97 2
                        ( $var->name
98 2
                        , Variable::GLOBAL_TYPE
99 2
                        );
100 2
                    $this->insert_relation_into
101 2
                        ( $insert
102 2
                        , $location
103 2
                        , $name_id
104 2
                        , $node->getAttribute("startLine")
105 2
                        );
106 2
                }
107 10
            });
108 10
    }
109
110 10
    protected function register_array_dim_fetch_listener(ListenerRegistry $registry) {
111 10
        $registry->on_enter_misc
112 10
            ( array(N\Expr\ArrayDimFetch::class)
113
            , function(Insert $insert, Location $location, N\Expr\ArrayDimFetch $node) {
114 1
                if ($node->var instanceof N\Expr\Variable 
115 1
                &&  $node->var->name == "GLOBALS"
116
                // Ignore usage of $GLOBALS with variable index.
117 1
                && !($node->dim instanceof N\Expr\Variable)) {
118 1
                    $name_id = $insert->name
119 1
                        ( $node->dim->value
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
120 1
                        , Variable::GLOBAL_TYPE
121 1
                        );
122 1
                    $this->insert_relation_into
123 1
                        ( $insert
124 1
                        , $location
125 1
                        , $name_id
126 1
                        , $node->getAttribute("startLine")
127 1
                        );
128 1
                }
129 10
            });
130 10
    }
131
132 10
    protected function register_error_suppressor_listener(ListenerRegistry $registry) {
133 10
        $registry->on_enter_misc
134 10
            ( array(N\Expr\ErrorSuppress::class)
135 10
            , function(Insert $insert, Location $location, N\Expr\ErrorSuppress $node) {
136 1
                $name_id = $insert->name
137 1
                    ( "@"
138 1
                    , Variable::LANGUAGE_CONSTRUCT_TYPE
139 1
                    );
140 1
                $this->insert_relation_into
141 1
                    ( $insert
142 1
                    , $location
143 1
                    , $name_id
144 1
                    , $node->getAttribute("startLine")
145 1
                    );
146 10
            });
147 10
        }
148
}
149