Completed
Push — develop ( 53b1d2...e700e7 )
by Paul
03:00
created

PhpFileModel::getFullNameFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\PropertyTrait\NamespaceTrait;
12
use PhpUnitGen\Model\PropertyTrait\NameTrait;
13
use PhpUnitGen\Model\PropertyTrait\NodeTrait;
14
use Respect\Validation\Validator;
15
16
/**
17
 * Class PhpFileModel.
18
 *
19
 * @author     Paul Thébaud <[email protected]>.
20
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
21
 * @license    https://opensource.org/licenses/MIT The MIT license.
22
 * @link       https://github.com/paul-thebaud/phpunit-generator
23
 * @since      Class available since Release 2.0.0.
24
 */
25
class PhpFileModel implements PhpFileModelInterface
26
{
27
    use NameTrait;
28
    use NamespaceTrait;
29
    use NodeTrait;
30
31
    /**
32
     * This array is constructed with the full name as key, and the class name as a value.
33
     * @var string[] $uses Imports needed for tests skeletons.
34
     */
35
    private $concreteUses = [];
36
37
    /**
38
     * This array is constructed with the name or the alias as key, and the real namespace, full name as a value.
39
     * @var string[] $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
        if (Validator::contains($name)->validate($this->concreteUses)) {
78
            throw new ParseException(sprintf(
79
                'It seems you import two times the class "%s" in your code',
80
                $fullName
81
            ));
82
        }
83
        $this->concreteUses[$fullName] = $name;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getConcreteUses(): array
90
    {
91
        return $this->concreteUses;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function addUse(string $name, string $fullName): void
98
    {
99
        $this->uses[$name] = $fullName;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function hasUse(string $name): bool
106
    {
107
        return isset($this->uses[$name]);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getFullNameUse(string $name): ?string
114
    {
115
        if ($this->hasUse($name)) {
116
            return $this->uses[$name];
117
        }
118
        return null;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getUses(): array
125
    {
126
        return $this->uses;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function addFunction(FunctionModelInterface $function): void
133
    {
134
        $this->functions[] = $function;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getFunctions(): array
141
    {
142
        return $this->functions;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function addClass(ClassModelInterface $class): void
149
    {
150
        $this->classes[] = $class;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getClasses(): array
157
    {
158
        return $this->classes;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function addTrait(TraitModelInterface $trait): void
165
    {
166
        $this->traits[] = $trait;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getTraits(): array
173
    {
174
        return $this->traits;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function addInterface(InterfaceModelInterface $interface): void
181
    {
182
        $this->interfaces[] = $interface;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function getInterfaces(): array
189
    {
190
        return $this->interfaces;
191
    }
192
}
193