Completed
Push — master ( 54f421...531c73 )
by Дмитрий
10s
created

TraitDefinition::getFilepath()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\Context;
9
use PhpParser\Node\Stmt;
10
use PHPSA\Compiler\Event;
11
12
class TraitDefinition extends ParentDefinition
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $filepath;
18
19
    /**
20
     * @var Stmt\Trait_
21
     */
22
    protected $statement;
23
24
    /**
25
     * @var ClassMethod[]
26
     */
27
    protected $methods = [];
28
29 1
    public function __construct($name, Stmt\Trait_ $statement)
30
    {
31 1
        $this->name = $name;
32 1
        $this->statement = $statement;
33 1
    }
34
35
    /**
36
     * Compile the definition
37
     *
38
     * @param Context $context
39
     * @return boolean
40
     */
41 1
    public function compile(Context $context)
42
    {
43 1
        $context->setFilepath($this->filepath);
44
45 1
        $context->getEventManager()->fire(
46 1
            Event\StatementBeforeCompile::EVENT_NAME,
47 1
            new Event\StatementBeforeCompile(
48 1
                $this->statement,
49
                $context
50 1
            )
51 1
        );
52
53 1
        return true;
54
    }
55
56
    /**
57
     * @return string
58
     */
59 1
    public function getFilepath()
60
    {
61 1
        return $this->filepath;
62
    }
63
64
    /**
65
     * @param string $filepath
66
     */
67 1
    public function setFilepath($filepath)
68
    {
69 1
        $this->filepath = $filepath;
70 1
    }
71
72
    /**
73
     * @return bool
74
     */
75 1
    public function precompile()
76
    {
77 1
        foreach ($this->statement->stmts as $stmt) {
78
            if ($stmt instanceof Stmt\ClassMethod) {
79
                $this->addMethod(new ClassMethod($stmt->name, $stmt, $stmt->type));
80
            }
81 1
        }
82
83 1
        return true;
84
    }
85
86
    /**
87
     * @param $method
88
     */
89
    public function addMethod($method)
90
    {
91
        $this->methods[] = $method;
92
    }
93
94
    /**
95
     * @return ClassMethod[]
96
     */
97
    public function getMethods()
98
    {
99
        return $this->methods;
100
    }
101
}
102