Skip to content

Commit 9b3e3af

Browse files
committed
added PHP 8 typehints
1 parent c181ffc commit 9b3e3af

20 files changed

+96
-216
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,14 @@ public static function enum(string $name): self
9292
}
9393

9494

95-
/**
96-
* @param string|object $class
97-
*/
98-
public static function from($class, bool $withBodies = false, bool $materializeTraits = true): self
95+
public static function from(string|object $class, bool $withBodies = false, bool $materializeTraits = true): self
9996
{
10097
return (new Factory)
10198
->fromClassReflection(new \ReflectionClass($class), $withBodies, $materializeTraits);
10299
}
103100

104101

105-
/**
106-
* @param string|object $class
107-
*/
108-
public static function withBodiesFrom($class): self
102+
public static function withBodiesFrom(string|object $class): self
109103
{
110104
return (new Factory)
111105
->fromClassReflection(new \ReflectionClass($class), withBodies: true);
@@ -139,8 +133,7 @@ public function getNamespace(): ?PhpNamespace
139133
}
140134

141135

142-
/** @return static */
143-
public function setName(?string $name): self
136+
public function setName(?string $name): static
144137
{
145138
if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::KEYWORDS[strtolower($name)]))) {
146139
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
@@ -158,7 +151,7 @@ public function getName(): ?string
158151

159152

160153
/** @deprecated */
161-
public function setClass(): self
154+
public function setClass(): static
162155
{
163156
$this->type = self::TYPE_CLASS;
164157
return $this;
@@ -171,8 +164,7 @@ public function isClass(): bool
171164
}
172165

173166

174-
/** @return static */
175-
public function setInterface(): self
167+
public function setInterface(): static
176168
{
177169
$this->type = self::TYPE_INTERFACE;
178170
return $this;
@@ -185,8 +177,7 @@ public function isInterface(): bool
185177
}
186178

187179

188-
/** @return static */
189-
public function setTrait(): self
180+
public function setTrait(): static
190181
{
191182
$this->type = self::TYPE_TRAIT;
192183
return $this;
@@ -205,8 +196,7 @@ public function isEnum(): bool
205196
}
206197

207198

208-
/** @return static */
209-
public function setType(string $type): self
199+
public function setType(string $type): static
210200
{
211201
if (!in_array($type, [self::TYPE_CLASS, self::TYPE_INTERFACE, self::TYPE_TRAIT, self::TYPE_ENUM], true)) {
212202
throw new Nette\InvalidArgumentException('Argument must be class|interface|trait|enum.');
@@ -223,8 +213,7 @@ public function getType(): string
223213
}
224214

225215

226-
/** @return static */
227-
public function setFinal(bool $state = true): self
216+
public function setFinal(bool $state = true): static
228217
{
229218
$this->final = $state;
230219
return $this;
@@ -237,8 +226,7 @@ public function isFinal(): bool
237226
}
238227

239228

240-
/** @return static */
241-
public function setAbstract(bool $state = true): self
229+
public function setAbstract(bool $state = true): static
242230
{
243231
$this->abstract = $state;
244232
return $this;
@@ -253,29 +241,23 @@ public function isAbstract(): bool
253241

254242
/**
255243
* @param string|string[] $names
256-
* @return static
257244
*/
258-
public function setExtends($names): self
245+
public function setExtends(string|array $names): static
259246
{
260-
if (!is_string($names) && !is_array($names)) {
261-
throw new Nette\InvalidArgumentException('Argument must be string or string[].');
262-
}
263-
264247
$this->validateNames((array) $names);
265248
$this->extends = $names;
266249
return $this;
267250
}
268251

269252

270253
/** @return string|string[] */
271-
public function getExtends()
254+
public function getExtends(): string|array
272255
{
273256
return $this->extends;
274257
}
275258

276259

277-
/** @return static */
278-
public function addExtend(string $name): self
260+
public function addExtend(string $name): static
279261
{
280262
$this->validateNames([$name]);
281263
$this->extends = (array) $this->extends;
@@ -286,9 +268,8 @@ public function addExtend(string $name): self
286268

287269
/**
288270
* @param string[] $names
289-
* @return static
290271
*/
291-
public function setImplements(array $names): self
272+
public function setImplements(array $names): static
292273
{
293274
$this->validateNames($names);
294275
$this->implements = $names;
@@ -303,17 +284,15 @@ public function getImplements(): array
303284
}
304285

305286

306-
/** @return static */
307-
public function addImplement(string $name): self
287+
public function addImplement(string $name): static
308288
{
309289
$this->validateNames([$name]);
310290
$this->implements[] = $name;
311291
return $this;
312292
}
313293

314294

315-
/** @return static */
316-
public function removeImplement(string $name): self
295+
public function removeImplement(string $name): static
317296
{
318297
$this->implements = array_diff($this->implements, [$name]);
319298
return $this;
@@ -322,10 +301,10 @@ public function removeImplement(string $name): self
322301

323302
/**
324303
* @param string[]|TraitUse[] $traits
325-
* @return static
326304
*/
327-
public function setTraits(array $traits): self
305+
public function setTraits(array $traits): static
328306
{
307+
(function (TraitUse|string ...$traits) {})(...$traits);
329308
$this->traits = [];
330309
foreach ($traits as $trait) {
331310
if (!$trait instanceof TraitUse) {
@@ -353,11 +332,7 @@ public function getTraitResolutions(): array
353332
}
354333

355334

356-
/**
357-
* @param array|bool $resolutions
358-
* @return static|TraitUse
359-
*/
360-
public function addTrait(string $name, $resolutions = [])
335+
public function addTrait(string $name, array|bool $resolutions = []): static|TraitUse
361336
{
362337
$this->traits[$name] = $trait = new TraitUse($name);
363338
if ($resolutions === true) {
@@ -369,37 +344,30 @@ public function addTrait(string $name, $resolutions = [])
369344
}
370345

371346

372-
/** @return static */
373-
public function removeTrait(string $name): self
347+
public function removeTrait(string $name): static
374348
{
375349
unset($this->traits[$name]);
376350
return $this;
377351
}
378352

379353

380-
/**
381-
* @param Method|Property|Constant|EnumCase|TraitUse $member
382-
* @return static
383-
*/
384-
public function addMember($member): self
354+
public function addMember(Method|Property|Constant|EnumCase|TraitUse $member): static
385355
{
386356
match (true) {
387357
$member instanceof Method => $this->methods[strtolower($member->getName())] = $member,
388358
$member instanceof Property => $this->properties[$member->getName()] = $member,
389359
$member instanceof Constant => $this->consts[$member->getName()] = $member,
390360
$member instanceof EnumCase => $this->cases[$member->getName()] = $member,
391361
$member instanceof TraitUse => $this->traits[$member->getName()] = $member,
392-
default => throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant|EnumCase|TraitUse.'),
393362
};
394363
return $this;
395364
}
396365

397366

398367
/**
399368
* @param Constant[]|mixed[] $consts
400-
* @return static
401369
*/
402-
public function setConstants(array $consts): self
370+
public function setConstants(array $consts): static
403371
{
404372
$this->consts = [];
405373
foreach ($consts as $k => $const) {
@@ -429,8 +397,7 @@ public function addConstant(string $name, $value): Constant
429397
}
430398

431399

432-
/** @return static */
433-
public function removeConstant(string $name): self
400+
public function removeConstant(string $name): static
434401
{
435402
unset($this->consts[$name]);
436403
return $this;
@@ -440,9 +407,8 @@ public function removeConstant(string $name): self
440407
/**
441408
* Sets cases to enum
442409
* @param EnumCase[] $cases
443-
* @return static
444410
*/
445-
public function setCases(array $cases): self
411+
public function setCases(array $cases): static
446412
{
447413
(function (EnumCase ...$cases) {})(...array_values($cases));
448414
$this->cases = [];
@@ -462,15 +428,14 @@ public function getCases(): array
462428

463429

464430
/** Adds case to enum */
465-
public function addCase(string $name, $value = null): EnumCase
431+
public function addCase(string $name, string|int|null $value = null): EnumCase
466432
{
467433
return $this->cases[$name] = (new EnumCase($name))
468434
->setValue($value);
469435
}
470436

471437

472-
/** @return static */
473-
public function removeCase(string $name): self
438+
public function removeCase(string $name): static
474439
{
475440
unset($this->cases[$name]);
476441
return $this;
@@ -479,9 +444,8 @@ public function removeCase(string $name): self
479444

480445
/**
481446
* @param Property[] $props
482-
* @return static
483447
*/
484-
public function setProperties(array $props): self
448+
public function setProperties(array $props): static
485449
{
486450
(function (Property ...$props) {})(...array_values($props));
487451
$this->properties = [];
@@ -523,9 +487,8 @@ public function addProperty(string $name, $value = null): Property
523487

524488
/**
525489
* @param string $name without $
526-
* @return static
527490
*/
528-
public function removeProperty(string $name): self
491+
public function removeProperty(string $name): static
529492
{
530493
unset($this->properties[$name]);
531494
return $this;
@@ -540,9 +503,8 @@ public function hasProperty(string $name): bool
540503

541504
/**
542505
* @param Method[] $methods
543-
* @return static
544506
*/
545-
public function setMethods(array $methods): self
507+
public function setMethods(array $methods): static
546508
{
547509
(function (Method ...$methods) {})(...array_values($methods));
548510
$this->methods = [];
@@ -588,8 +550,7 @@ public function addMethod(string $name): Method
588550
}
589551

590552

591-
/** @return static */
592-
public function removeMethod(string $name): self
553+
public function removeMethod(string $name): static
593554
{
594555
unset($this->methods[strtolower($name)]);
595556
return $this;

src/PhpGenerator/Closure.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ public function __toString(): string
4141

4242
/**
4343
* @param Parameter[] $uses
44-
* @return static
4544
*/
46-
public function setUses(array $uses): self
45+
public function setUses(array $uses): static
4746
{
4847
(function (Parameter ...$uses) {})(...$uses);
4948
$this->uses = $uses;

src/PhpGenerator/Constant.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,20 @@ final class Constant
2727
private bool $final = false;
2828

2929

30-
/** @return static */
31-
public function setValue($val): self
30+
public function setValue($val): static
3231
{
3332
$this->value = $val;
3433
return $this;
3534
}
3635

3736

38-
public function getValue()
37+
public function getValue(): mixed
3938
{
4039
return $this->value;
4140
}
4241

4342

44-
/** @return static */
45-
public function setFinal(bool $state = true): self
43+
public function setFinal(bool $state = true): static
4644
{
4745
$this->final = $state;
4846
return $this;

src/PhpGenerator/EnumCase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ final class EnumCase
2525
private string|int|null $value = null;
2626

2727

28-
/** @return static */
29-
public function setValue($val): self
28+
public function setValue(string|int|null $val): static
3029
{
3130
$this->value = $val;
3231
return $this;

src/PhpGenerator/Extractor.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private function addConstantToClass(ClassType $class, Node\Stmt\ClassConst $node
347347
}
348348

349349

350-
private function addEnumCaseToClass(ClassType $class, Node\Stmt\EnumCase $node)
350+
private function addEnumCaseToClass(ClassType $class, Node\Stmt\EnumCase $node): void
351351
{
352352
$case = $class->addCase($node->name->toString(), $node->expr?->value);
353353
$this->addCommentAndAttributes($case, $node);
@@ -381,10 +381,7 @@ private function addCommentAndAttributes($element, Node $node): void
381381
}
382382

383383

384-
/**
385-
* @param GlobalFunction|Method $function
386-
*/
387-
private function setupFunction($function, Node\FunctionLike $node): void
384+
private function setupFunction(GlobalFunction|Method $function, Node\FunctionLike $node): void
388385
{
389386
$function->setReturnReference($node->returnsByRef());
390387
$function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
@@ -407,7 +404,7 @@ private function setupFunction($function, Node\FunctionLike $node): void
407404
}
408405

409406

410-
private function toPhp($value): string
407+
private function toPhp(mixed $value): string
411408
{
412409
return $this->printer->prettyPrint([$value]);
413410
}

0 commit comments

Comments
 (0)