TraitDefinition::getMethods()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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