Security update for Core, with self-updated composer
[yaffs-website] / vendor / nikic / php-parser / test_old / run.php
1 <?php
2
3 error_reporting(E_ALL | E_STRICT);
4 ini_set('short_open_tag', false);
5
6 if ('cli' !== php_sapi_name()) {
7     die('This script is designed for running on the command line.');
8 }
9
10 function showHelp($error) {
11     die($error . "\n\n" .
12 <<<OUTPUT
13 This script has to be called with the following signature:
14
15     php run.php [--no-progress] testType pathToTestFiles
16
17 The test type must be one of: PHP5, PHP7 or Symfony.
18
19 The following options are available:
20
21     --no-progress    Disables showing which file is currently tested.
22
23 OUTPUT
24     );
25 }
26
27 $options = array();
28 $arguments = array();
29
30 // remove script name from argv
31 array_shift($argv);
32
33 foreach ($argv as $arg) {
34     if ('-' === $arg[0]) {
35         $options[] = $arg;
36     } else {
37         $arguments[] = $arg;
38     }
39 }
40
41 if (count($arguments) !== 2) {
42     showHelp('Too little arguments passed!');
43 }
44
45 $showProgress = true;
46 $verbose = false;
47 foreach ($options as $option) {
48     if ($option === '--no-progress') {
49         $showProgress = false;
50     } elseif ($option === '--verbose') {
51         $verbose = true;
52     } else {
53         showHelp('Invalid option passed!');
54     }
55 }
56
57 $testType = $arguments[0];
58 $dir = $arguments[1];
59
60 switch ($testType) {
61     case 'Symfony':
62         $version = 'Php5';
63         $fileFilter = function($path) {
64             return preg_match('~\.php(?:\.cache)?$~', $path) && false === strpos($path, 'skeleton');
65         };
66         $codeExtractor = function($file, $code) {
67             return $code;
68         };
69         break;
70     case 'PHP5':
71     case 'PHP7':
72     $version = $testType === 'PHP5' ? 'Php5' : 'Php7';
73         $fileFilter = function($path) {
74             return preg_match('~\.phpt$~', $path);
75         };
76         $codeExtractor = function($file, $code) {
77             if (preg_match('~(?:
78 # skeleton files
79   ext.gmp.tests.001
80 | ext.skeleton.tests.001
81 # multibyte encoded files
82 | ext.mbstring.tests.zend_multibyte-01
83 | Zend.tests.multibyte.multibyte_encoding_001
84 | Zend.tests.multibyte.multibyte_encoding_004
85 | Zend.tests.multibyte.multibyte_encoding_005
86 # pretty print difference due to INF vs 1e1000
87 | ext.standard.tests.general_functions.bug27678
88 | tests.lang.bug24640
89 # pretty print differences due to negative LNumbers
90 | Zend.tests.neg_num_string
91 | Zend.tests.bug72918
92 # pretty print difference due to nop statements
93 | ext.mbstring.tests.htmlent
94 | ext.standard.tests.file.fread_basic
95 )\.phpt$~x', $file)) {
96                 return null;
97             }
98
99             if (!preg_match('~--FILE--\s*(.*?)--[A-Z]+--~s', $code, $matches)) {
100                 return null;
101             }
102             if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
103                 return null;
104             }
105
106             return $matches[1];
107         };
108         break;
109     default:
110         showHelp('Test type must be one of: PHP5, PHP7 or Symfony');
111 }
112
113 require_once dirname(__FILE__) . '/../lib/PhpParser/Autoloader.php';
114 PhpParser\Autoloader::register();
115
116 $parserName    = 'PhpParser\Parser\\' . $version;
117 $parser        = new $parserName(new PhpParser\Lexer\Emulative);
118 $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
119 $nodeDumper    = new PhpParser\NodeDumper;
120
121 $parseFail = $ppFail = $compareFail = $count = 0;
122
123 $readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0;
124 $totalStartTime = microtime(true);
125
126 foreach (new RecursiveIteratorIterator(
127              new RecursiveDirectoryIterator($dir),
128              RecursiveIteratorIterator::LEAVES_ONLY)
129          as $file) {
130     if (!$fileFilter($file)) {
131         continue;
132     }
133
134     $startTime = microtime(true);
135     $code = file_get_contents($file);
136     $readTime += microtime(true) - $startTime;
137
138     if (null === $code = $codeExtractor($file, $code)) {
139         continue;
140     }
141
142     set_time_limit(10);
143
144     ++$count;
145
146     if ($showProgress) {
147         echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
148     }
149
150     try {
151         $startTime = microtime(true);
152         $stmts = $parser->parse($code);
153         $parseTime += microtime(true) - $startTime;
154
155         $startTime = microtime(true);
156         $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
157         $ppTime += microtime(true) - $startTime;
158
159         try {
160             $startTime = microtime(true);
161             $ppStmts = $parser->parse($code);
162             $reparseTime += microtime(true) - $startTime;
163
164             $startTime = microtime(true);
165             $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
166             $compareTime += microtime(true) - $startTime;
167
168             if (!$same) {
169                 echo $file, ":\n    Result of initial parse and parse after pretty print differ\n";
170                 if ($verbose) {
171                     echo "Pretty printer output:\n=====\n$code\n=====\n\n";
172                 }
173
174                 ++$compareFail;
175             }
176         } catch (PhpParser\Error $e) {
177             echo $file, ":\n    Parse of pretty print failed with message: {$e->getMessage()}\n";
178             if ($verbose) {
179                 echo "Pretty printer output:\n=====\n$code\n=====\n\n";
180             }
181
182             ++$ppFail;
183         }
184     } catch (PhpParser\Error $e) {
185         echo $file, ":\n    Parse failed with message: {$e->getMessage()}\n";
186
187         ++$parseFail;
188     }
189 }
190
191 if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
192     $exit = 0;
193     echo "\n\n", 'All tests passed.', "\n";
194 } else {
195     $exit = 1;
196     echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
197     if (0 !== $parseFail) {
198         echo '    ', $parseFail,   ' parse failures.',        "\n";
199     }
200     if (0 !== $ppFail) {
201         echo '    ', $ppFail,      ' pretty print failures.', "\n";
202     }
203     if (0 !== $compareFail) {
204         echo '    ', $compareFail, ' compare failures.',      "\n";
205     }
206 }
207
208 echo "\n",
209      'Tested files:         ', $count,        "\n",
210      "\n",
211      'Reading files took:   ', $readTime,    "\n",
212      'Parsing took:         ', $parseTime,   "\n",
213      'Pretty printing took: ', $ppTime,      "\n",
214      'Reparsing took:       ', $reparseTime, "\n",
215      'Comparing took:       ', $compareTime, "\n",
216      "\n",
217      'Total time:           ', microtime(true) - $totalStartTime, "\n",
218      'Maximum memory usage: ', memory_get_peak_usage(true), "\n";
219
220 exit($exit);