Issues (22)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

test/SyntaxHighlightPrinterTest.php (1 issue)

Upgrade to new PHP Analysis Engine

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("function () {\n    return 'a\nb';\n}", $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
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
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