Completed
Pull Request — master (#235)
by Kévin
04:11
created

Context::notice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA;
7
8
use PHPSA\Compiler\Expression;
9
use PHPSA\Compiler\GlobalVariable;
10
use PHPSA\Definition\AbstractDefinition;
11
use PHPSA\Definition\ParentDefinition;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Webiny\Component\EventManager\EventManager;
14
15
class Context
16
{
17
    /**
18
     * For FunctionDefinition it's null, use scopePointer
19
     *
20
     * @var ParentDefinition|null
21
     */
22
    public $scope;
23
24
    /**
25
     * @var AliasManager
26
     */
27
    public $aliasManager;
28
29
    /**
30
     * @var Application
31
     */
32
    public $application;
33
34
    /**
35
     * @var string|integer
36
     */
37
    public $currentBranch;
38
39
    /**
40
     * @var OutputInterface
41
     */
42
    public $output;
43
44
    /**
45
     * @var Variable[]
46
     */
47
    protected $symbols = [];
48
49
    /**
50
     * @var ScopePointer|null
51
     */
52
    public $scopePointer;
53
54
    /**
55
     * @var string
56
     */
57
    protected $filepath;
58
59
    /**
60
     * @var EventManager
61
     */
62
    protected $eventManager;
63
64
    /**
65
     * Construct our Context with all needed information
66
     *
67
     * @param OutputInterface $output
68
     * @param Application $application
69
     * @param EventManager $eventManager
70
     */
71 868
    public function __construct(OutputInterface $output, Application $application, EventManager $eventManager)
72
    {
73 868
        $this->output = $output;
74 868
        $this->application = $application;
75
76 868
        $this->initGlobals();
77 868
        $this->eventManager = $eventManager;
78 868
    }
79
80
    /**
81
     * @return Expression
82
     */
83 859
    public function getExpressionCompiler()
84
    {
85 859
        return new Expression($this, $this->eventManager);
86
    }
87
88
    /**
89
     * Adds all global variables to the context.
90
     */
91 868
    public function initGlobals()
92
    {
93
        /**
94
         * http://php.net/manual/language.variables.superglobals.php
95
         */
96 868
        $this->addVariable(new GlobalVariable('GLOBALS', [], CompiledExpression::ARR));
97 868
        $this->addVariable(new GlobalVariable('_SERVER', [], CompiledExpression::ARR));
98 868
        $this->addVariable(new GlobalVariable('_GET', [], CompiledExpression::ARR));
99 868
        $this->addVariable(new GlobalVariable('_POST', [], CompiledExpression::ARR));
100 868
        $this->addVariable(new GlobalVariable('_FILES', [], CompiledExpression::ARR));
101 868
        $this->addVariable(new GlobalVariable('_COOKIE', [], CompiledExpression::ARR));
102 868
        $this->addVariable(new GlobalVariable('_SESSION', [], CompiledExpression::ARR));
103 868
        $this->addVariable(new GlobalVariable('_REQUEST', [], CompiledExpression::ARR));
104 868
        $this->addVariable(new GlobalVariable('_ENV', [], CompiledExpression::ARR));
105 868
    }
106
107
    /**
108
     * Creates a variable from a symbol and adds it to the context.
109
     *
110
     * @param string $name
111
     * @return Variable
112
     */
113 3
    public function addSymbol($name)
114
    {
115 3
        $variable = new Variable($name);
116 3
        $this->symbols[$name] = $variable;
117
118 3
        return $variable;
119
    }
120
121
    /**
122
     * Adds a variable to the context.
123
     *
124
     * @param Variable $variable
125
     * @return bool
126
     */
127 868
    public function addVariable(Variable $variable)
128
    {
129 868
        $this->symbols[$variable->getName()] = $variable;
130
131 868
        return true;
132
    }
133
134
    /**
135
     * Resets context to beginning stage.
136
     */
137 41
    public function clear()
138
    {
139 41
        $this->symbols = [];
140 41
        $this->scope = null;
141 41
        $this->scopePointer = null;
142 41
        $this->currentBranch = null;
143 41
        $this->aliasManager = null;
144
145 41
        $this->initGlobals();
146 41
    }
147
148
    /**
149
     * Clears only all symbols.
150
     */
151 39
    public function clearSymbols()
152
    {
153 39
        unset($this->symbols);
154 39
        $this->symbols = [];
155 39
    }
156
157
    /**
158
     * Returns a variable if it exists.
159
     *
160
     * @param $name
161
     * @return Variable|null
162
     */
163 79
    public function getSymbol($name)
164
    {
165 79
        return isset($this->symbols[$name]) ? $this->symbols[$name] : null;
166
    }
167
168
    /**
169
     * Creates a notice message.
170
     *
171
     * @param string $type
172
     * @param string $message
173
     * @param \PhpParser\NodeAbstract $expr
174
     * @param int $status
175
     * @return bool
176
     */
177 41
    public function notice($type, $message, \PhpParser\NodeAbstract $expr, $status = Check::CHECK_SAFE)
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
    {
179 41
        return $this->addIssue($type, $message, new IssueLocation($this->filepath, $expr->getLine()));
180
    }
181
182
    /**
183
     * Creates a syntax error message.
184
     *
185
     * @param \PhpParser\Error $exception
186
     * @param string $filepath
187
     * @return bool
188
     */
189
    public function syntaxError(\PhpParser\Error $exception, $filepath)
190
    {
191
        return $this->addIssue('syntax-error', $exception->getMessage(), new IssueLocation($filepath, $expr->getLine()));
0 ignored issues
show
Bug introduced by
The variable $expr 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...
192
    }
193
194
    /**
195
     * @param string $type
196
     * @param string $message
197
     * @param IssueLocation $location
198
     * @return bool
199
     */
200 41
    protected function addIssue($type, $message, IssueLocation $location)
201
    {
202 41
        $issue = new Issue($type, $message, $location);
203
204 41
        if ($this->application->getConfiguration()->getValue('blame')) {
205
            exec("git blame --show-email -L {$line},{$line} " . $location->getFilePath(), $result);
0 ignored issues
show
Bug introduced by
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...
206
            if ($result && isset($result[0])) {
207
                $issue->setBlame(trim($result[0]));
208
            }
209
        }
210
211 41
        $issueCollector = $this->application->getIssuesCollector();
212 41
        $issueCollector->addIssue($issue);
213
214 41
        return true;
215
    }
216
217
    /**
218
     * Returns an array of all variables.
219
     *
220
     * @return Variable[]
221
     */
222 39
    public function getSymbols()
223
    {
224 39
        return $this->symbols;
225
    }
226
227
    /**
228
     * @param AbstractDefinition $scope
229
     */
230 868
    public function setScope(AbstractDefinition $scope = null)
231
    {
232 868
        $this->scope = $scope;
0 ignored issues
show
Documentation Bug introduced by
It seems like $scope can also be of type object<PHPSA\Definition\AbstractDefinition>. However, the property $scope is declared as type object<PHPSA\Definition\ParentDefinition>|null. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
233 868
    }
234
235
    /**
236
     * Creates a debug message.
237
     */
238 41
    public function debug($message, \PhpParser\Node $expr = null)
239
    {
240 41
        if ($this->output->isDebug()) {
241
            $this->output->writeln('[DEBUG] ' . $message);
242
            $this->output->write($this->filepath);
243
244
            if ($expr) {
245
                $this->output->write(':' . $expr->getLine());
246
            }
247
248
            $this->output->writeln('');
249
        }
250 41
    }
251
252
    /**
253
     * @return string
254
     */
255
    public function getFilepath()
256
    {
257
        return $this->filepath;
258
    }
259
260
    /**
261
     * @param string $filepath
262
     */
263 41
    public function setFilepath($filepath)
264
    {
265 41
        $this->filepath = $filepath;
266 41
    }
267
268
    /**
269
     * @return integer
270
     */
271 36
    public function getCurrentBranch()
272
    {
273 36
        return $this->currentBranch;
274
    }
275
276
    /**
277
     * @param int|string $currentBranch
278
     */
279 9
    public function setCurrentBranch($currentBranch)
280
    {
281 9
        $this->currentBranch = $currentBranch;
282 9
    }
283
284
    /**
285
     * Updates all references on the given variable.
286
     *
287
     * @param Variable $variable
288
     * @param $type
289
     * @param $value
290
     */
291 5
    public function modifyReferencedVariables(Variable $variable, $type, $value)
292
    {
293 5
        foreach ($this->symbols as $symbol) {
294 5
            $referencedTo = $symbol->getReferencedTo();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $referencedTo is correct as $symbol->getReferencedTo() (which targets PHPSA\Variable::getReferencedTo()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
295 5
            if ($referencedTo) {
296 2
                if ($referencedTo === $variable) {
297 2
                    $symbol->modify($type, $value);
298 2
                }
299 2
            }
300 5
        }
301 5
    }
302
303
    /**
304
     * @return EventManager
305
     */
306 54
    public function getEventManager()
307
    {
308 54
        return $this->eventManager;
309
    }
310
}
311