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

TraitDefinition   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.41%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 88
ccs 21
cts 29
cp 0.7241
rs 10
c 4
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 12 1
A setFilepath() 0 4 1
A precompile() 0 10 3
A addMethod() 0 4 1
A getMethods() 0 4 1
A getFilepath() 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->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