TraitDefinition   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addMethod() 0 4 1
A getMethods() 0 4 1
A __construct() 0 5 1
A compile() 0 14 1
A getFilepath() 0 4 1
A setFilepath() 0 4 1
A precompile() 0 10 3
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