Completed
Pull Request — master (#193)
by Enrico
06:57 queued 02:48
created

TraitDefinition::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 1
rs 9.4285
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->getEventManager()->fire(
44 1
            Event\StatementBeforeCompile::EVENT_NAME,
45 1
            new Event\StatementBeforeCompile(
46 1
                $this->statement,
47
                $context
48 1
            )
49 1
        );
50
51 1
        return true;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getFilepath()
58
    {
59 1
        return $this->filepath;
60
    }
61
62
    /**
63
     * @param string $filepath
64
     */
65 1
    public function setFilepath($filepath)
66
    {
67 1
        $this->filepath = $filepath;
68 1
    }
69
70
    /**
71
     * @return bool
72
     */
73 1
    public function precompile()
74
    {
75 1
        foreach ($this->statement->stmts as $stmt) {
76
            if ($stmt instanceof Stmt\ClassMethod) {
77
                $this->addMethod(new ClassMethod($stmt->name, $stmt, $stmt->type));
78
            }
79 1
        }
80
81 1
        return true;
82
    }
83
84
    /**
85
     * @param $method
86
     */
87
    public function addMethod($method)
88
    {
89
        $this->methods[] = $method;
90
    }
91
92
    /**
93
     * @return ClassMethod[]
94
     */
95
    public function getMethods()
96
    {
97
        return $this->methods;
98
    }
99
}
100