Completed
Push — master ( 51a843...7b3f43 )
by Richard
05:32
created

DependOn   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 33.85 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 98.08%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
c 11
b 0
f 0
dl 44
loc 130
wmc 16
lcom 1
cbo 6
ccs 102
cts 104
cp 0.9808
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() 20 20 2
B register_func_call_listener() 24 24 3
B register_global_listener() 0 23 4
B register_array_dim_fetch_listener() 0 22 4
A register_error_suppressor_listener() 0 17 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 licence 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 328
    public function name() {
29 328
        return "depend_on";    
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 23
    public function register_listeners(ListenerRegistry $registry) {
36 23
        $this->register_method_call_listener($registry);
37 23
        $this->register_func_call_listener($registry);
38 23
        $this->register_global_listener($registry);
39 23
        $this->register_array_dim_fetch_listener($registry);
40 23
        $this->register_error_suppressor_listener($registry);
41 23
    }
42
43 23 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 23
        $registry->on_enter_misc
45 23
            ( array(N\Expr\MethodCall::class)
46
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
47
                // The 'name' could also be a variable like in $this->$method();
48 4
                if (is_string($node->name)) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49 3
                    $ref_id = $insert->get_reference
50 3
                        ( Variable::METHOD_TYPE
51 3
                        , $node->name
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
52 3
                        , $location->file_path()
53 3
                        , $node->getAttribute("startLine")
54 3
                        );
55 3
                    $this->insert_relation_into
56 3
                        ( $insert
57 3
                        , $location
58 3
                        , $ref_id
59 3
                        );
60 3
                }
61 23
            });
62 23
    }
63
64 23 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...
65 23
        $registry->on_enter_misc
66 23
            ( array(N\Expr\FuncCall::class)
67
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
68
                // Omit calls to closures, we would not be able to
69
                // analyze them anyway atm.
70
                // Omit functions in arrays, we would not be able to
71
                // analyze them anyway atm.
72 13
                if (!($node->name instanceof N\Expr\Variable ||
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73 13
                      $node->name instanceof N\Expr\ArrayDimFetch)) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74 11
                    $ref_id = $insert->get_reference
75 11
                        ( Variable::FUNCTION_TYPE
76 11
                        , $node->name->parts[0]
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
77 11
                        , $location->file_path()
78 11
                        , $node->getAttribute("startLine")
79 11
                        );
80 11
                    $this->insert_relation_into
81 11
                        ( $insert
82 11
                        , $location
83 11
                        , $ref_id
84 11
                        );
85 11
                }
86 23
            });
87 23
    }
88
89 23
    protected function register_global_listener(ListenerRegistry $registry) {
90 23
        $registry->on_enter_misc
91 23
            ( array(N\Stmt\Global_::class)
92
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
93 2
                foreach ($node->vars as $var) {
0 ignored issues
show
Bug introduced by
Accessing vars on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
94 2
                    if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) {
95
                        throw new \RuntimeException(
96
                            "Expected Variable with string name, found: ".print_r($var, true));
97
                    }
98 2
                    $ref_id = $insert->get_reference
99 2
                        ( Variable::GLOBAL_TYPE
100 2
                        , $var->name
101 2
                        , $location->file_path()
102 2
                        , $node->getAttribute("startLine")
103 2
                        );
104 2
                $this->insert_relation_into
105 2
                    ( $insert
106 2
                    , $location
107 2
                    , $ref_id
108 2
                    );
109 2
                }
110 23
            });
111 23
    }
112
113 23
    protected function register_array_dim_fetch_listener(ListenerRegistry $registry) {
114 23
        $registry->on_enter_misc
115 23
            ( array(N\Expr\ArrayDimFetch::class)
116
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
117 5
                if ($node->var instanceof N\Expr\Variable 
0 ignored issues
show
Bug introduced by
Accessing var on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
118 5
                &&  $node->var->name == "GLOBALS"
0 ignored issues
show
Bug introduced by
Accessing var on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
119
                // Ignore usage of $GLOBALS with variable index.
120 5
                && !($node->dim instanceof N\Expr\Variable)) {
0 ignored issues
show
Bug introduced by
Accessing dim on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
121 3
                    $ref_id = $insert->get_reference
122 3
                        ( Variable::GLOBAL_TYPE
123 3
                        , $node->dim->value
0 ignored issues
show
Bug introduced by
Accessing dim on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
124 3
                        , $location->file_path()
125 3
                        , $node->getAttribute("startLine")
126 3
                        );
127 3
                    $this->insert_relation_into
128 3
                        ( $insert
129 3
                        , $location
130 3
                        , $ref_id
131 3
                        );
132 3
                }
133 23
            });
134 23
    }
135
136 23
    protected function register_error_suppressor_listener(ListenerRegistry $registry) {
137 23
        $registry->on_enter_misc
138 23
            ( array(N\Expr\ErrorSuppress::class)
139 23
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
140 3
                $ref_id = $insert->get_reference
141 3
                    ( Variable::LANGUAGE_CONSTRUCT_TYPE
142 3
                    , "@"
143 3
                    , $location->file_path()
144 3
                    , $node->getAttribute("startLine")
145 3
                    );
146 3
                $this->insert_relation_into
147 3
                    ( $insert
148 3
                    , $location
149 3
                    , $ref_id
150 3
                    );
151 23
            });
152 23
        }
153
}
154