This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PhpSchool\PSXTest; |
||
4 | |||
5 | use Colors\Color; |
||
6 | use PhpParser\Error; |
||
7 | use PhpParser\Node\Expr; |
||
8 | use PhpParser\Node\Scalar\String_; |
||
9 | use PhpParser\Node\Stmt; |
||
10 | use PhpParser\Parser; |
||
11 | use PhpSchool\PSX\ColorsAdapter; |
||
12 | use PhpSchool\PSX\ColourAdapterInterface; |
||
13 | use PhpSchool\PSX\Lexer; |
||
14 | use PhpSchool\PSX\SyntaxHighlightPrinter; |
||
15 | use PhpSchool\PSX\SyntaxHighlighterConfig; |
||
16 | |||
17 | /** |
||
18 | * Class SyntaxHighlightPrinterTest |
||
19 | * @package PhpSchool\PSXTest |
||
20 | * @author Aydin Hassan <[email protected]> |
||
21 | */ |
||
22 | class SyntaxHighlightPrinterTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @dataProvider provideTestPrettyPrint |
||
26 | * @covers \PhpSchool\PSX\SyntaxHighlightPrinter<extended> |
||
27 | */ |
||
28 | public function testPrettyPrint($name, $code, $expected, $mode) |
||
29 | { |
||
30 | $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode); |
||
31 | } |
||
32 | |||
33 | public function provideTestPrettyPrint() |
||
34 | { |
||
35 | return $this->getTests(__DIR__ . '/code', 'test'); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @dataProvider provideTestPrettyPrintFile |
||
40 | * @covers \PhpSchool\PSX\SyntaxHighlightPrinter<extended> |
||
41 | */ |
||
42 | public function testPrettyPrintFile($name, $code, $expected, $mode) |
||
43 | { |
||
44 | $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode); |
||
45 | } |
||
46 | |||
47 | public function provideTestPrettyPrintFile() |
||
48 | { |
||
49 | return $this->getTests(__DIR__ . '/code', 'file-test'); |
||
50 | } |
||
51 | |||
52 | public function testPrettyPrintExpr() |
||
53 | { |
||
54 | $prettyPrinter = $this->getPrinter(); |
||
55 | $expr = new Expr\BinaryOp\Mul( |
||
56 | new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')), |
||
57 | new Expr\Variable('c') |
||
58 | ); |
||
59 | $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr)); |
||
60 | |||
61 | $expr = new Expr\Closure(array( |
||
62 | 'stmts' => array(new Stmt\Return_(new String_("a\nb"))) |
||
63 | )); |
||
64 | $res = $prettyPrinter->prettyPrintExpr($expr); |
||
65 | $this->assertEquals("[34mfunction[0m () [33m{[0m\n [95mreturn[0m [32m'a\nb'[0m;\n[33m}[0m", $res); |
||
66 | } |
||
67 | |||
68 | protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) |
||
69 | { |
||
70 | $lexer = new Lexer([ |
||
71 | 'usedAttributes' => [ |
||
72 | 'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'startTokenPos', 'endTokenPos' |
||
73 | ] |
||
74 | ]); |
||
75 | |||
76 | $parser5 = new Parser\Php5($lexer); |
||
77 | $parser7 = new Parser\Php7($lexer); |
||
78 | |||
79 | list($version, $options) = $this->parseModeLine($modeLine); |
||
80 | $prettyPrinter = $this->getPrinter($options); |
||
81 | |||
82 | try { |
||
83 | $output5 = $this->canonicalize($prettyPrinter->$method($parser5->parse($code))); |
||
84 | } catch (Error $e) { |
||
85 | $output5 = null; |
||
86 | $this->assertEquals('php7', $version); |
||
87 | } |
||
88 | |||
89 | try { |
||
90 | $output7 = $this->canonicalize($prettyPrinter->$method($parser7->parse($code))); |
||
91 | } catch (Error $e) { |
||
92 | $output7 = null; |
||
93 | $this->assertEquals('php5', $version); |
||
94 | } |
||
95 | |||
96 | if ('php5' === $version) { |
||
97 | $this->assertSame($expected, $output5, $name); |
||
98 | $this->assertNotSame($expected, $output7, $name); |
||
99 | } elseif ('php7' === $version) { |
||
100 | $this->assertSame($expected, $output7, $name); |
||
101 | $this->assertNotSame($expected, $output5, $name); |
||
102 | } else { |
||
103 | $this->assertSame($expected, $output5, $name); |
||
104 | $this->assertSame($expected, $output7, $name); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | protected function getTests($directory, $fileExtension) |
||
109 | { |
||
110 | $it = new \RecursiveDirectoryIterator($directory); |
||
111 | $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY); |
||
112 | $it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)'); |
||
113 | |||
114 | $tests = array(); |
||
115 | foreach ($it as $file) { |
||
116 | $fileName = realpath($file->getPathname()); |
||
117 | $fileContents = file_get_contents($fileName); |
||
118 | |||
119 | // evaluate @@{expr}@@ expressions |
||
0 ignored issues
–
show
|
|||
120 | $fileContents = preg_replace_callback( |
||
121 | '/@@\{(.*?)\}@@/', |
||
122 | function ($matches) { |
||
123 | return eval('return ' . $matches[1] . ';'); |
||
124 | }, |
||
125 | $fileContents |
||
126 | ); |
||
127 | |||
128 | // parse sections |
||
129 | $parts = array_map('trim', explode('-----', $fileContents)); |
||
130 | |||
131 | // first part is the name |
||
132 | $name = array_shift($parts) . ' (' . $fileName . ')'; |
||
133 | $shortName = basename($fileName, '.test'); |
||
134 | |||
135 | // multiple sections possible with always two forming a pair |
||
136 | $chunks = array_chunk($parts, 2); |
||
137 | foreach ($chunks as $i => $chunk) { |
||
138 | $dataSetName = $shortName . (count($chunks) > 1 ? '#' . $i : ''); |
||
139 | list($expected, $mode) = $this->extractMode($this->canonicalize($chunk[1])); |
||
140 | $tests[$dataSetName] = array($name, $chunk[0], $expected, $mode); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $tests; |
||
145 | } |
||
146 | |||
147 | private function extractMode($expected) |
||
148 | { |
||
149 | $firstNewLine = strpos($expected, "\n"); |
||
150 | if (false === $firstNewLine) { |
||
151 | $firstNewLine = strlen($expected); |
||
152 | } |
||
153 | |||
154 | $firstLine = substr($expected, 0, $firstNewLine); |
||
155 | if (0 !== strpos($firstLine, '!!')) { |
||
156 | return [$expected, null]; |
||
157 | } |
||
158 | |||
159 | $expected = (string) substr($expected, $firstNewLine + 1); |
||
160 | return [$expected, substr($firstLine, 2)]; |
||
161 | } |
||
162 | |||
163 | private function parseModeLine($modeLine) |
||
164 | { |
||
165 | $parts = explode(' ', $modeLine, 2); |
||
166 | $version = isset($parts[0]) ? $parts[0] : 'both'; |
||
167 | $options = isset($parts[1]) ? json_decode($parts[1], true) : []; |
||
168 | return [$version, $options]; |
||
169 | } |
||
170 | |||
171 | protected function canonicalize($str) |
||
172 | { |
||
173 | // trim from both sides |
||
174 | $str = trim($str); |
||
175 | |||
176 | // normalize EOL to \n |
||
177 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
||
178 | |||
179 | // trim right side of all lines |
||
180 | return implode("\n", array_map('rtrim', explode("\n", $str))); |
||
181 | } |
||
182 | |||
183 | private function getPrinter(array $options = []) |
||
184 | { |
||
185 | $color = new Color; |
||
186 | $color->setForceStyle(true); |
||
187 | $colourAdapter = new ColorsAdapter($color); |
||
188 | return new SyntaxHighlightPrinter(new SyntaxHighlighterConfig, $colourAdapter, $options); |
||
189 | } |
||
190 | } |
||
191 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.