Completed
Push — master ( d925db...e7e919 )
by Vitaly
02:50
created

MethodGenerator::code()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:30
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Class method generation class.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class MethodGenerator extends FunctionGenerator
14
{
15
    /** @var bool Flag that method is static */
16
    protected $isStatic = false;
17
18
    /** @var bool Flag that method is abstract */
19
    protected $isAbstract = false;
20
21
    /** @var bool Flag that method is final */
22
    protected $isFinal = false;
23
24
    /** @var string Method visibility */
25
    protected $visibility = ClassGenerator::VISIBILITY_PUBLIC;
26
27
    /**
28
     * Set method to be static.
29
     *
30
     * @return MethodGenerator
31
     */
32
    public function defStatic() : MethodGenerator
33
    {
34
        $this->isStatic = true;
35
36
        return $this;
37
    }
38
39
    /**
40
     * Set method to be final.
41
     *
42
     * @return MethodGenerator
43
     */
44
    public function defFinal() : MethodGenerator
45
    {
46
        if ($this->isAbstract) {
47
            throw new \InvalidArgumentException('Method cannot be final as it is already abstract');
48
        }
49
50
        $this->isFinal = true;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set method to be abstract.
57
     *
58
     * @return MethodGenerator
59
     */
60
    public function defAbstract() : MethodGenerator
61
    {
62
        if ($this->isFinal) {
63
            throw new \InvalidArgumentException('Method cannot be abstract as it is already final');
64
        }
65
66
        $this->isAbstract = true;
67
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function defLine(string $code) : AbstractGenerator
75
    {
76
        if ($this->isAbstract === true) {
77
            throw new \InvalidArgumentException('Abstract class cannot have code');
78
        }
79
80
        return parent::defLine($code);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function code(int $indentation = 0) : string
87
    {
88
        if ($this->isAbstract === true) {
89
            return $this->buildDefinition() . ';';
90
        } else {
91
            return parent::code($indentation);
92
        }
93
    }
94
95
    /**
96
     * Build function definition.
97
     *
98
     * @return string Function definition
99
     */
100
    protected function buildDefinition()
101
    {
102
        return ($this->isFinal ? 'final ' : '') .
103
        ($this->isAbstract ? 'abstract ' : '') .
104
        $this->visibility . ' ' .
105
        ($this->isStatic ? 'static ' : '') .
106
        'function ' . $this->name . '()';
107
    }
108
}
109