Completed
Push — master ( 389cae...29b40e )
by Richard
06:27
created

DependOn   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 37.5 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 97.37%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 19
c 6
b 0
f 0
lcom 0
cbo 11
dl 36
loc 96
ccs 74
cts 76
cp 0.9737
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
D register_listeners() 36 84 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 313
    public function name() {
29 313
        return "depend_on";    
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function register_listeners(ListenerRegistry $registry) {
36 22
        $registry->on_enter_misc(function(Insert $insert, Location $location, \PhpParser\Node $node) {
37 21
            $ref_ids = array();
38 21
            if ($node instanceof N\Expr\MethodCall) {
39
                // The 'name' could also be a variable like in $this->$method();
40 5 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...
41 4
                    $ref_ids[] = $insert->get_reference
42 4
                        ( Variable::METHOD_TYPE
43 4
                        , $node->name
44 4
                        , $location->file_path()
45 4
                        , $node->getAttribute("startLine")
46 4
                        );
47 4
                }
48 5
            }
49 21 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...
50
                // Omit calls to closures, we would not be able to
51
                // analyze them anyway atm.
52
                // Omit functions in arrays, we would not be able to
53
                // analyze them anyway atm.
54 12
                if (!($node->name instanceof N\Expr\Variable ||
55 12
                      $node->name instanceof N\Expr\ArrayDimFetch)) {
56 10
                    $ref_ids[] = $insert->get_reference
57 10
                        ( Variable::FUNCTION_TYPE
58 10
                        , $node->name->parts[0]
59 10
                        , $location->file_path()
60 10
                        , $node->getAttribute("startLine")
61 10
                        );
62 10
                }
63 12
            }
64 21
            elseif ($node instanceof N\Stmt\Global_) {
65 3
                foreach ($node->vars as $var) {
66 3
                    if (!($var instanceof N\Expr\Variable) || !is_string("$var->name")) {
67
                        throw new \RuntimeException(
68
                            "Expected Variable with string name, found: ".print_r($var, true));
69
                    }
70 3
                    $ref_ids[] = $insert->get_reference
71 3
                        ( Variable::GLOBAL_TYPE
72 3
                        , $var->name
0 ignored issues
show
Bug introduced by
It seems like $var->name can also be of type object<PhpParser\Node\Expr>; however, Lechimp\Dicto\Indexer\Insert::get_reference() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73 3
                        , $location->file_path()
74 3
                        , $node->getAttribute("startLine")
75 3
                        );
76 3
                }
77 3
            }
78 21
            elseif ($node instanceof N\Expr\ArrayDimFetch) {
79 6
                if ($node->var instanceof N\Expr\Variable && $node->var->name == "GLOBALS") {
80
                    // Ignore usage of $GLOBALS with variable index.
81 5
                    if (!($node->dim instanceof N\Expr\Variable)) {
82 4
                        $ref_ids[] = $insert->get_reference
83 4
                            ( Variable::GLOBAL_TYPE
84 4
                            , $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...
85 4
                            , $location->file_path()
86 4
                            , $node->getAttribute("startLine")
87 4
                            );
88 4
                    }
89 5
                }
90 6
            }
91 21
            elseif ($node instanceof N\Expr\ErrorSuppress) {
92 4
                $ref_ids[] = $insert->get_reference
93 4
                        ( Variable::LANGUAGE_CONSTRUCT_TYPE
94 4
                        , "@"
95 4
                        , $location->file_path()
96 4
                        , $node->getAttribute("startLine")
97 4
                        );
98 4
            }
99 21
            foreach ($ref_ids as $ref_id) {
100 16
                $start_line = $node->getAttribute("startLine");
101 16
                $source_line = $location->file_content($start_line, $start_line);
102
                // Record a dependency for every entity we currently know as dependent.
103 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...
104 16
                    if ($entity[0] == Variable::FILE_TYPE) {
105 16
                        continue;
106
                    }
107 16
                    $insert->relation
108 16
                        ( "depend_on"
109 16
                        , $entity[1]
110 16
                        , $ref_id
111 16
                        , $location->file_path()
112 16
                        , $start_line
113 16
                        , $source_line
114 16
                        );
115 16
                }
116 21
            }
117 22
        });
118 22
    }
119
}
120