Skip to content

Commit 677ee80

Browse files
committed
Implement strict typing and some other PHP 8 features
1 parent d3e6f68 commit 677ee80

File tree

12 files changed

+401
-770
lines changed

12 files changed

+401
-770
lines changed

src/Builder/BaseBuilder.php

Lines changed: 47 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Twig\Environment;
1414
use Twig\Loader\FilesystemLoader;
1515
use Twig\TwigFilter;
16+
use TwigGenerator\Extension\ExtraFilterExtension;
17+
use TwigGenerator\Extension\PHPPrintExtension;
18+
use TwigGenerator\Extension\TwigPrintExtension;
1619

1720
/**
1821
* @author Cédric Lombardot
@@ -22,225 +25,139 @@ abstract class BaseBuilder implements BuilderInterface
2225
/**
2326
* Default Twig file extension.
2427
*/
25-
const TWIG_EXTENSION = '.php.twig';
28+
final protected const TWIG_EXTENSION = '.php.twig';
2629

27-
/**
28-
* @var \TwigGenerator\Builder\Generator The generator.
29-
*/
30-
protected $generator;
30+
protected Generator $generator;
3131

3232
/**
33-
* @var array A list of template directories.
33+
* @var string[] A list of template directories.
3434
*/
35-
protected $templateDirectories = array();
35+
protected array $templateDirectories = [];
3636

37-
/**
38-
* @var string
39-
*/
40-
protected $templateName;
37+
protected string $templateName = '';
4138

42-
/**
43-
* @var string
44-
*/
45-
protected $outputName;
39+
protected string $outputName = '';
4640

47-
/**
48-
* @var Boolean
49-
*/
50-
protected $mustOverwriteIfExists = false;
41+
protected bool $mustOverwriteIfExists = false;
5142

52-
/**
53-
* @var array
54-
*/
55-
protected $twigFilters = array(
56-
);
43+
protected array $twigFilters = [];
5744

58-
/**
59-
* @var array
60-
*/
61-
protected $variables = array();
45+
protected array $variables = [];
6246

63-
/**
64-
* @var array
65-
*/
66-
protected $twigExtensions = array(
67-
'\\TwigGenerator\\Extension\\PHPPrintExtension',
68-
'\\TwigGenerator\\Extension\\TwigPrintExtension',
69-
'\\TwigGenerator\\Extension\\ExtraFilterExtension',
70-
);
47+
protected array $twigExtensions = [
48+
PHPPrintExtension::class,
49+
TwigPrintExtension::class,
50+
ExtraFilterExtension::class,
51+
];
7152

72-
/**
73-
* Constructor
74-
*/
7553
public function __construct()
7654
{
7755
$this->templateDirectories = $this->getDefaultTemplateDirs();
7856
$this->templateName = $this->getDefaultTemplateName();
7957
}
8058

81-
/**
82-
* {@inheritDoc}
83-
*/
84-
public function setGenerator(Generator $generator)
59+
public function setGenerator(Generator $generator): void
8560
{
8661
$this->generator = $generator;
8762
}
8863

89-
/**
90-
* {@inheritDoc}
91-
*/
92-
public function getGenerator()
64+
public function getGenerator(): Generator
9365
{
9466
return $this->generator;
9567
}
9668

97-
/**
98-
* {@inheritDoc}
99-
*/
100-
public function addTemplateDir($templateDir)
69+
public function addTemplateDir($templateDir): void
10170
{
10271
$this->templateDirectories[$templateDir] = $templateDir;
10372
}
10473

105-
/**
106-
* {@inheritDoc}
107-
*/
108-
public function setTemplateDirs(array $templateDirs)
74+
public function setTemplateDirs(array $templateDirs): void
10975
{
11076
$this->templateDirectories = $templateDirs;
11177
}
11278

113-
/**
114-
* {@inheritDoc}
115-
*/
116-
public function getTemplateDirs()
79+
/** @return string[] */
80+
public function getTemplateDirs(): array
11781
{
11882
return $this->templateDirectories;
11983
}
12084

121-
/**
122-
* {@inheritDoc}
123-
*/
124-
public function getDefaultTemplateDirs()
85+
public function getDefaultTemplateDirs(): array
12586
{
126-
return array();
87+
return [];
12788
}
12889

129-
/**
130-
* {@inheritDoc}
131-
*/
132-
public function setTemplateName($templateName)
90+
public function setTemplateName(string $templateName): void
13391
{
13492
$this->templateName = $templateName;
13593
}
13694

137-
/**
138-
* {@inheritDoc}
139-
*/
140-
public function getTemplateName()
95+
public function getTemplateName(): string
14196
{
14297
return $this->templateName;
14398
}
14499

145-
/**
146-
* {@inheritDoc}
147-
*/
148-
public function getDefaultTemplateName()
100+
public function getDefaultTemplateName(): string
149101
{
150102
return $this->getSimpleClassName() . self::TWIG_EXTENSION;
151103
}
152104

153-
/**
154-
* {@inheritDoc}
155-
*/
156-
public function getSimpleClassName($class = null)
105+
public function getSimpleClassName($class = null): string
157106
{
158107
if (null === $class) {
159-
$class = get_class($this);
108+
$class = self::class;
160109
}
161110

162111
$classParts = explode('\\', $class);
163-
$simpleClassName = array_pop($classParts);
164-
165-
return $simpleClassName;
112+
return array_pop($classParts);
166113
}
167114

168-
/**
169-
* {@inheritDoc}
170-
*/
171-
public function setOutputName($outputName)
115+
public function setOutputName(string $outputName): void
172116
{
173117
$this->outputName = $outputName;
174118
}
175119

176-
/**
177-
* {@inheritDoc}
178-
*/
179-
public function getOutputName()
120+
public function getOutputName(): string
180121
{
181122
return $this->outputName;
182123
}
183124

184-
/**
185-
* {@inheritDoc}
186-
*/
187-
public function mustOverwriteIfExists()
125+
public function mustOverwriteIfExists(): bool
188126
{
189127
return $this->mustOverwriteIfExists;
190128
}
191129

192-
/**
193-
* {@inheritDoc}
194-
*/
195-
public function setMustOverwriteIfExists($status = true)
130+
public function setMustOverwriteIfExists(bool $status = true): void
196131
{
197132
$this->mustOverwriteIfExists = $status;
198133
}
199134

200-
/**
201-
* {@inheritDoc}
202-
*/
203-
public function setVariables(array $variables)
135+
public function setVariables(array $variables): void
204136
{
205137
$this->variables = $variables;
206138
}
207139

208-
/**
209-
* {@inheritDoc}
210-
*/
211-
public function setVariable($key, $value)
140+
public function setVariable(string $key, mixed $value): void
212141
{
213142
$this->variables[$key] = $value;
214143
}
215144

216-
/**
217-
* {@inheritDoc}
218-
*/
219-
public function getVariables()
145+
public function getVariables(): array
220146
{
221147
return $this->variables;
222148
}
223149

224-
/**
225-
* {@inheritDoc}
226-
*/
227-
public function hasVariable($key)
150+
public function hasVariable($key): bool
228151
{
229152
return isset($this->variables[$key]);
230153
}
231154

232-
/**
233-
* {@inheritDoc}
234-
*/
235-
public function getVariable($key, $default = null)
155+
public function getVariable(string $key, mixed $default = null): mixed
236156
{
237157
return $this->hasVariable($key) ? $this->variables[$key] : $default;
238158
}
239159

240-
/**
241-
* {@inheritDoc}
242-
*/
243-
public function writeOnDisk($outputDirectory)
160+
public function writeOnDisk(string $outputDirectory): void
244161
{
245162
$path = $outputDirectory . DIRECTORY_SEPARATOR . $this->getOutputName();
246163
$dir = dirname($path);
@@ -254,10 +171,7 @@ public function writeOnDisk($outputDirectory)
254171
}
255172
}
256173

257-
/**
258-
* {@inheritDoc}
259-
*/
260-
public function getCode()
174+
public function getCode(): string
261175
{
262176
$twig = $this->getTwigEnvironment();
263177
$template = $twig->load($this->getTemplateName());
@@ -268,10 +182,7 @@ public function getCode()
268182
return $template->render($variables);
269183
}
270184

271-
/**
272-
* {@inheritDoc}
273-
*/
274-
public function addTwigFilters(array $filters)
185+
public function addTwigFilters(array $filters): void
275186
{
276187
foreach($filters as $filter) {
277188
if (is_string($filter)) {
@@ -283,10 +194,7 @@ public function addTwigFilters(array $filters)
283194
}
284195
}
285196

286-
/**
287-
* {@inheritDoc}
288-
*/
289-
public function addTwigExtensions(array $extensions)
197+
public function addTwigExtensions(array $extensions): void
290198
{
291199
foreach($extensions as $extension) {
292200
if (is_string($extension)) {
@@ -301,10 +209,8 @@ public function addTwigExtensions(array $extensions)
301209
/**
302210
* Initialize the Twig Environment which automatically loads
303211
* extensions and filters.
304-
*
305-
* @return Environment
306212
*/
307-
protected function getTwigEnvironment()
213+
protected function getTwigEnvironment(): Environment
308214
{
309215
$loader = new FilesystemLoader($this->getTemplateDirs());
310216
$twig = new Environment($loader, array(
@@ -320,7 +226,7 @@ protected function getTwigEnvironment()
320226
return $twig;
321227
}
322228

323-
protected function loadTwigFilters(Environment $twig)
229+
protected function loadTwigFilters(Environment $twig): void
324230
{
325231
foreach ($this->twigFilters as $twigFilter) {
326232
if (is_object($twigFilter)) {
@@ -335,7 +241,7 @@ protected function loadTwigFilters(Environment $twig)
335241
}
336242
}
337243

338-
protected function loadTwigExtensions(Environment $twig)
244+
protected function loadTwigExtensions(Environment $twig): void
339245
{
340246
foreach ($this->twigExtensions as $twigExtensionName) {
341247
if (is_object($twigExtensionName)) {

0 commit comments

Comments
 (0)