Skip to content

Commit a5a506c

Browse files
authored
Merge pull request #68 from lisachenko/fix/invalid-type-hint
[Feature] Add/update type information for source files
2 parents c9ca40a + d64a325 commit a5a506c

38 files changed

+501
-458
lines changed

src/DeclareStatement.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class DeclareStatement
2626
self::ENCODING => 'string',
2727
];
2828

29-
/** @var string */
30-
protected $directive;
29+
protected string $directive;
3130

3231
/** @var int|string */
3332
protected $value;

src/Generator/AbstractGenerator.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ abstract class AbstractGenerator implements GeneratorInterface
2424
*/
2525
public const LINE_FEED = "\n";
2626

27-
/** @var bool */
28-
protected $isSourceDirty = true;
27+
protected bool $isSourceDirty = true;
2928

30-
/** @var int|string 4 spaces by default */
31-
protected $indentation = ' ';
29+
/** @var string 4 spaces by default */
30+
protected string $indentation = ' ';
3231

33-
/** @var string */
34-
protected $sourceContent;
32+
/**
33+
* TODO: Type should be changed to "string" in the next major version. Nullable for BC
34+
*/
35+
protected ?string $sourceContent = null;
3536

3637
/**
3738
* @param array $options
@@ -80,7 +81,7 @@ public function getIndentation()
8081
}
8182

8283
/**
83-
* @param string $sourceContent
84+
* @param ?string $sourceContent
8485
* @return AbstractGenerator
8586
*/
8687
public function setSourceContent($sourceContent)
@@ -90,7 +91,7 @@ public function setSourceContent($sourceContent)
9091
}
9192

9293
/**
93-
* @return string
94+
* @return ?string
9495
*/
9596
public function getSourceContent()
9697
{

src/Generator/AbstractMemberGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@ abstract class AbstractMemberGenerator extends AbstractGenerator
2525
public const VISIBILITY_PROTECTED = 'protected';
2626
public const VISIBILITY_PRIVATE = 'private';
2727

28-
/** @var DocBlockGenerator|null */
29-
protected $docBlock;
28+
protected ?DocBlockGenerator $docBlock = null;
3029

31-
/** @var string */
32-
protected $name;
30+
protected string $name = '';
3331

34-
/** @var int */
35-
protected $flags = self::FLAG_PUBLIC;
32+
protected int $flags = self::FLAG_PUBLIC;
3633

3734
/**
38-
* @param int|array $flags
35+
* @param int|int[] $flags
3936
* @return AbstractMemberGenerator
4037
*/
4138
public function setFlags($flags)

src/Generator/BodyGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
class BodyGenerator extends AbstractGenerator
1212
{
13-
/** @var string */
14-
protected $content;
13+
protected string $content = '';
1514

1615
/**
1716
* @param string $content

src/Generator/ClassGenerator.php

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,38 @@ class ClassGenerator extends AbstractGenerator implements TraitUsageInterface
3939
public const FLAG_ABSTRACT = 0x01;
4040
public const FLAG_FINAL = 0x02;
4141

42-
/** @var FileGenerator */
43-
protected $containingFileGenerator;
42+
protected ?FileGenerator $containingFileGenerator = null;
4443

45-
/** @var string */
46-
protected $namespaceName;
44+
protected ?string $namespaceName = null;
4745

48-
/** @var DocBlockGenerator */
49-
protected $docBlock;
46+
protected ?DocBlockGenerator $docBlock = null;
5047

51-
/** @var string */
52-
protected $name;
48+
protected string $name = '';
5349

54-
/** @var bool */
55-
protected $flags = 0x00;
50+
protected int $flags = 0x00;
5651

57-
/** @var string */
58-
protected $extendedClass;
52+
/** @psalm-var ?class-string */
53+
protected ?string $extendedClass = null;
5954

6055
/**
61-
* @var string[] Array of string names
56+
* Array of implemented interface names
57+
*
58+
* @var string[]
6259
* @psalm-var array<class-string>
6360
*/
64-
protected $implementedInterfaces = [];
61+
protected array $implementedInterfaces = [];
6562

6663
/** @var PropertyGenerator[] */
67-
protected $properties = [];
64+
protected array $properties = [];
6865

6966
/** @var PropertyGenerator[] */
70-
protected $constants = [];
67+
protected array $constants = [];
7168

7269
/** @var MethodGenerator[] */
73-
protected $methods = [];
70+
protected array $methods = [];
7471

7572
/** @var TraitUsageGenerator Object to encapsulate trait usage logic */
76-
protected $traitUsageGenerator;
73+
protected TraitUsageGenerator $traitUsageGenerator;
7774

7875
/**
7976
* Build a Code Generation Php Object from a Class Reflection
@@ -109,7 +106,6 @@ public static function fromReflection(ClassReflection $classReflection)
109106

110107
$interfaceNames = [];
111108
foreach ($interfaces as $interface) {
112-
/** @var ClassReflection $interface */
113109
$interfaceNames[] = $interface->getName();
114110
}
115111

@@ -139,7 +135,11 @@ public static function fromReflection(ClassReflection $classReflection)
139135
$methods = [];
140136

141137
foreach ($classReflection->getMethods() as $reflectionMethod) {
142-
$className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName();
138+
$className = $cg->getName();
139+
$namespaceName = $cg->getNamespaceName();
140+
if ($namespaceName !== null) {
141+
$className = $namespaceName . '\\' . $className;
142+
}
143143

144144
if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
145145
$methods[] = MethodGenerator::fromReflection($reflectionMethod);
@@ -211,23 +211,24 @@ public static function fromArray(array $array)
211211
}
212212

213213
/**
214-
* @param string $name
215-
* @param string $namespaceName
216-
* @param array|string $flags
217-
* @param string $extends
218-
* @param array $interfaces
219-
* @param array $properties
220-
* @param array $methods
221-
* @param DocBlockGenerator $docBlock
214+
* @param string $name
215+
* @param string $namespaceName
216+
* @param int|int[]|null $flags
217+
* @param class-string|null $extends
218+
* @param string[] $interfaces
219+
* @psalm-param array<class-string> $interfaces
220+
* @param PropertyGenerator[]|string[]|array[] $properties
221+
* @param MethodGenerator[]|string[]|array[] $methods
222+
* @param DocBlockGenerator $docBlock
222223
*/
223224
public function __construct(
224225
$name = null,
225226
$namespaceName = null,
226227
$flags = null,
227228
$extends = null,
228-
$interfaces = [],
229-
$properties = [],
230-
$methods = [],
229+
array $interfaces = [],
230+
array $properties = [],
231+
array $methods = [],
231232
$docBlock = null
232233
) {
233234
$this->traitUsageGenerator = new TraitUsageGenerator($this);
@@ -247,7 +248,7 @@ public function __construct(
247248
if ($extends !== null) {
248249
$this->setExtendedClass($extends);
249250
}
250-
if (is_array($interfaces)) {
251+
if ($interfaces !== []) {
251252
$this->setImplementedInterfaces($interfaces);
252253
}
253254
if ($methods !== []) {
@@ -283,7 +284,7 @@ public function getName()
283284
}
284285

285286
/**
286-
* @param string $namespaceName
287+
* @param ?string $namespaceName
287288
* @return self
288289
*/
289290
public function setNamespaceName($namespaceName)
@@ -293,7 +294,7 @@ public function setNamespaceName($namespaceName)
293294
}
294295

295296
/**
296-
* @return string
297+
* @return ?string
297298
*/
298299
public function getNamespaceName()
299300
{
@@ -310,7 +311,7 @@ public function setContainingFileGenerator(FileGenerator $fileGenerator)
310311
}
311312

312313
/**
313-
* @return FileGenerator
314+
* @return ?FileGenerator
314315
*/
315316
public function getContainingFileGenerator()
316317
{
@@ -327,15 +328,15 @@ public function setDocBlock(DocBlockGenerator $docBlock)
327328
}
328329

329330
/**
330-
* @return DocBlockGenerator
331+
* @return ?DocBlockGenerator
331332
*/
332333
public function getDocBlock()
333334
{
334335
return $this->docBlock;
335336
}
336337

337338
/**
338-
* @param array|string $flags
339+
* @param int[]|int $flags
339340
* @return self
340341
*/
341342
public function setFlags($flags)
@@ -354,7 +355,7 @@ public function setFlags($flags)
354355
}
355356

356357
/**
357-
* @param string $flag
358+
* @param int $flag
358359
* @return self
359360
*/
360361
public function addFlag($flag)
@@ -364,7 +365,7 @@ public function addFlag($flag)
364365
}
365366

366367
/**
367-
* @param string $flag
368+
* @param int $flag
368369
* @return self
369370
*/
370371
public function removeFlag($flag)
@@ -404,11 +405,12 @@ public function setFinal($isFinal)
404405
*/
405406
public function isFinal()
406407
{
407-
return $this->flags & self::FLAG_FINAL;
408+
return (bool) ($this->flags & self::FLAG_FINAL);
408409
}
409410

410411
/**
411-
* @param string $extendedClass
412+
* @param ?string $extendedClass
413+
* @psalm-param ?class-string $extendedClass
412414
* @return self
413415
*/
414416
public function setExtendedClass($extendedClass)
@@ -418,7 +420,8 @@ public function setExtendedClass($extendedClass)
418420
}
419421

420422
/**
421-
* @return string
423+
* @return ?string
424+
* @psalm-return ?class-string
422425
*/
423426
public function getExtendedClass()
424427
{
@@ -459,7 +462,7 @@ public function setImplementedInterfaces(array $implementedInterfaces)
459462
}
460463

461464
/**
462-
* @return string
465+
* @return string[]
463466
* @psalm-return array<class-string>
464467
*/
465468
public function getImplementedInterfaces()
@@ -469,6 +472,7 @@ public function getImplementedInterfaces()
469472

470473
/**
471474
* @param string $implementedInterface
475+
* @psalm-param class-string $implementedInterface
472476
* @return bool
473477
*/
474478
public function hasImplementedInterface($implementedInterface)
@@ -491,8 +495,8 @@ public function removeImplementedInterface($implementedInterface)
491495
$interfaceType = TypeGenerator::fromTypeString($implementedInterface);
492496

493497
$this->implementedInterfaces = array_filter(
494-
array_map([TypeGenerator::class, 'fromTypeString'], $this->implementedInterfaces),
495-
static fn (TypeGenerator $interface): bool => ! $interfaceType->equals($interface)
498+
$this->implementedInterfaces,
499+
static fn (string $interface): bool => ! TypeGenerator::fromTypeString($interface)->equals($interfaceType)
496500
);
497501

498502
return $this;
@@ -612,20 +616,18 @@ public function addConstants(array $constants)
612616
}
613617

614618
/**
615-
* @param array $properties
619+
* @param PropertyGenerator[]|string[]|array[] $properties
616620
* @return self
617621
*/
618622
public function addProperties(array $properties)
619623
{
620624
foreach ($properties as $property) {
621625
if ($property instanceof PropertyGenerator) {
622626
$this->addPropertyFromGenerator($property);
627+
} elseif (is_string($property)) {
628+
$this->addProperty($property);
623629
} else {
624-
if (is_string($property)) {
625-
$this->addProperty($property);
626-
} elseif (is_array($property)) {
627-
$this->addProperty(...array_values($property));
628-
}
630+
$this->addProperty(...array_values($property));
629631
}
630632
}
631633

@@ -792,20 +794,18 @@ public function hasProperty($propertyName)
792794
}
793795

794796
/**
795-
* @param array $methods
797+
* @param MethodGenerator[]|string[]|array[] $methods
796798
* @return self
797799
*/
798800
public function addMethods(array $methods)
799801
{
800802
foreach ($methods as $method) {
801803
if ($method instanceof MethodGenerator) {
802804
$this->addMethodFromGenerator($method);
805+
} elseif (is_string($method)) {
806+
$this->addMethod($method);
803807
} else {
804-
if (is_string($method)) {
805-
$this->addMethod($method);
806-
} elseif (is_array($method)) {
807-
$this->addMethod(...array_values($method));
808-
}
808+
$this->addMethod(...array_values($method));
809809
}
810810
}
811811

@@ -816,7 +816,7 @@ public function addMethods(array $methods)
816816
* Add Method from scalars
817817
*
818818
* @param string $name
819-
* @param array $parameters
819+
* @param ParameterGenerator[]|array[]|string[] $parameters
820820
* @param int $flags
821821
* @param string $body
822822
* @param string $docBlock

0 commit comments

Comments
 (0)