Completed
Pull Request — master (#303)
by Enrico
03:37
created

FunctionDefinition   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 83.72%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 110
ccs 36
cts 43
cp 0.8372
rs 10
wmc 12
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C compile() 0 45 8
A getFilepath() 0 4 1
A setFilepath() 0 4 1
A getParams() 0 4 1
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 PHPSA\Compiler\Parameter;
11
use PHPSA\Compiler\Types;
12
use PhpParser\Node;
13
use PHPSA\Compiler\Event;
14
15
/**
16
 * Function Definition
17
 *
18
 * @package PHPSA\Definition
19
 */
20
class FunctionDefinition extends ParentDefinition
21
{
22
    /**
23
     * @todo Use Finder
24
     *
25
     * @var string
26
     */
27
    protected $filepath;
28
29
    /**
30
     * @var Node\Stmt\Function_
31
     */
32
    protected $statement;
33
34
    /**
35
     * @var int
36
     */
37
    protected $returnTypes = CompiledExpression::MIXED;
38
39
    /**
40
     * @var array
41
     */
42
    protected $possibleReturnTypes = [];
43
44
    /**
45
     * @param string $name
46
     * @param Node\Stmt\Function_ $statement
47
     */
48 5
    public function __construct($name, Node\Stmt\Function_ $statement)
49
    {
50 5
        $this->name = $name;
51 5
        $this->statement = $statement;
52 5
    }
53
54
    /**
55
     * Compile function to check it
56
     *
57
     * @param Context $context
58
     * @return bool
59
     */
60 5
    public function compile(Context $context)
61
    {
62 5
        if ($this->compiled) {
63
            return true;
64
        }
65
66 5
        $context->setFilepath($this->filepath);
67 5
        $this->compiled = true;
68
69 5
        $context->scopePointer = $this->getPointer();
70 5
        $context->setScope(null);
71
72 5
        $context->getEventManager()->fire(
73 5
            Event\StatementBeforeCompile::EVENT_NAME,
74 5
            new Event\StatementBeforeCompile(
75 5
                $this->statement,
76
                $context
77 5
            )
78 5
        );
79
80 5
        if (count($this->statement->params) > 0) {
81
            /** @var  Node\Param $parameter */
82 3
            foreach ($this->statement->params as $parameter) {
83 3
                $type = CompiledExpression::UNKNOWN;
84
85 3
                if ($parameter->type) {
86
                    if (is_string($parameter->type)) {
87
                        $type = Types::getType($parameter->type);
88
                    } elseif ($parameter->type instanceof Node\Name) {
89
                        $type = CompiledExpression::OBJECT;
90
                    }
91
                }
92
93 3
                $context->addVariable(
94 3
                    new Parameter($parameter->name, null, $type, $parameter->byRef)
95 3
                );
96 3
            }
97 3
        }
98
99 5
        foreach ($this->statement->stmts as $st) {
100 4
            \PHPSA\nodeVisitorFactory($st, $context);
101 5
        }
102
103 5
        return true;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 5
    public function getFilepath()
110
    {
111 5
        return $this->filepath;
112
    }
113
114
    /**
115
     * @param string $filepath
116
     */
117 5
    public function setFilepath($filepath)
118
    {
119 5
        $this->filepath = $filepath;
120 5
    }
121
122
    /**
123
    * @return Node\Param[]
124
    */
125 1
    public function getParams()
126
    {
127 1
        return $this->statement->params;
128
    }
129
}
130