Passed
Push — develop ( 481302...fbb092 )
by Paul
02:15
created

PhpFileModel::addTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace PhpUnitGen\Model;
4
5
use PhpUnitGen\Exception\ParseException;
6
use PhpUnitGen\Model\ModelInterface\ClassModelInterface;
7
use PhpUnitGen\Model\ModelInterface\FunctionModelInterface;
8
use PhpUnitGen\Model\ModelInterface\InterfaceModelInterface;
9
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
10
use PhpUnitGen\Model\ModelInterface\TraitModelInterface;
11
use PhpUnitGen\Model\ModelInterface\UseModelInterface;
12
use PhpUnitGen\Model\PropertyTrait\NamespaceTrait;
13
use PhpUnitGen\Model\PropertyTrait\NameTrait;
14
use PhpUnitGen\Model\PropertyTrait\NodeTrait;
15
use Respect\Validation\Validator;
16
17
/**
18
 * Class PhpFileModel.
19
 *
20
 * @author     Paul Thébaud <[email protected]>.
21
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
22
 * @license    https://opensource.org/licenses/MIT The MIT license.
23
 * @link       https://github.com/paul-thebaud/phpunit-generator
24
 * @since      Class available since Release 2.0.0.
25
 */
26
class PhpFileModel implements PhpFileModelInterface
27
{
28
    use NameTrait;
29
    use NamespaceTrait;
30
    use NodeTrait;
31
32
    /**
33
     * This array is constructed with the full name as key, and the class name as a value.
34
     * @var string[] $uses Imports needed for tests skeletons.
35
     */
36
    private $concreteUses = [];
37
38
    /**
39
     * @var UseModelInterface[] $uses Imports contained in the file.
40
     */
41
    private $uses = [];
42
43
    /**
44
     * @var FunctionModelInterface[] $functions Functions contained in the file.
45
     */
46
    private $functions = [];
47
48
    /**
49
     * @var ClassModelInterface[] $classes Classes contained in the file.
50
     */
51
    private $classes = [];
52
53
    /**
54
     * @var TraitModelInterface[] $traits Traits contained in the file.
55
     */
56
    private $traits = [];
57
58
    /**
59
     * @var InterfaceModelInterface[] $interfaces Interfaces contained in the file.
60
     */
61
    private $interfaces = [];
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getFullNameFor(string $name): string
67
    {
68
        $namespace = $this->getNamespaceString();
69
        return $namespace === null? $name : $namespace . '\\' . $name;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function addConcreteUse(string $fullName, string $name): void
76
    {
77
        // Full name exists and concrete use correspond to this one
78
        if (Validator::key($fullName)->validate($this->concreteUses)
79
            && $name === $this->concreteUses[$fullName]
80
        ) {
81
            // Do not add
82
            return;
83
        }
84
85
        // Delete duplicate class name
86
        $iteration = 0;
87
        while (Validator::contains($name)->validate($this->concreteUses)) {
88
            if ($iteration === 0 && Validator::contains($fullName)->validate($this->uses)) {
89
                // If a known alias exists
90
                $name = array_search($fullName, $this->uses);
91
            } else {
92
                // Give a default alias
93
                $name .= 'Alias';
94
            }
95
            $iteration++;
96
        }
97
        $this->concreteUses[$fullName] = $name;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getConcreteUses(): array
104
    {
105
        return $this->concreteUses;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function addUse(string $name, string $fullName): void
112
    {
113
        $this->uses[$name] = $fullName;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function hasUse(string $name): bool
120
    {
121
        return Validator::key($name)->validate($this->uses);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getUse(string $name): string
128
    {
129
        if (! $this->hasUse($name)) {
130
            throw new ParseException(sprintf(
131
                'Trying to get a full class name for "%s", but it does not exists',
132
                $name
133
            ));
134
        }
135
        return $this->uses[$name];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->uses[$name] returns the type PhpUnitGen\Model\ModelInterface\UseModelInterface which is incompatible with the type-hinted return string.
Loading history...
136
    }
137
138
    /**
139
     * Get the index of a use from his name or alias.
140
     *
141
     * @param string $name The name or alias of this use.
142
     *
143
     * @return int|null The index of this use, null if no one corresponding.
144
     */
145
    private function getUseIndex(string $name): ?int
0 ignored issues
show
Unused Code introduced by
The method getUseIndex() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
146
    {
147
        foreach ($this->uses as $key => $use) {
148
            if ($use->getName() === $name) {
149
                return $key;
150
            }
151
            if ($use->getAlias() === $name) {
152
                return $key;
153
            }
154
        }
155
        return null;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function addFunction(FunctionModelInterface $function): void
162
    {
163
        $this->functions[] = $function;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getFunctions(): array
170
    {
171
        return $this->functions;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function addClass(ClassModelInterface $class): void
178
    {
179
        $this->classes[] = $class;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function getClasses(): array
186
    {
187
        return $this->classes;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function addTrait(TraitModelInterface $trait): void
194
    {
195
        $this->traits[] = $trait;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function getTraits(): array
202
    {
203
        return $this->traits;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function addInterface(InterfaceModelInterface $interface): void
210
    {
211
        $this->interfaces[] = $interface;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getInterfaces(): array
218
    {
219
        return $this->interfaces;
220
    }
221
}
222