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.
Completed
Push — master ( 072bc6...f2b09a )
by Maurice
02:08
created

Obfuscator::obfuscateFileContents()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 3
eloc 17
nc 7
nop 2
1
<?php
2
/**
3
 * Obfuscator.php
4
 *
5
 * @package         Obfuscator
6
 * @subpackage      Obfuscator
7
 */
8
9
namespace Naneau\Obfuscator;
10
11
use Naneau\Obfuscator\Obfuscator\Event\File as FileEvent;
12
use Naneau\Obfuscator\Obfuscator\Event\FileError as FileErrorEvent;
13
14
use PhpParser\NodeTraverserInterface as NodeTraverser;
15
16
use PhpParser\Parser;
17
use PhpParser\Lexer;
18
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
19
20
use Symfony\Component\EventDispatcher\EventDispatcher;
21
22
use \RegexIterator;
23
use \RecursiveDirectoryIterator;
24
use \RecursiveIteratorIterator;
25
use \SplFileInfo;
26
27
use \Exception;
28
29
/**
30
 * Obfuscator
31
 *
32
 * Obfuscates a directory of files
33
 *
34
 * @category        Naneau
35
 * @package         Obfuscator
36
 * @subpackage      Obfuscator
37
 */
38
class Obfuscator
39
{
40
    /**
41
     * the parser
42
     *
43
     * @var Parser
44
     */
45
    private $parser;
46
47
    /**
48
     * the node traverser
49
     *
50
     * @var NodeTraverser
51
     */
52
    private $traverser;
53
54
    /**
55
     * the "pretty" printer
56
     *
57
     * @var PrettyPrinter
58
     */
59
    private $prettyPrinter;
60
61
    /**
62
     * the event dispatcher
63
     *
64
     * @var EventDispatcher
65
     */
66
    private $eventDispatcher;
67
68
    /**
69
     * The file regex
70
     *
71
     * @var string
72
     **/
73
    private $fileRegex = '/\.php$/';
74
75
    /**
76
     * Strip whitespace
77
     *
78
     * @param  string $directory
79
     * @param  bool   $stripWhitespace
80
     * @return void
81
     **/
82
    public function obfuscate($directory, $stripWhitespace = false,
83
        $ignoreError = false)
84
    {
85
        foreach ($this->getFiles($directory) as $file) {
86
            $this->getEventDispatcher()->dispatch(
87
                'obfuscator.file',
88
                new FileEvent($file)
89
            );
90
91
            // Write obfuscated source
92
            file_put_contents($file, $this->obfuscateFileContents($file,
93
                $ignoreError));
94
95
            // Strip whitespace if required
96
            if ($stripWhitespace) {
97
                file_put_contents($file, php_strip_whitespace($file));
98
            }
99
        }
100
    }
101
102
    /**
103
     * Get the parser
104
     *
105
     * @return Parser
106
     */
107
    public function getParser()
108
    {
109
        return $this->parser;
110
    }
111
112
    /**
113
     * Set the parser
114
     *
115
     * @param  Parser     $parser
116
     * @return Obfuscator
117
     */
118
    public function setParser(Parser $parser)
119
    {
120
        $this->parser = $parser;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Get the node traverser
127
     *
128
     * @return NodeTraverser
129
     */
130
    public function getTraverser()
131
    {
132
        return $this->traverser;
133
    }
134
135
    /**
136
     * Set the node traverser
137
     *
138
     * @param  NodeTraverser $traverser
139
     * @return Obfuscator
140
     */
141
    public function setTraverser(NodeTraverser $traverser)
142
    {
143
        $this->traverser = $traverser;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Get the "pretty" printer
150
     *
151
     * @return PrettyPrinter
152
     */
153
    public function getPrettyPrinter()
154
    {
155
        return $this->prettyPrinter;
156
    }
157
158
    /**
159
     * Set the "pretty" printer
160
     *
161
     * @param  PrettyPrinter $prettyPrinter
162
     * @return Obfuscator
163
     */
164
    public function setPrettyPrinter(PrettyPrinter $prettyPrinter)
165
    {
166
        $this->prettyPrinter = $prettyPrinter;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Get the event dispatcher
173
     *
174
     * @return EventDispatcher
175
     */
176
    public function getEventDispatcher()
177
    {
178
        return $this->eventDispatcher;
179
    }
180
181
    /**
182
     * Set the event dispatcher
183
     *
184
     * @param EventDispatcher $eventDispatcher
185
     * @return Obfuscator
186
     */
187
    public function setEventDispatcher(EventDispatcher $eventDispatcher)
188
    {
189
        $this->eventDispatcher = $eventDispatcher;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Get the regex for file inclusion
196
     *
197
     * @return string
198
     */
199
    public function getFileRegex()
200
    {
201
        return $this->fileRegex;
202
    }
203
204
    /**
205
     * Set the regex for file inclusion
206
     *
207
     * @param string $fileRegex
208
     * @return Obfuscator
209
     */
210
    public function setFileRegex($fileRegex)
211
    {
212
        $this->fileRegex = $fileRegex;
213
214
        return $this;
215
    }
216
217
    /**
218
     * Get the file list
219
     *
220
     * @return SplFileInfo
221
     **/
222
    private function getFiles($directory)
223
    {
224
        return new RegexIterator(
225
            new RecursiveIteratorIterator(
226
                new RecursiveDirectoryIterator($directory)
227
            ),
228
            $this->getFileRegex()
229
        );
230
    }
231
232
    /**
233
     * Obfuscate a single file's contents
234
     *
235
     * @param  string $file
236
     * @param  boolean $ignoreError if true, do not throw an Error and 
237
     *                              exit, but continue with next file
238
     * @return string obfuscated contents
239
     **/
240
    private function obfuscateFileContents($file, $ignoreError)
241
    {
242
        try {
243
            // Input code
244
            $source = php_strip_whitespace($file);
245
246
            // Get AST
247
            $ast = $this->getTraverser()->traverse(
248
                $this->getParser()->parse($source)
0 ignored issues
show
Bug introduced by
It seems like $this->getParser()->parse($source) targeting PhpParser\ParserAbstract::parse() can also be of type null; however, PhpParser\NodeTraverserInterface::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
249
            );
250
251
            return "<?php\n" . $this->getPrettyPrinter()->prettyPrint($ast);
252
        } catch (Exception $e) {
253
            if($ignoreError) {
254
                sprintf('Could not parse file "%s"', $file);
255
                $this->getEventDispatcher()->dispatch(
256
                    'obfuscator.file.error',
257
                    new FileErrorEvent($file, $e->getMessage())
258
                );
259
            } else {
260
                throw new Exception(
261
                    sprintf('Could not parse file "%s"', $file),
262
                    null,
263
                    $e
264
                );
265
            }
266
        }
267
    }
268
}
269