1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ScramblePrivateProperty.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\Property; |
22
|
|
|
|
23
|
|
|
use PhpParser\Node\Expr\PropertyFetch; |
24
|
|
|
|
25
|
|
|
use PhpParser\Node\Expr\Variable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ScramblePrivateProperty |
29
|
|
|
* |
30
|
|
|
* Renames private properties |
31
|
|
|
* |
32
|
|
|
* WARNING |
33
|
|
|
* |
34
|
|
|
* See warning for private method scrambler |
35
|
|
|
* |
36
|
|
|
* @category Naneau |
37
|
|
|
* @package Obfuscator |
38
|
|
|
* @subpackage NodeVisitor |
39
|
|
|
*/ |
40
|
|
|
class ScramblePrivateProperty extends ScramblerVisitor |
41
|
|
|
{ |
42
|
|
|
use TrackingRenamerTrait; |
43
|
|
|
use SkipTrait; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param StringScrambler $scrambler |
49
|
|
|
* @return void |
|
|
|
|
50
|
|
|
**/ |
51
|
|
|
public function __construct(StringScrambler $scrambler) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($scrambler); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Before node traversal |
58
|
|
|
* |
59
|
|
|
* @param Node[] $nodes |
60
|
|
|
* @return array |
61
|
|
|
**/ |
62
|
|
|
public function beforeTraverse(array $nodes) |
63
|
|
|
{ |
64
|
|
|
$this |
65
|
|
|
->resetRenamed() |
66
|
|
|
->scanPropertyDefinitions($nodes); |
67
|
|
|
|
68
|
|
|
return $nodes; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check all variable nodes |
73
|
|
|
* |
74
|
|
|
* @param Node $node |
75
|
|
|
* @return void |
76
|
|
|
**/ |
77
|
|
|
public function enterNode(Node $node) |
78
|
|
|
{ |
79
|
|
|
if ($node instanceof PropertyFetch) { |
80
|
|
|
|
81
|
|
|
if (!is_string($node->name)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($this->isRenamed($node->name)) { |
86
|
|
|
$node->name = $this->getNewName($node->name); |
87
|
|
|
return $node; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Recursively scan for private method definitions and rename them |
94
|
|
|
* |
95
|
|
|
* @param Node[] $nodes |
96
|
|
|
* @return void |
97
|
|
|
**/ |
98
|
|
View Code Duplication |
private function scanPropertyDefinitions(array $nodes) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
foreach ($nodes as $node) { |
101
|
|
|
// Scramble the private method definitions |
102
|
|
|
if ($node instanceof Property && ($node->type & ClassNode::MODIFIER_PRIVATE)) { |
103
|
|
|
foreach($node->props as $property) { |
104
|
|
|
|
105
|
|
|
// Record original name and scramble it |
106
|
|
|
$originalName = $property->name; |
107
|
|
|
$this->scramble($property); |
108
|
|
|
|
109
|
|
|
// Record renaming |
110
|
|
|
$this->renamed($originalName, $property->name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Recurse over child nodes |
116
|
|
|
if (isset($node->stmts) && is_array($node->stmts)) { |
|
|
|
|
117
|
|
|
$this->scanPropertyDefinitions($node->stmts); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.