1 | <?php |
||
2 | /** |
||
3 | * This file is a part of "Axessors" library. |
||
4 | * |
||
5 | * @author <[email protected]> |
||
6 | * @license GPL |
||
7 | */ |
||
8 | |||
9 | namespace NoOne4rever\Axessors; |
||
10 | |||
11 | use NoOne4rever\Axessors\Exceptions\ParseError; |
||
12 | |||
13 | /** |
||
14 | * Class Lexer. |
||
15 | * |
||
16 | * A general implementation of Axessors tokenizer. |
||
17 | * |
||
18 | * @package NoOne4rever\Axessors |
||
19 | */ |
||
20 | abstract class Lexer |
||
21 | { |
||
22 | /** @var \ReflectionClass class' reflection */ |
||
23 | protected $reflection; |
||
24 | /** @var resource file with PHP-code */ |
||
25 | protected $source; |
||
26 | /** @var int the first line of class declaration */ |
||
27 | protected $startLine; |
||
28 | /** @var int the last line of class declaration */ |
||
29 | protected $endLine; |
||
30 | /** @var string current line */ |
||
31 | protected $currentLine; |
||
32 | /** @var int number of the current symbol */ |
||
33 | protected $currentSym = 0; |
||
34 | |||
35 | /** @var int number of the current line */ |
||
36 | private $lineNumber = 0; |
||
37 | |||
38 | /** |
||
39 | * Lexer constructor. |
||
40 | * |
||
41 | * @param \ReflectionClass $reflection class' reflection |
||
42 | */ |
||
43 | 5 | public function __construct(\ReflectionClass $reflection) |
|
44 | { |
||
45 | 5 | $this->reflection = $reflection; |
|
46 | 5 | $this->openSource(); |
|
47 | 5 | $this->skipFirstLines(); |
|
48 | 5 | } |
|
49 | |||
50 | /** |
||
51 | * Splits given code into array of tokens. |
||
52 | * |
||
53 | * @param string $code |
||
54 | * @param string[] $expectations tokens' patterns |
||
55 | * @param int[] $requiredItems necessary tokens |
||
56 | * @return string[] found tokens |
||
57 | * @throws ParseError if an important token not found in Axessors comment |
||
58 | */ |
||
59 | 5 | protected function parse(string $code, array $expectations, array $requiredItems): array |
|
60 | { |
||
61 | 5 | $this->currentSym = 0; |
|
62 | 5 | $code = trim($code); |
|
63 | 5 | $result = []; |
|
64 | 5 | foreach ($expectations as $index => $pattern) { |
|
65 | 5 | $this->skipWhitespace($code); |
|
66 | 5 | preg_match($this->makeRegEx($pattern), substr($code, $this->currentSym), $token); |
|
67 | 5 | if (empty($token)) { |
|
68 | 5 | if (in_array($index, $requiredItems)) { |
|
69 | 5 | throw new ParseError("token with pattern \"$pattern\" not found while parsing {$this->reflection->getFileName()}:{$this->lineNumber}"); |
|
70 | } |
||
71 | } else { |
||
72 | 5 | $result[$index] = $token[0]; |
|
73 | 5 | $this->currentSym += strlen($token[0]); |
|
74 | } |
||
75 | } |
||
76 | 5 | return $result; |
|
77 | } |
||
78 | |||
79 | 5 | private function makeRegEx(string $token): string |
|
80 | { |
||
81 | 5 | return "/^$token/"; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Skips whitespace symbols in code. |
||
86 | * |
||
87 | * @param string $code line of code |
||
88 | */ |
||
89 | 5 | private function skipWhitespace(string $code): void |
|
90 | { |
||
91 | 5 | preg_match('/^\s+/', substr($code, $this->currentSym), $whitespace); |
|
92 | 5 | if (!empty($whitespace)) { |
|
93 | 5 | $this->currentSym += strlen($whitespace[0]); |
|
94 | } |
||
95 | 5 | } |
|
96 | |||
97 | /** |
||
98 | * Opens source with class declaration. |
||
99 | */ |
||
100 | 5 | protected function openSource(): void |
|
101 | { |
||
102 | 5 | $this->source = fopen($this->reflection->getFileName(), 'rb'); |
|
0 ignored issues
–
show
|
|||
103 | 5 | $this->startLine = $this->reflection->getStartLine(); |
|
104 | 5 | $this->endLine = $this->reflection->getEndLine(); |
|
105 | 5 | } |
|
106 | |||
107 | /** |
||
108 | * Reads a line from source. |
||
109 | */ |
||
110 | 5 | protected function readLine(): void |
|
111 | { |
||
112 | 5 | $this->currentLine = fgets($this->source); |
|
113 | 5 | ++$this->lineNumber; |
|
114 | 5 | } |
|
115 | |||
116 | /** |
||
117 | * Checks if the line is empty. |
||
118 | * |
||
119 | * @return bool result of the checkout |
||
120 | */ |
||
121 | 5 | protected function isLineEmpty(): bool |
|
122 | { |
||
123 | 5 | return $this->currentLine !== false; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Skips lines before class declaration. |
||
128 | */ |
||
129 | 5 | protected function skipFirstLines(): void |
|
130 | { |
||
131 | 5 | for ($i = 1; $i < $this->startLine; ++$i) { |
|
132 | 5 | $this->readLine(); |
|
133 | } |
||
134 | 5 | } |
|
135 | } |
||
136 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.