|
14 | 14 | * @link http://pear.php.net/package/PHP_CodeSniffer
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +use PHP_CodeSniffer\Config; |
| 18 | +use PHP_CodeSniffer\Exceptions\RuntimeException; |
| 19 | +use PHP_CodeSniffer\Exceptions\TokenizerException; |
| 20 | +use PHP_CodeSniffer\Tokenizers\PHP; |
| 21 | +use PHP_CodeSniffer\Util\Tokens; |
| 22 | + |
17 | 23 | error_reporting(E_ALL | E_STRICT);
|
18 | 24 |
|
19 | 25 | if (ini_get('phar.readonly') === '1') {
|
20 | 26 | echo 'Unable to build, phar.readonly in php.ini is set to read only.'.PHP_EOL;
|
21 | 27 | exit(1);
|
22 | 28 | }
|
23 | 29 |
|
| 30 | +require_once dirname(__DIR__).'/autoload.php'; |
| 31 | +require_once dirname(__DIR__).'/src/Util/Tokens.php'; |
| 32 | + |
| 33 | +if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
| 34 | + define('PHP_CODESNIFFER_VERBOSITY', 0); |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Replacement for the PHP native php_strip_whitespace() function, |
| 40 | + * which doesn't handle attributes correctly for cross-version PHP. |
| 41 | + * |
| 42 | + * @param string $fullpath Path to file. |
| 43 | + * @param \PHP_CodeSniffer\Config $config Perfunctory Config. |
| 44 | + * |
| 45 | + * @return string |
| 46 | + * |
| 47 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException When tokenizer errors are encountered. |
| 48 | + */ |
| 49 | +function stripWhitespaceAndComments($fullpath, $config) |
| 50 | +{ |
| 51 | + $contents = file_get_contents($fullpath); |
| 52 | + |
| 53 | + try { |
| 54 | + $tokenizer = new PHP($contents, $config, "\n"); |
| 55 | + $tokens = $tokenizer->getTokens(); |
| 56 | + } catch (TokenizerException $e) { |
| 57 | + throw new RuntimeException('Failed to tokenize file '.$fullpath); |
| 58 | + } |
| 59 | + |
| 60 | + $stripped = ''; |
| 61 | + foreach ($tokens as $token) { |
| 62 | + if ($token['code'] === T_ATTRIBUTE_END || $token['code'] === T_OPEN_TAG) { |
| 63 | + $stripped .= $token['content']."\n"; |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + if (isset(Tokens::$emptyTokens[$token['code']]) === false) { |
| 68 | + $stripped .= $token['content']; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + if ($token['code'] === T_WHITESPACE) { |
| 73 | + $stripped .= ' '; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return $stripped; |
| 78 | + |
| 79 | +}//end stripWhitespaceAndComments() |
| 80 | + |
| 81 | + |
24 | 82 | $startTime = microtime(true);
|
25 | 83 |
|
26 | 84 | $scripts = [
|
|
53 | 111 | $rdi = new \RecursiveDirectoryIterator($srcDir, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
|
54 | 112 | $di = new \RecursiveIteratorIterator($rdi, 0, \RecursiveIteratorIterator::CATCH_GET_CHILD);
|
55 | 113 |
|
| 114 | + $config = new Config(); |
56 | 115 | $fileCount = 0;
|
| 116 | + |
57 | 117 | foreach ($di as $file) {
|
58 | 118 | $filename = $file->getFilename();
|
59 | 119 |
|
|
68 | 128 | }
|
69 | 129 |
|
70 | 130 | $path = 'src'.substr($fullpath, $srcDirLen);
|
71 |
| - $phar->addFile($fullpath, $path); |
| 131 | + |
| 132 | + if (substr($filename, -4) === '.xml') { |
| 133 | + $phar->addFile($fullpath, $path); |
| 134 | + } else { |
| 135 | + // PHP file. |
| 136 | + $phar->addFromString($path, stripWhitespaceAndComments($fullpath, $config)); |
| 137 | + } |
72 | 138 |
|
73 | 139 | ++$fileCount;
|
74 | 140 | }//end foreach
|
75 | 141 |
|
76 | 142 | // Add autoloader.
|
77 |
| - $phar->addFile(realpath(__DIR__.'/../autoload.php'), 'autoload.php'); |
| 143 | + $phar->addFromString('autoload.php', stripWhitespaceAndComments(realpath(__DIR__.'/../autoload.php'), $config)); |
78 | 144 |
|
79 | 145 | // Add licence file.
|
80 | 146 | $phar->addFile(realpath(__DIR__.'/../licence.txt'), 'licence.txt');
|
|
0 commit comments