GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ScramblePrivateMethod::enterNode()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 7
nc 4
nop 1
1
<?php
2
/**
3
 * ScramblePrivateMethod.php
4
 *
5
 * @category        Naneau
6
 * @package         Obfuscator
7
 * @subpackage      NodeVisitor
8
 */
9
10
namespace Naneau\Obfuscator\Node\Visitor;
11
12
use Naneau\Obfuscator\Node\Visitor\TrackingRenamerTrait;
13
use Naneau\Obfuscator\Node\Visitor\SkipTrait;
14
15
use Naneau\Obfuscator\Node\Visitor\Scrambler as ScramblerVisitor;
16
use Naneau\Obfuscator\StringScrambler;
17
18
use PhpParser\Node;
19
20
use PhpParser\Node\Stmt\Class_ as ClassNode;
21
use PhpParser\Node\Stmt\ClassMethod;
22
use PhpParser\Node\Expr\MethodCall;
23
use PhpParser\Node\Expr\StaticCall;
24
use PhpParser\Node\Expr\Variable;
25
26
/**
27
 * ScramblePrivateMethod
28
 *
29
 * Renames private methods
30
 *
31
 * WARNING
32
 *
33
 * This method is not foolproof. This visitor scans for all private method
34
 * declarations and renames them. It then finds *all* method calls in the
35
 * class, and renames them if they match the name of a renamed method. If your
36
 * class calls a method of *another* class that happens to match one of the
37
 * renamed private methods, this visitor will rename it.
38
 *
39
 * @category        Naneau
40
 * @package         Obfuscator
41
 * @subpackage      NodeVisitor
42
 */
43
class ScramblePrivateMethod extends ScramblerVisitor
44
{
45
    use TrackingRenamerTrait;
46
    use SkipTrait;
47
48
    /**
49
     * Before node traversal
50
     *
51
     * @param  Node[] $nodes
52
     * @return array
53
     **/
54
    public function beforeTraverse(array $nodes)
55
    {
56
        $this
57
            ->resetRenamed()
58
            ->skip($this->variableMethodCallsUsed($nodes));
59
60
        $this->scanMethodDefinitions($nodes);
61
62
        return $nodes;
63
    }
64
65
    /**
66
     * Check all variable nodes
67
     *
68
     * @param  Node $node
69
     * @return void
70
     **/
71
    public function enterNode(Node $node)
72
    {
73
        if ($this->shouldSkip()) {
74
            return;
75
        }
76
77
        // Scramble calls
78
        if ($node instanceof MethodCall || $node instanceof StaticCall) {
79
80
            // Node wasn't renamed
81
            if (!$this->isRenamed($node->name)) {
82
                return;
83
            }
84
85
            // Scramble usage
86
            return $this->scramble($node);
87
        }
88
    }
89
90
    /**
91
     * Recursively scan for method calls and see if variables are used
92
     *
93
     * @param  Node[] $nodes
94
     * @return void
95
     **/
96
    private function variableMethodCallsUsed(array $nodes)
97
    {
98
        foreach ($nodes as $node) {
99
            if ($node instanceof MethodCall && $node->name instanceof Variable) {
100
                // A method call uses a Variable as its name
101
                return true;
102
            }
103
104
            // Recurse over child nodes
105
            if (isset($node->stmts) && is_array($node->stmts)) {
0 ignored issues
show
Bug introduced by
Accessing stmts 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...
106
                $used = $this->variableMethodCallsUsed($node->stmts);
0 ignored issues
show
Bug introduced by
Accessing stmts 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...
107
108
                if ($used) {
109
                    return true;
110
                }
111
            }
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * Recursively scan for private method definitions and rename them
119
     *
120
     * @param  Node[] $nodes
121
     * @return void
122
     **/
123 View Code Duplication
    private function scanMethodDefinitions(array $nodes)
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...
124
    {
125
        foreach ($nodes as $node) {
126
            // Scramble the private method definitions
127
            if ($node instanceof ClassMethod && ($node->type & ClassNode::MODIFIER_PRIVATE)) {
128
129
                // Record original name and scramble it
130
                $originalName = $node->name;
131
                $this->scramble($node);
132
133
                // Record renaming
134
                $this->renamed($originalName, $node->name);
135
            }
136
137
            // Recurse over child nodes
138
            if (isset($node->stmts) && is_array($node->stmts)) {
0 ignored issues
show
Bug introduced by
Accessing stmts 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...
139
                $this->scanMethodDefinitions($node->stmts);
0 ignored issues
show
Bug introduced by
Accessing stmts 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...
140
            }
141
        }
142
    }
143
}
144