Completed
Push — master ( 605cf4...2dcac2 )
by Richard
05:16
created

DependOn::enterGlobal()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 13
cp 0.8462
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 3
crap 4.0582
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\ASTVisitor;
15
use Lechimp\Dicto\Indexer\Location;
16
use Lechimp\Dicto\Indexer\Insert;
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 implements ASTVisitor {
25
    /**
26
     * @inheritdoc
27
     */
28 64
    public function name() {
29 64
        return "depend on";
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 11
    public function visitorJumpLabels() {
36
        return
37
            [ N\Expr\MethodCall::class => "enterMethodCall"
38 11
            , N\Expr\FuncCall::class => "enterFunctionCall"
39 11
            , N\Stmt\Global_::class => "enterGlobal"
40 11
            , N\Expr\ArrayDimFetch::class => "enterArrayDimFetch"
41 11
            , N\Expr\ErrorSuppress::class => "enterErrorSuppress"
42 11
            ];
43
    }
44
45 1 View Code Duplication
    public function enterMethodCall(Insert $insert, Location $location, N\Expr\MethodCall $node) {
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...
46
        // The 'name' could also be a variable like in $this->$method();
47 1
        if (is_string($node->name)) {
48 1
            $method_reference = $insert->_method_reference
49 1
                ( $node->name
50 1
                , $location->_file()
51 1
                , $location->_line()
52 1
                , $location->_column()
53 1
                );
54 1
            $this->insert_relation_into
55 1
                ( $insert
56 1
                , $location
57 1
                , $method_reference
58 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...
59 1
                );
60 1
        }
61 1
    }
62
63 2 View Code Duplication
    public function enterFunctionCall(Insert $insert, Location $location, N\Expr\FuncCall $node) {
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...
64
        // Omit calls to closures, we would not be able to
65
        // analyze them anyway atm.
66
        // Omit functions in arrays, we would not be able to
67
        // analyze them anyway atm.
68 2
        if (!($node->name instanceof N\Expr\Variable ||
69 2
              $node->name instanceof N\Expr\ArrayDimFetch)) {
70 2
            $function_reference = $insert->_function_reference
71 2
                ( $node->name->parts[0]
72 2
                , $location->_file()
73 2
                , $location->_line()
74 2
                , $location->_column()
75 2
                );
76 2
            $this->insert_relation_into
77 2
                ( $insert
78 2
                , $location
79 2
                , $function_reference
80 2
                );
81 2
        }
82 2
    }
83
84 6
    public function enterGlobal(Insert $insert, Location $location, N\Stmt\Global_ $node) {
85 6
        foreach ($node->vars as $var) {
86 6
            if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) {
87
                throw new \RuntimeException(
88
                    "Expected Variable with string name, found: ".print_r($var, true));
89
            }
90 6
            $global = $insert->_global($var->name);
91 6
            $this->insert_relation_into
92 6
                ( $insert
93 6
                , $location
94 6
                , $global
95 6
                );
96 6
        }
97 6
    }
98
99 1
    public function enterArrayDimFetch(Insert $insert, Location $location, N\Expr\ArrayDimFetch $node) {
100 1
        if ($node->var instanceof N\Expr\Variable
101 1
        &&  $node->var->name == "GLOBALS"
102
        // Ignore usage of $GLOBALS with variable index.
103 1
        && !($node->dim instanceof N\Expr\Variable)) {
104 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...
105 1
            $this->insert_relation_into
106 1
                ( $insert
107 1
                , $location
108 1
                , $global
109 1
                );
110 1
        }
111 1
    }
112
113 1
    public function enterErrorSuppress(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...
114 1
        $language_construct = $insert->_language_construct("@");
115 1
        $this->insert_relation_into
116 1
            ( $insert
117 1
            , $location
118 1
            , $language_construct
119 1
            );
120 1
    }
121
}
122