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

TraitDefinition   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 73.32%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 0
loc 90
ccs 22
cts 30
cp 0.7332
rs 10
c 5
b 0
f 0
wmc 9
lcom 1
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
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
A addMethod() 0 4 1
A getMethods() 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\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