Completed
Push — master ( 9bdd1a...55c284 )
by Дмитрий
03:18
created

ClosureDefinition::preCompile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 20
rs 9.2
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
use PhpParser\Node;
11
12
/**
13
 * Class FunctionDefinition
14
 * @package PHPSA\Definition
15
 */
16
class ClosureDefinition extends ParentDefinition
17
{
18
    /**
19
     * @todo Use Finder
20
     *
21
     * @var string
22
     */
23
    protected $filepath;
24
25
    /**
26
     * @var Node\Expr\Closure
27
     */
28
    protected $statement;
29
30
    /**
31
     * @var int
32
     */
33
    protected $returnTypes = CompiledExpression::MIXED;
34
35
    /**
36
     * @var array
37
     */
38
    protected $possibleReturnTypes = array();
39
40
    protected $symbolTable = [];
41
42
    /**
43
     * @param Node\Expr\Closure $statement
44
     */
45
    public function __construct(Node\Expr\Closure $statement)
46
    {
47
        $this->statement = $statement;
48
    }
49
50
    /**
51
     * @param Context $context
52
     */
53
    public function preCompile(Context $context)
54
    {
55
        if ($this->statement->uses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statement->uses of type PhpParser\Node\Expr\ClosureUse[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
            /**
57
             * Store variables from User to next restore Context
58
             */
59
            foreach ($this->statement->uses as $variable) {
60
                $variable = $context->getSymbol($variable->var);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $variable is correct as $context->getSymbol($variable->var) (which targets PHPSA\Context::getSymbol()) 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...
61
                if ($variable) {
62
                    $this->symbolTable[$variable->getName()] = clone $variable;
63
                }
64
            }
65
        }
66
    }
67
68
    /**
69
     * Compile function to check it
70
     *
71
     * @param Context $context
72
     * @return bool
73
     */
74
    public function compile(Context $context)
75
    {
76
        if ($this->compiled) {
77
            return true;
78
        }
79
80
        $context->setFilepath($this->filepath);
81
        $this->compiled = true;
82
83
        $context->clearSymbols();
84
        $context->scopePointer = $this->getPointer();
85
        $context->setScope(null);
86
87
        if (count($this->statement->stmts) == 0) {
88
            return $context->notice(
89
                'not-implemented-function',
90
                sprintf('Closure %s() is not implemented', $this->name),
91
                $this->statement
92
            );
93
        }
94
95
        if (count($this->statement->params) > 0) {
96
            /** @var  Node\Param $parameter */
97
            foreach ($this->statement->params as $parameter) {
98
                $context->addSymbol($parameter->name);
99
            }
100
        }
101
102
        foreach ($this->statement->stmts as $st) {
103
            \PHPSA\nodeVisitorFactory($st, $context);
104
        }
105
106
        return true;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getFilepath()
113
    {
114
        return $this->filepath;
115
    }
116
117
    /**
118
     * @param string $filepath
119
     */
120
    public function setFilepath($filepath)
121
    {
122
        $this->filepath = $filepath;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getNamespace()
129
    {
130
        return $this->namespace;
131
    }
132
}
133