Completed
Push — master ( 4e85fc...8a5d17 )
by personal
07:51
created

Tokenizer::cleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Token;
11
12
/**
13
 * Tokenize file
14
 *
15
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
16
 */
17
class Tokenizer {
18
19
    /**
20
     * Tokenize file
21
     *
22
     * @param $filename
23
     * @return TokenCollection
24
     */
25
    public function tokenize($filename) {
26
        //
27
        // fixes memory problems with large files
28
        // https://github.com/Halleck45/PhpMetrics/issues/13
29
        $size = filesize($filename);
30
        $limit = 102400; // around 100 Ko
31
        if($size > $limit) {
32
            $tokens = array();
33
            $hwnd = fopen($filename, 'r');
34
            while (!feof($hwnd)) {
35
                $content = stream_get_line($hwnd, $limit);
36
                // string is arbitrary splitted, so content can be incorrect
37
                // for example: "Unterminated comment starting..."
38
                $content .= '/* */';
39
                $tokens = array_merge($tokens, token_get_all($this->cleanup($content)));
40
                unset($content);
41
            }
42
            return new TokenCollection($tokens);
43
        }
44
45
        return new TokenCollection(token_get_all($this->cleanup(file_get_contents($filename))));
46
    }
47
48
    /**
49
     * Clean php source
50
     *
51
     * @param $content
52
     * @return string
53
     */
54
    private function cleanup($content) {
55
        // replacing short open tags by <?php
56
        // if file contains short open tags but short_open_tags='Off' in php.ini bug can occur
57
        // @see https://github.com/Halleck45/PhpMetrics/issues/154
58
        return preg_replace('!(<\?\s)!', '<?php ', $content);
59
    }
60
61
}