Completed
Push — master ( 9b2a73...93cace )
by Richard
05:06
created

DependOn   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 49.14 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 97.78%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 57
loc 116
ccs 88
cts 90
cp 0.9778
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A register_listeners() 0 7 1
A register_method_call_listener() 21 21 2
B register_func_call_listener() 24 24 3
A register_global_listener() 0 18 4
A register_array_dim_fetch_listener() 0 17 4
A register_error_suppressor_listener() 12 12 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 58
    public function name() {
29 58
        return "depend on";
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 11
    public function register_listeners(ListenerRegistry $registry) {
36 11
        $this->register_method_call_listener($registry);
37 11
        $this->register_func_call_listener($registry);
38 11
        $this->register_global_listener($registry);
39 11
        $this->register_array_dim_fetch_listener($registry);
40 11
        $this->register_error_suppressor_listener($registry);
41 11
    }
42
43 11 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 11
        $registry->on_enter_misc
45 11
            ( [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 1
                if (is_string($node->name)) {
49 1
                    $method_reference = $insert->_method_reference
50 1
                        ( $node->name
51 1
                        , $location->in_entities()[0][1]
52 1
                        , $location->line()
53 1
                        , $location->column()
54 1
                        );
55 1
                    $this->insert_relation_into
56 1
                        ( $insert
57 1
                        , $location
58 1
                        , $method_reference
59 1
                        , $location->line()
0 ignored issues
show
Unused Code introduced by
The call to DependOn::insert_relation_into() has too many arguments starting with $location->line().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60 1
                        );
61 1
                }
62 11
            });
63 11
    }
64
65 11 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...
66 11
        $registry->on_enter_misc
67 11
            ( [N\Expr\FuncCall::class]
68
            , function(Insert $insert, Location $location, N\Expr\FuncCall $node) {
69
                // Omit calls to closures, we would not be able to
70
                // analyze them anyway atm.
71
                // Omit functions in arrays, we would not be able to
72
                // analyze them anyway atm.
73 2
                if (!($node->name instanceof N\Expr\Variable ||
74 2
                      $node->name instanceof N\Expr\ArrayDimFetch)) {
75 2
                    $function_reference = $insert->_function_reference
76 2
                        ( $node->name->parts[0]
77 2
                        , $location->in_entities()[0][1]
78 2
                        , $location->line()
79 2
                        , $location->column()
80 2
                        );
81 2
                    $this->insert_relation_into
82 2
                        ( $insert
83 2
                        , $location
84 2
                        , $function_reference
85 2
                        );
86 2
                }
87 11
            });
88 11
    }
89
90 11
    protected function register_global_listener(ListenerRegistry $registry) {
91 11
        $registry->on_enter_misc
92 11
            ( [N\Stmt\Global_::class]
93
            , function(Insert $insert, Location $location, N\Stmt\Global_ $node) {
94 6
                foreach ($node->vars as $var) {
95 6
                    if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) {
96
                        throw new \RuntimeException(
97
                            "Expected Variable with string name, found: ".print_r($var, true));
98
                    }
99 6
                    $global = $insert->_global($var->name);
100 6
                    $this->insert_relation_into
101 6
                        ( $insert
102 6
                        , $location
103 6
                        , $global
104 6
                        );
105 6
                }
106 11
            });
107 11
    }
108
109 11
    protected function register_array_dim_fetch_listener(ListenerRegistry $registry) {
110 11
        $registry->on_enter_misc
111 11
            ( [N\Expr\ArrayDimFetch::class]
112
            , function(Insert $insert, Location $location, N\Expr\ArrayDimFetch $node) {
113 1
                if ($node->var instanceof N\Expr\Variable 
114 1
                &&  $node->var->name == "GLOBALS"
115
                // Ignore usage of $GLOBALS with variable index.
116 1
                && !($node->dim instanceof N\Expr\Variable)) {
117 1
                    $global = $insert->_global($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...
118 1
                    $this->insert_relation_into
119 1
                        ( $insert
120 1
                        , $location
121 1
                        , $global
122 1
                        );
123 1
                }
124 11
            });
125 11
    }
126
127 11 View Code Duplication
    protected function register_error_suppressor_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...
128 11
        $registry->on_enter_misc
129 11
            ( [N\Expr\ErrorSuppress::class]
130 11
            , function(Insert $insert, Location $location, N\Expr\ErrorSuppress $node) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131 1
                $language_construct = $insert->_language_construct("@");
132 1
                $this->insert_relation_into
133 1
                    ( $insert
134 1
                    , $location
135 1
                    , $language_construct
136 1
                    );
137 11
            });
138 11
        }
139
}
140