Class_::implement()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace PhpParser\Builder;
4
5
use PhpParser;
6
use PhpParser\Node\Name;
7
use PhpParser\Node\Stmt;
8
9
class Class_ extends Declaration
10
{
11
    protected $name;
12
13
    protected $extends = null;
14
    protected $implements = array();
15
    protected $type = 0;
16
17
    protected $uses = array();
18
    protected $constants = array();
19
    protected $properties = array();
20
    protected $methods = array();
21
22
    /**
23
     * Creates a class builder.
24
     *
25
     * @param string $name Name of the class
26
     */
27
    public function __construct($name) {
28
        $this->name = $name;
29
    }
30
31
    /**
32
     * Extends a class.
33
     *
34
     * @param Name|string $class Name of class to extend
35
     *
36
     * @return $this The builder instance (for fluid interface)
37
     */
38
    public function extend($class) {
39
        $this->extends = $this->normalizeName($class);
40
41
        return $this;
42
    }
43
44
    /**
45
     * Implements one or more interfaces.
46
     *
47
     * @param Name|string ...$interfaces Names of interfaces to implement
48
     *
49
     * @return $this The builder instance (for fluid interface)
50
     */
51
    public function implement() {
52
        foreach (func_get_args() as $interface) {
53
            $this->implements[] = $this->normalizeName($interface);
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * Makes the class abstract.
61
     *
62
     * @return $this The builder instance (for fluid interface)
63
     */
64
    public function makeAbstract() {
65
        $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
66
67
        return $this;
68
    }
69
70
    /**
71
     * Makes the class final.
72
     *
73
     * @return $this The builder instance (for fluid interface)
74
     */
75
    public function makeFinal() {
76
        $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
77
78
        return $this;
79
    }
80
81
    /**
82
     * Adds a statement.
83
     *
84
     * @param Stmt|PhpParser\Builder $stmt The statement to add
85
     *
86
     * @return $this The builder instance (for fluid interface)
87
     */
88
    public function addStmt($stmt) {
89
        $stmt = $this->normalizeNode($stmt);
90
91
        $targets = array(
92
            'Stmt_TraitUse'    => &$this->uses,
93
            'Stmt_ClassConst'  => &$this->constants,
94
            'Stmt_Property'    => &$this->properties,
95
            'Stmt_ClassMethod' => &$this->methods,
96
        );
97
98
        $type = $stmt->getType();
99
        if (!isset($targets[$type])) {
100
            throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
101
        }
102
103
        $targets[$type][] = $stmt;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Returns the built class node.
110
     *
111
     * @return Stmt\Class_ The built class node
112
     */
113
    public function getNode() {
114
        return new Stmt\Class_($this->name, array(
115
            'type' => $this->type,
116
            'extends' => $this->extends,
117
            'implements' => $this->implements,
118
            'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
119
        ), $this->attributes);
120
    }
121
}