Passed
Branch master (da5350)
by Timur
02:19
created

PhpClass::removeImplements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
use function join;
8
9
class PhpClass extends OOPStructure
10
{
11
    protected string $extends = '';
12
    protected bool   $isAbstract = false;
13
    protected bool   $isFinal = false;
14
    protected array  $implements = [];
15
16
    public function setExtends(string $fqcn): self
17
    {
18
        $this->extends = $this->resolveQualifier($fqcn);
19
20
        return $this;
21
    }
22
23
    public function addImplements(string ...$classNames): self
24
    {
25
        foreach ($classNames as $name) {
26
            $this->implements[] = $this->resolveQualifier($name);
27
        }
28
29
        return $this;
30
    }
31
32
    public function removeImplements(): self
33
    {
34
        $this->implements = [];
35
36
        return $this;
37
    }
38
39
    protected function buildImplements(): string
40
    {
41
        return !empty($this->implements) ? ' implements '.join(', ', $this->implements) : '';
42
    }
43
44
    protected function buildExtends(): string
45
    {
46
        if ($this->extends) {
47
            return " extends $this->extends";
48
        }
49
50
        return '';
51
    }
52
53
    public function setName(string $name): self
54
    {
55
        $this->name = $name;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param mixed $value
62
     */
63
    public function addConst(string $name, $value, string $modifier = Modifier::PUBLIC): self
64
    {
65
        return $this
66
            ->append(Property::new($name, $modifier, '', $value)->setConst());
67
    }
68
69
    public function addProperty(string $name, string $modifier = Modifier::PUBLIC, string $type = '', $defaulValue = ''): self
70
    {
71
        return $this->append(new Property($name, $modifier, $type, $defaulValue));
72
    }
73
74
    public function addMethod(string $name, string $modifier = 'public', string $returnType = ''): self
75
    {
76
        return $this
77
            ->append(new Method($name, $modifier, $returnType))
78
            ->emptyLine()
79
        ;
80
    }
81
82
    public function createMethod(string $name, string $modifier = 'public', string $returnType = ''): Method
83
    {
84
        $method = new Method($name, $modifier, $returnType);
85
86
        $this->append($method)->emptyLine();
87
88
        return $method;
89
    }
90
91
    public function createConstructor(string $modifier = 'public'): Method
92
    {
93
        $constructor = new Method('__construct', $modifier, '');
94
95
        $this->append($constructor)->emptyLine();
96
97
        return $constructor;
98
    }
99
100
    public function generate(): string
101
    {
102
        return <<<CODE
103
        {$this->buildDocBlock()}{$this->buildPrefix()}class $this->name{$this->buildExtends()}{$this->buildImplements()}
104
        {
105
        {$this->generateContent()}
106
        }
107
        CODE;
108
    }
109
110
    private function buildPrefix(): string
111
    {
112
        $prefix = '';
113
114
        if ($this->isFinal) {
115
            $prefix .= 'final ';
116
        } elseif ($this->isAbstract) {
117
            $prefix .= 'abstract ';
118
        }
119
120
        return $prefix;
121
    }
122
123
    public function isFinal(): bool
124
    {
125
        return $this->isFinal;
126
    }
127
128
    public function setFinal(): self
129
    {
130
        $this->isFinal = true;
131
132
        // Class cannot be final and abstract at the same time
133
        $this->isAbstract = false;
134
135
        return $this;
136
    }
137
138
    public function unsetFinal(): self
139
    {
140
        $this->isFinal = false;
141
142
        return $this;
143
    }
144
145
    public function isAbstract(): bool
146
    {
147
        return $this->isAbstract;
148
    }
149
150
    public function setAbstract(): self
151
    {
152
        $this->isAbstract = true;
153
154
        // Class cannot be final and abstract at the same time
155
        $this->isFinal = false;
156
157
        return $this;
158
    }
159
160
    public function unsetAbstract(): self
161
    {
162
        $this->isAbstract = false;
163
164
        return $this;
165
    }
166
}
167