Scanners::processFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Scabbia2 Scanners Component
4
 * https://github.com/eserozvataf/scabbia2
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @link        https://github.com/eserozvataf/scabbia2-scanners for the canonical source repository
10
 * @copyright   2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/)
11
 * @license     http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0
12
 */
13
14
namespace Scabbia\Scanners;
15
16
use Scabbia\Helpers\FileSystem;
17
// use Scabbia\Scanners\ScannerInterface;
18
use LogicException;
19
use ReflectionClass;
20
21
/**
22
 * Scanners registry
23
 *
24
 * @package     Scabbia\Scanners
25
 * @author      Eser Ozvataf <[email protected]>
26
 * @since       2.0.0
27
 */
28
class Scanners
29
{
30
    /** @type array      scanner classes */
31
    public $scanners = [];
32
33
34
    /**
35
     * Registers a scanner
36
     *
37
     * @param array|string   $uScanners    scanner instances
38
     *
39
     * @return void
40
     */
41
    public function register(...$uScanners)
42
    {
43
        foreach ($uScanners as $tScanner) {
44
            $this->scanners[] = $tScanner;
45
        }
46
    }
47
48
    /**
49
     * Scans all files in folders
50
     *
51
     * @param array|string  $uFolders          folders
52
     *
53
     * @return void
54
     */
55
    public function processFolder(...$uFolders)
56
    {
57
        foreach ($uFolders as $tFolder) {
58
            FileSystem::getFilesWalk(
59
                $tFolder,
0 ignored issues
show
Bug introduced by
It seems like $tFolder defined by $tFolder on line 57 can also be of type array; however, Scabbia\Helpers\FileSystem::getFilesWalk() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
                "*.php",
61
                true,
62
                [$this, "processFile"]
63
            );
64
        }
65
    }
66
67
    /**
68
     * Scans a file
69
     *
70
     * @param string      $uFile             file path
71
     *
72
     * @return void
73
     */
74
    public function processFile($uFile)
75
    {
76
        $tFileContents = FileSystem::read($uFile);
77
78
        foreach ($this->scanners as $tScanner) {
79
            $tScanner->processFile($uFile, $tFileContents);
80
        }
81
82
        $tTokenStream = TokenStream::fromString($tFileContents);
0 ignored issues
show
Security Bug introduced by
It seems like $tFileContents defined by \Scabbia\Helpers\FileSystem::read($uFile) on line 76 can also be of type false; however, Scabbia\Scanners\TokenStream::fromString() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
83
        $this->processTokenStream($tTokenStream);
84
    }
85
86
    /**
87
     * Scans a token stream
88
     *
89
     * @param TokenStream $uTokenStream      extracted tokens wrapped with tokenstream
90
     *
91
     * @return void
92
     */
93
    public function processTokenStream(TokenStream $uTokenStream)
94
    {
95
        foreach ($this->scanners as $tScanner) {
96
            $tScanner->processTokenStream($uTokenStream);
97
        }
98
99
        $tBuffer = "";
100
101
        $tUses = [];
102
        $tLastNamespace = null;
103
        $tLastClass = null;
104
        $tLastClassDerivedFrom = null;
105
        $tExpectation = 0; // 1=namespace, 2=class
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
106
107
        foreach ($uTokenStream as $tToken) {
108
            if ($tToken[0] === T_WHITESPACE) {
109
                continue;
110
            }
111
112
            if ($tExpectation === 0) {
113
                if ($tToken[0] === T_NAMESPACE) {
114
                    $tBuffer = "";
115
                    $tExpectation = 1;
116
                    continue;
117
                }
118
119
                if ($tToken[0] === T_CLASS) {
120
                    $tExpectation = 2;
121
                    continue;
122
                }
123
124
                if ($tToken[0] === T_USE) {
125
                    $tBuffer = "";
126
                    $tExpectation = 5;
127
                    continue;
128
                }
129 View Code Duplication
            } elseif ($tExpectation === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
                if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) {
131
                    $tBuffer .= $tToken[1];
132
                } else {
133
                    $tLastNamespace = $tBuffer;
134
                    $tExpectation = 0;
135
                }
136
            } elseif ($tExpectation === 2) {
137
                $tLastClass = "{$tLastNamespace}\\{$tToken[1]}";
138
                $tExpectation = 3;
139
            } elseif ($tExpectation === 3) {
140
                if ($tToken[0] === T_EXTENDS) {
141
                    $tBuffer = "";
142
                    $tExpectation = 4;
143
                    continue;
144
                }
145
146
                $tSkip = false;
147
                if ($tLastClassDerivedFrom !== null && !class_exists($tLastClassDerivedFrom)) {
148
                    $tSkip = true;
0 ignored issues
show
Unused Code introduced by
$tSkip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
                    throw new LogicException(sprintf(
150
                        "\"%s\" derived from \"%s\", but it could not be found.\n",
151
                        $tLastClass,
152
                        $tLastClassDerivedFrom
153
                    ));
154
                }
155
156
                if (!$tSkip && !isset($this->result[$tLastClass])) {
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
157
                    $this->processClass($tLastClass);
158
                }
159
160
                $tExpectation = 0;
161
            } elseif ($tExpectation === 4) {
162
                if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) {
163
                    $tBuffer .= $tToken[1];
164
                } else {
165
                    $tFound = false;
166
167
                    foreach ($tUses as $tUse) {
168
                        $tLength = strlen($tBuffer);
169
                        if (strlen($tUse) >= $tLength && substr($tUse, -$tLength) === $tBuffer) {
170
                            $tLastClassDerivedFrom = $tUse;
171
                            $tFound = true;
172
                            break;
173
                        }
174
                    }
175
176
                    if (!$tFound) {
177
                        if (strpos($tBuffer, "\\") !== false) {
178
                            $tLastClassDerivedFrom = $tBuffer;
179
                        } else {
180
                            $tLastClassDerivedFrom = "{$tLastNamespace}\\{$tBuffer}";
181
                        }
182
                    }
183
184
                    $tExpectation = 3;
185
                }
186 View Code Duplication
            } elseif ($tExpectation === 5) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
                if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) {
188
                    $tBuffer .= $tToken[1];
189
                } else {
190
                    $tUses[] = $tBuffer;
191
                    $tExpectation = 0;
192
                }
193
            }
194
        }
195
    }
196
197
    /**
198
     * Processes classes using reflection
199
     *
200
     * @param string      $uClass            class name
201
     *
202
     * @return void
203
     */
204
    public function processClass($uClass)
205
    {
206
        $tReflection = new ReflectionClass($uClass);
207
208
        foreach ($this->scanners as $tScanner) {
209
            $tScanner->processClass($uClass, $tReflection);
210
        }
211
    }
212
}
213