Completed
Push — master ( 1a5eaf...51a843 )
by Richard
05:35
created

DependOn   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 89
Duplicated Lines 25.84 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 97.22%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 17
c 10
b 0
f 0
lcom 0
cbo 10
dl 23
loc 89
ccs 70
cts 72
cp 0.9722
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
C register_listeners() 23 77 16

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
        $registry->on_enter_misc
37 23
            ( array(N\Expr\MethodCall::class, N\Expr\FuncCall::class,
38 23
                    N\Stmt\Global_::class, N\Expr\ArrayDimFetch::class,
39 23
                    N\Expr\ErrorSuppress::class)
40 23
            , function(Insert $insert, Location $location, \PhpParser\Node $node) {
41 21
                $ref_ids = array();
42 21
                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 17 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 13
                    if (!($node->name instanceof N\Expr\Variable ||
59 13
                          $node->name instanceof N\Expr\ArrayDimFetch)) {
60 11
                        $ref_ids[] = $insert->get_reference
61 11
                            ( Variable::FUNCTION_TYPE
62 11
                            , $node->name->parts[0]
63 11
                            , $location->file_path()
64 11
                            , $node->getAttribute("startLine")
65 11
                            );
66 11
                    }
67 13
                }
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 21
                foreach ($ref_ids as $ref_id) {
104 17
                    $this->insert_relation_into
105 17
                        ( $insert
106 17
                        , $location
107 17
                        , $ref_id
108 17
                        );
109 21
                }
110 23
            });
111 23
        }
112
}
113