Issues (197)

Security Analysis    not enabled

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.

src/CheckClassReferencesAreValid.php (16 issues)

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 Imanghafoori\LaravelMicroscope;
4
5
use Illuminate\Support\Composer;
6
use Illuminate\Support\Str;
7
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson;
8
use Imanghafoori\LaravelMicroscope\Analyzers\FileManipulator;
9
use Imanghafoori\LaravelMicroscope\Analyzers\GetClassProperties;
10
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector;
11
use Imanghafoori\LaravelMicroscope\Analyzers\ParseUseStatement;
12
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
13
14
class CheckClassReferencesAreValid
15
{
16
    public static function check($tokens, $absFilePath)
17
    {
18
        try {
19
            self::checkReferences($tokens, $absFilePath);
20
        } catch (\ErrorException $e) {
0 ignored issues
show
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
21
            // In case a file is moved or deleted,
22
            // composer will need a dump autoload.
23
            if (! Str::endsWith($e->getFile(), 'vendor\composer\ClassLoader.php')) {
24
                throw $e;
25
            }
26
27
            self::warnDumping($e->getMessage());
28
            resolve(Composer::class)->dumpAutoloads();
29
        }
30
    }
31
32
    public static function warnDumping($msg)
33
    {
34
        $p = resolve(ErrorPrinter::class)->printer;
35
        $p->writeln('It seems composer has some trouble with autoload...');
36
        $p->writeln($msg);
37
        $p->writeln('Running "composer dump-autoload" command...  \(*_*)\  ');
38
    }
39
40
    private static function checkReferences($tokens, $absFilePath)
41
    {
42
        // If file is empty or does not begin with <?php
43
        if (($tokens[0][0] ?? null) !== T_OPEN_TAG) {
44
            return;
45
        }
46
47
        [
48
            $currentNamespace,
0 ignored issues
show
The variable $currentNamespace does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
            $class,
0 ignored issues
show
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
            $type,
0 ignored issues
show
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
51
            $parent,
0 ignored issues
show
The variable $parent does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
52
            $interfaces,
0 ignored issues
show
The variable $interfaces does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
53
        ] = GetClassProperties::readClassDefinition($tokens);
54
55
        // It means that, there is no class/trait definition found in the file.
56
        if (! $class) {
57
            return;
58
        }
59
60
        event('laravel_microscope.checking_file', [$absFilePath]);
61
        // @todo better to do it an event listener.
62
63
        self::checkAtSignStrings($tokens, $absFilePath);
64
65
        self::checkNotImportedClasses($tokens, $absFilePath);
66
67
        self::checkImportedClasses($currentNamespace, $class, $absFilePath);
68
    }
69
70
    private static function checkImportedClassesExist($imports, $absFilePath)
71
    {
72
        $printer = app(ErrorPrinter::class);
73
74
        foreach ($imports as $i => $import) {
75
            [$class, $line] = $import;
0 ignored issues
show
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $line does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
77
            if (! self::isAbsent($class)) {
78
                continue;
79
            }
80
81
            if (\is_dir(base_path(NamespaceCorrector::getRelativePathFromNamespace($class)))) {
82
                continue;
83
            }
84
85
            $isInUserSpace = Str::startsWith($class, array_keys(ComposerJson::readAutoload()));
86
87
            [$isCorrected, $corrects] = FileManipulator::fixReference($absFilePath, $class, $line, '', true);
0 ignored issues
show
The variable $isCorrected does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $corrects does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
88
89
            if ($isInUserSpace && $isCorrected) {
90
                $printer->printFixation($absFilePath, $class, $line, $corrects);
91
            } else {
92
                $printer->wrongImport($absFilePath, $class, $line);
93
            }
94
        }
95
    }
96
97
    public static function isAbsent($class)
98
    {
99
        return ! \class_exists($class) && ! \interface_exists($class) && ! \trait_exists($class);
100
    }
101
102
    protected static function fullNamespace($currentNamespace, $class)
103
    {
104
        return $currentNamespace ? $currentNamespace.'\\'.$class : $class;
105
    }
106
107
    public static function checkAtSignStrings($tokens, $absFilePath, $onlyAbsClassPath = false)
108
    {
109
        $printer = app(ErrorPrinter::class);
110
111
        foreach ($tokens as $token) {
112
            // If it is a string containing a single '@'
113
            if ($token[0] != T_CONSTANT_ENCAPSED_STRING || \substr_count($token[1], '@') != 1) {
114
                continue;
115
            }
116
117
            $trimmed = \trim($token[1], '\'\"');
118
119
            if ($onlyAbsClassPath && $trimmed[0] !== '\\') {
120
                continue;
121
            }
122
123
            [$class, $method] = \explode('@', $trimmed);
0 ignored issues
show
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
124
125
            if (\substr_count($class, '\\') <= 0) {
126
                continue;
127
            }
128
129
            if (Str::contains($trimmed, ['-', '/', '[', '*', '+', '.', '(', '$', '^'])) {
130
                continue;
131
            }
132
133
            if (! \class_exists($class)) {
134
                $isInUserSpace = Str::startsWith($class, \array_keys(ComposerJson::readAutoload()));
135
                $result = FileManipulator::fixReference($absFilePath, $class, $token[2]);
136
                if ($isInUserSpace && $result[0]) {
137
                    $printer->printFixation($absFilePath, $class, $token[2], $result[1]);
138
                } else {
139
                    $printer->wrongUsedClassError($absFilePath, $token[1], $token[2]);
140
                }
141
            } elseif (! \method_exists($class, $method)) {
142
                $printer->wrongMethodError($absFilePath, $trimmed, $token[2]);
143
            }
144
        }
145
    }
146
147
    private static function checkImportedClasses($currentNamespace, $class, $absPath)
148
    {
149
        $namespacedClassName = self::fullNamespace($currentNamespace, $class);
150
151
        $imports = ParseUseStatement::getUseStatementsByPath($namespacedClassName, $absPath);
152
153
        self::checkImportedClassesExist($imports, $absPath);
154
    }
155
156
    private static function fix($absFilePath, $class, $line, $namespace)
157
    {
158
        $baseClassName = Str::replaceFirst($namespace.'\\', '', $class);
159
160
        $result = FileManipulator::fixReference($absFilePath, $baseClassName, $line, '\\');
161
162
        if ($result[0]) {
163
            return $result;
164
        }
165
166
        return FileManipulator::fixReference($absFilePath, $class, $line);
167
    }
168
169
    private static function checkNotImportedClasses($tokens, $absFilePath)
170
    {
171
        [$nonImportedClasses, $namespace] = ParseUseStatement::findClassReferences($tokens, $absFilePath);
0 ignored issues
show
The variable $namespace does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $nonImportedClasses does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
172
173
        $printer = app(ErrorPrinter::class);
174
175
        loopStart:
176
        foreach ($nonImportedClasses as $nonImportedClass) {
177
            $class = $nonImportedClass['class'];
178
            $line = $nonImportedClass['line'];
179
180
            if (! self::isAbsent($class) || \function_exists($class)) {
181
                continue;
182
            }
183
184
            if (! ComposerJson::isInAppSpace($class)) {
185
                $printer->doesNotExist($class, $absFilePath, $line, 'wrongReference', 'Class does not exist:');
186
                continue;
187
            }
188
189
            [$isFixed, $corrections] = self::fix($absFilePath, $class, $line, $namespace);
0 ignored issues
show
The variable $isFixed does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $corrections does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
190
191
            $method = $isFixed ? 'printFixation' : 'wrongImportPossibleFixes';
192
            $printer->$method($absFilePath, $class, $line, $corrections);
193
            if ($isFixed) {
194
                $tokens = token_get_all(file_get_contents($absFilePath));
195
                ([$nonImportedClasses, $namespace] = ParseUseStatement::findClassReferences($tokens, $absFilePath));
196
                goto loopStart;
197
            }
198
        }
199
    }
200
}
201