Completed
Push — master ( a943ea...15df77 )
by Igor
01:36
created

ClassGenerator::generateScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
namespace Leaditin\Code\Generator;
4
5
use Leaditin\Code\Flag;
6
7
/**
8
 * @package Leaditin\Code
9
 * @author Igor Vuckovic <[email protected]>
10
 * @license MIT
11
 */
12
class ClassGenerator extends ClassAwareGenerator
13
{
14
    /**
15
     * @var Flag
16
     */
17
    protected $flag;
18
19
    /**
20
     * @var string[]
21
     */
22
    protected $interfaces = [];
23
24
    /**
25
     * @var string[]
26
     */
27
    protected $traits = [];
28
29
    /**
30
     * @inheritDoc
31
     */
32 7
    public function __construct(
33
        ConstantGenerator $constantGenerator,
34
        DocBlockGenerator $docBlockGenerator,
35
        MethodGenerator $methodGenerator,
36
        PropertyGenerator $propertyGenerator
37
    ) {
38 7
        parent::__construct($constantGenerator, $docBlockGenerator, $methodGenerator, $propertyGenerator);
39
40 7
        $this->flag = new Flag();
41 7
    }
42
43
    /**
44
     * @param bool $isAbstract
45
     *
46
     * @return ClassGenerator
47
     */
48 1
    public function setAbstract(bool $isAbstract): self
49
    {
50 1
        $isAbstract ? $this->flag->addFlag(Flag::FLAG_ABSTRACT) : $this->flag->removeFlag(Flag::FLAG_ABSTRACT);
51
52 1
        return $this;
53
    }
54
55
    /**
56
     * @param bool $isFinal
57
     *
58
     * @return static
59
     */
60 1
    public function setFinal(bool $isFinal): self
61
    {
62 1
        $isFinal ? $this->flag->addFlag(Flag::FLAG_FINAL) : $this->flag->removeFlag(Flag::FLAG_FINAL);
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * @param string $fullyQualifiedInterfaceName
69
     *
70
     * @return static
71
     */
72 1
    public function implementInterface(string $fullyQualifiedInterfaceName): self
73
    {
74 1
        $fullyQualifiedInterfaceName = '\\' . ltrim($fullyQualifiedInterfaceName, '\\');
75
76 1
        $this->interfaces[$fullyQualifiedInterfaceName] = $fullyQualifiedInterfaceName;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @param string $fullyQualifiedTraitName
83
     *
84
     * @return static
85
     */
86 1
    public function useTrait(string $fullyQualifiedTraitName): self
87
    {
88 1
        $fullyQualifiedTraitName = '\\' . ltrim($fullyQualifiedTraitName, '\\');
89
90 1
        $this->traits[$fullyQualifiedTraitName] = $fullyQualifiedTraitName;
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98 3
    protected function scope(): string
99
    {
100 3
        return 'class';
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106 7
    protected function generateScope(): string
107
    {
108 7
        return $this->generateLine(
109 7
            "{$this->generateFlags()} $this->name{$this->generateInheritance()}{$this->generateImplements()}"
110 7
        ) . $this->generateLine('{') . $this->generateUses();
111
    }
112
113
    /**
114
     * @return string
115
     */
116 7
    protected function generateFlags(): string
117
    {
118 7
        if ($this->flag->hasFlag(Flag::FLAG_FINAL)) {
119 1
            return 'final class';
120
        }
121
122 6
        if ($this->flag->hasFlag(Flag::FLAG_ABSTRACT)) {
123 1
            return 'abstract class';
124
        }
125
126 5
        return 'class';
127
    }
128
129
    /**
130
     * @return string
131
     */
132 7
    protected function generateImplements(): string
133
    {
134 7
        if ($this->interfaces === []) {
135 6
            return '';
136
        }
137
138 1
        return ' implements ' . implode(', ', $this->interfaces);
139
    }
140
141
    /**
142
     * @return string
143
     */
144 7
    protected function generateUses(): string
145
    {
146 7
        if ($this->traits === []) {
147 6
            return '';
148
        }
149
150 1
        return $this->generateLine('use ' . implode(', ', $this->traits) . ';', 1, 1);
151
    }
152
}
153