Completed
Push — master ( a94ac3...322d21 )
by Richard
05:34
created

DependOn   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 100
Duplicated Lines 36 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 97.53%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 19
c 7
b 0
f 0
lcom 0
cbo 11
dl 36
loc 100
ccs 79
cts 81
cp 0.9753
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
D register_listeners() 36 88 18

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 311
    public function name() {
29 311
        return "depend_on";    
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 22
    public function register_listeners(ListenerRegistry $registry) {
36 22
        $registry->on_enter_misc
37 22
            ( array(N\Expr\MethodCall::class, N\Expr\FuncCall::class,
38 22
                    N\Stmt\Global_::class, N\Expr\ArrayDimFetch::class,
39 22
                    N\Expr\ErrorSuppress::class)
40 22
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
41 20
                $ref_ids = array();
42 20
                if ($node instanceof N\Expr\MethodCall) {
43
                    // The 'name' could also be a variable like in $this->$method();
44 4 View Code Duplication
                    if (is_string($node->name)) {
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...
45 3
                        $ref_ids[] = $insert->get_reference
46 3
                            ( Variable::METHOD_TYPE
47 3
                            , $node->name
48 3
                            , $location->file_path()
49 3
                            , $node->getAttribute("startLine")
50 3
                            );
51 3
                    }
52 4
                }
53 16 View Code Duplication
                elseif($node instanceof N\Expr\FuncCall) {
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...
54
                    // Omit calls to closures, we would not be able to
55
                    // analyze them anyway atm.
56
                    // Omit functions in arrays, we would not be able to
57
                    // analyze them anyway atm.
58 12
                    if (!($node->name instanceof N\Expr\Variable ||
59 12
                          $node->name instanceof N\Expr\ArrayDimFetch)) {
60 10
                        $ref_ids[] = $insert->get_reference
61 10
                            ( Variable::FUNCTION_TYPE
62 10
                            , $node->name->parts[0]
63 10
                            , $location->file_path()
64 10
                            , $node->getAttribute("startLine")
65 10
                            );
66 10
                    }
67 12
                }
68 8
                elseif ($node instanceof N\Stmt\Global_) {
69 2
                    foreach ($node->vars as $var) {
70 2
                        if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) {
71
                            throw new \RuntimeException(
72
                                "Expected Variable with string name, found: ".print_r($var, true));
73
                        }
74 2
                        $ref_ids[] = $insert->get_reference
75 2
                            ( Variable::GLOBAL_TYPE
76 2
                            , $var->name
77 2
                            , $location->file_path()
78 2
                            , $node->getAttribute("startLine")
79 2
                            );
80 2
                    }
81 2
                }
82 8
                elseif ($node instanceof N\Expr\ArrayDimFetch) {
83 5
                    if ($node->var instanceof N\Expr\Variable && $node->var->name == "GLOBALS") {
84
                        // Ignore usage of $GLOBALS with variable index.
85 4
                        if (!($node->dim instanceof N\Expr\Variable)) {
86 3
                            $ref_ids[] = $insert->get_reference
87 3
                                ( Variable::GLOBAL_TYPE
88 3
                                , $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...
89 3
                                , $location->file_path()
90 3
                                , $node->getAttribute("startLine")
91 3
                                );
92 3
                        }
93 4
                    }
94 5
                }
95 3
                elseif ($node instanceof N\Expr\ErrorSuppress) {
96 3
                    $ref_ids[] = $insert->get_reference
97 3
                            ( Variable::LANGUAGE_CONSTRUCT_TYPE
98 3
                            , "@"
99 3
                            , $location->file_path()
100 3
                            , $node->getAttribute("startLine")
101 3
                            );
102 3
                }
103 20
                foreach ($ref_ids as $ref_id) {
104 16
                    $start_line = $node->getAttribute("startLine");
105 16
                    $source_line = $location->file_content($start_line, $start_line);
106
                    // Record a dependency for every entity we currently know as dependent.
107 16 View Code Duplication
                    foreach ($location->in_entities() as $entity) {
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...
108 16
                        if ($entity[0] == Variable::FILE_TYPE) {
109 16
                            continue;
110
                        }
111 16
                        $insert->relation
112 16
                            ( "depend_on"
113 16
                            , $entity[1]
114 16
                            , $ref_id
115 16
                            , $location->file_path()
116 16
                            , $start_line
117 16
                            , $source_line
118 16
                            );
119 16
                    }
120 20
                }
121 22
            });
122 22
        }
123
}
124