diff --git a/src/DeclareStatement.php b/src/DeclareStatement.php index 9e6f678a..faf55ee6 100644 --- a/src/DeclareStatement.php +++ b/src/DeclareStatement.php @@ -26,8 +26,7 @@ class DeclareStatement self::ENCODING => 'string', ]; - /** @var string */ - protected $directive; + protected string $directive; /** @var int|string */ protected $value; diff --git a/src/Generator/AbstractGenerator.php b/src/Generator/AbstractGenerator.php index d5920d58..cd35c7db 100644 --- a/src/Generator/AbstractGenerator.php +++ b/src/Generator/AbstractGenerator.php @@ -24,14 +24,12 @@ abstract class AbstractGenerator implements GeneratorInterface */ public const LINE_FEED = "\n"; - /** @var bool */ - protected $isSourceDirty = true; + protected bool $isSourceDirty = true; - /** @var int|string 4 spaces by default */ - protected $indentation = ' '; + /** @var string 4 spaces by default */ + protected string $indentation = ' '; - /** @var string */ - protected $sourceContent; + protected string $sourceContent = ''; /** * @param array $options diff --git a/src/Generator/AbstractMemberGenerator.php b/src/Generator/AbstractMemberGenerator.php index 0d18a148..d2f05762 100644 --- a/src/Generator/AbstractMemberGenerator.php +++ b/src/Generator/AbstractMemberGenerator.php @@ -25,17 +25,14 @@ abstract class AbstractMemberGenerator extends AbstractGenerator public const VISIBILITY_PROTECTED = 'protected'; public const VISIBILITY_PRIVATE = 'private'; - /** @var DocBlockGenerator|null */ - protected $docBlock; + protected ?DocBlockGenerator $docBlock = null; - /** @var string */ - protected $name; + protected string $name = ''; - /** @var int */ - protected $flags = self::FLAG_PUBLIC; + protected int $flags = self::FLAG_PUBLIC; /** - * @param int|array $flags + * @param int|int[] $flags * @return AbstractMemberGenerator */ public function setFlags($flags) diff --git a/src/Generator/BodyGenerator.php b/src/Generator/BodyGenerator.php index db7c1ffa..51c209a9 100644 --- a/src/Generator/BodyGenerator.php +++ b/src/Generator/BodyGenerator.php @@ -10,8 +10,7 @@ class BodyGenerator extends AbstractGenerator { - /** @var string */ - protected $content; + protected string $content = ''; /** * @param string $content diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index 30083074..5885f5ed 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -39,41 +39,38 @@ class ClassGenerator extends AbstractGenerator implements TraitUsageInterface public const FLAG_ABSTRACT = 0x01; public const FLAG_FINAL = 0x02; - /** @var FileGenerator */ - protected $containingFileGenerator; + protected ?FileGenerator $containingFileGenerator = null; - /** @var string */ - protected $namespaceName; + protected ?string $namespaceName = null; - /** @var DocBlockGenerator */ - protected $docBlock; + protected ?DocBlockGenerator $docBlock = null; - /** @var string */ - protected $name; + protected string $name = ''; - /** @var bool */ - protected $flags = 0x00; + protected int $flags = 0x00; - /** @var string */ - protected $extendedClass; + /** @psalm-var ?class-string */ + protected ?string $extendedClass = null; /** - * @var string[] Array of string names + * Array of implemented interface names + * + * @var string[] * @psalm-var array */ - protected $implementedInterfaces = []; + protected array $implementedInterfaces = []; /** @var PropertyGenerator[] */ - protected $properties = []; + protected array $properties = []; /** @var PropertyGenerator[] */ - protected $constants = []; + protected array $constants = []; /** @var MethodGenerator[] */ - protected $methods = []; + protected array $methods = []; /** @var TraitUsageGenerator Object to encapsulate trait usage logic */ - protected $traitUsageGenerator; + protected TraitUsageGenerator $traitUsageGenerator; /** * Build a Code Generation Php Object from a Class Reflection @@ -109,7 +106,6 @@ public static function fromReflection(ClassReflection $classReflection) $interfaceNames = []; foreach ($interfaces as $interface) { - /** @var ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } @@ -139,7 +135,11 @@ public static function fromReflection(ClassReflection $classReflection) $methods = []; foreach ($classReflection->getMethods() as $reflectionMethod) { - $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName(); + $className = $cg->getName(); + $namespaceName = $cg->getNamespaceName(); + if ($namespaceName !== null) { + $className = $namespaceName . '\\' . $className; + } if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); @@ -211,23 +211,24 @@ public static function fromArray(array $array) } /** - * @param string $name - * @param string $namespaceName - * @param array|string $flags - * @param string $extends - * @param array $interfaces - * @param array $properties - * @param array $methods - * @param DocBlockGenerator $docBlock + * @param string $name + * @param string $namespaceName + * @param int|int[]|null $flags + * @param class-string|null $extends + * @param string[] $interfaces + * @psalm-param array $interfaces + * @param PropertyGenerator[]|string[]|array[] $properties + * @param MethodGenerator[]|string[]|array[] $methods + * @param DocBlockGenerator $docBlock */ public function __construct( $name = null, $namespaceName = null, $flags = null, $extends = null, - $interfaces = [], - $properties = [], - $methods = [], + array $interfaces = [], + array $properties = [], + array $methods = [], $docBlock = null ) { $this->traitUsageGenerator = new TraitUsageGenerator($this); @@ -247,7 +248,7 @@ public function __construct( if ($extends !== null) { $this->setExtendedClass($extends); } - if (is_array($interfaces)) { + if ($interfaces !== []) { $this->setImplementedInterfaces($interfaces); } if ($methods !== []) { @@ -283,7 +284,7 @@ public function getName() } /** - * @param string $namespaceName + * @param ?string $namespaceName * @return self */ public function setNamespaceName($namespaceName) @@ -293,7 +294,7 @@ public function setNamespaceName($namespaceName) } /** - * @return string + * @return ?string */ public function getNamespaceName() { @@ -310,7 +311,7 @@ public function setContainingFileGenerator(FileGenerator $fileGenerator) } /** - * @return FileGenerator + * @return ?FileGenerator */ public function getContainingFileGenerator() { @@ -327,7 +328,7 @@ public function setDocBlock(DocBlockGenerator $docBlock) } /** - * @return DocBlockGenerator + * @return ?DocBlockGenerator */ public function getDocBlock() { @@ -335,7 +336,7 @@ public function getDocBlock() } /** - * @param array|string $flags + * @param int[]|int $flags * @return self */ public function setFlags($flags) @@ -354,7 +355,7 @@ public function setFlags($flags) } /** - * @param string $flag + * @param int $flag * @return self */ public function addFlag($flag) @@ -364,7 +365,7 @@ public function addFlag($flag) } /** - * @param string $flag + * @param int $flag * @return self */ public function removeFlag($flag) @@ -404,11 +405,12 @@ public function setFinal($isFinal) */ public function isFinal() { - return $this->flags & self::FLAG_FINAL; + return (bool) ($this->flags & self::FLAG_FINAL); } /** - * @param string $extendedClass + * @param ?string $extendedClass + * @psalm-param ?class-string $extendedClass * @return self */ public function setExtendedClass($extendedClass) @@ -418,7 +420,8 @@ public function setExtendedClass($extendedClass) } /** - * @return string + * @return ?string + * @psalm-return ?class-string */ public function getExtendedClass() { @@ -459,7 +462,7 @@ public function setImplementedInterfaces(array $implementedInterfaces) } /** - * @return string + * @return string[] * @psalm-return array */ public function getImplementedInterfaces() @@ -469,6 +472,7 @@ public function getImplementedInterfaces() /** * @param string $implementedInterface + * @psalm-param class-string $implementedInterface * @return bool */ public function hasImplementedInterface($implementedInterface) @@ -491,8 +495,8 @@ public function removeImplementedInterface($implementedInterface) $interfaceType = TypeGenerator::fromTypeString($implementedInterface); $this->implementedInterfaces = array_filter( - array_map([TypeGenerator::class, 'fromTypeString'], $this->implementedInterfaces), - static fn (TypeGenerator $interface): bool => ! $interfaceType->equals($interface) + $this->implementedInterfaces, + static fn (string $interface): bool => ! TypeGenerator::fromTypeString($interface)->equals($interfaceType) ); return $this; @@ -612,7 +616,7 @@ public function addConstants(array $constants) } /** - * @param array $properties + * @param PropertyGenerator[]|string[]|array[] $properties * @return self */ public function addProperties(array $properties) @@ -620,12 +624,10 @@ public function addProperties(array $properties) foreach ($properties as $property) { if ($property instanceof PropertyGenerator) { $this->addPropertyFromGenerator($property); + } elseif (is_string($property)) { + $this->addProperty($property); } else { - if (is_string($property)) { - $this->addProperty($property); - } elseif (is_array($property)) { - $this->addProperty(...array_values($property)); - } + $this->addProperty(...array_values($property)); } } @@ -792,7 +794,7 @@ public function hasProperty($propertyName) } /** - * @param array $methods + * @param MethodGenerator[]|string[]|array[] $methods * @return self */ public function addMethods(array $methods) @@ -800,12 +802,10 @@ public function addMethods(array $methods) foreach ($methods as $method) { if ($method instanceof MethodGenerator) { $this->addMethodFromGenerator($method); + } elseif (is_string($method)) { + $this->addMethod($method); } else { - if (is_string($method)) { - $this->addMethod($method); - } elseif (is_array($method)) { - $this->addMethod(...array_values($method)); - } + $this->addMethod(...array_values($method)); } } @@ -816,7 +816,7 @@ public function addMethods(array $methods) * Add Method from scalars * * @param string $name - * @param array $parameters + * @param ParameterGenerator[]|array[]|string[] $parameters * @param int $flags * @param string $body * @param string $docBlock diff --git a/src/Generator/DocBlockGenerator.php b/src/Generator/DocBlockGenerator.php index 4728c84a..398eb18d 100644 --- a/src/Generator/DocBlockGenerator.php +++ b/src/Generator/DocBlockGenerator.php @@ -23,23 +23,17 @@ class DocBlockGenerator extends AbstractGenerator { - /** @var string */ - protected $shortDescription; + protected string $shortDescription = ''; - /** @var string */ - protected $longDescription; + protected string $longDescription = ''; - /** @var array */ - protected $tags = []; + protected array $tags = []; - /** @var string */ - protected $indentation = ''; + protected string $indentation = ''; - /** @var bool */ - protected $wordwrap = true; + protected bool $wordwrap = true; - /** @var TagManager|null */ - protected static $tagManager; + protected static ?TagManager $tagManager = null; /** * Build a DocBlock generator object from a reflection object @@ -108,9 +102,9 @@ protected static function getTagManager() } /** - * @param string $shortDescription - * @param string $longDescription - * @param array $tags + * @param ?string $shortDescription + * @param ?string $longDescription + * @param array[]|TagInterface[] $tags */ public function __construct($shortDescription = null, $longDescription = null, array $tags = []) { @@ -120,7 +114,7 @@ public function __construct($shortDescription = null, $longDescription = null, a if ($longDescription) { $this->setLongDescription($longDescription); } - if (is_array($tags) && $tags) { + if ($tags) { $this->setTags($tags); } } @@ -162,7 +156,7 @@ public function getLongDescription() } /** - * @param array $tags + * @param array[]|TagInterface[] $tags * @return DocBlockGenerator */ public function setTags(array $tags) diff --git a/src/Generator/FileGenerator.php b/src/Generator/FileGenerator.php index 78a4d051..0afaa749 100644 --- a/src/Generator/FileGenerator.php +++ b/src/Generator/FileGenerator.php @@ -45,38 +45,36 @@ class FileGenerator extends AbstractGenerator { - /** @var string */ - protected $filename; + protected string $filename = ''; - /** @var DocBlockGenerator */ - protected $docBlock; + protected ?DocBlockGenerator $docBlock = null; - /** @var array */ - protected $requiredFiles = []; + /** @var string[] */ + protected array $requiredFiles = []; + + protected string $namespace = ''; + + /** @psalm-var list */ + protected array $uses = []; - /** @var string */ - protected $namespace; - /** - * @var array - * @psalm-var list - */ - protected $uses = []; /** * @var ClassGenerator[] * @psalm-var array */ - protected $classes = []; + protected array $classes = []; - /** @var string */ - protected $body; + protected string $body = ''; - /** @var DeclareStatement[] */ - protected $declares = []; + /** + * @var DeclareStatement[] + * @psalm-var array + */ + protected array $declares = []; /** * Passes $options to {@link setOptions()}. * - * @param array|Traversable $options + * @param array|Traversable|null $options */ public function __construct($options = null) { @@ -150,7 +148,7 @@ public function setDocBlock($docBlock) } /** - * @return DocBlockGenerator + * @return ?DocBlockGenerator */ public function getDocBlock() { @@ -158,7 +156,7 @@ public function getDocBlock() } /** - * @param array $requiredFiles + * @param string[] $requiredFiles * @return FileGenerator */ public function setRequiredFiles(array $requiredFiles) @@ -168,7 +166,7 @@ public function setRequiredFiles(array $requiredFiles) } /** - * @return array + * @return string[] */ public function getRequiredFiles() { @@ -258,7 +256,7 @@ public function setUse($use, $as = null) } /** - * @param array $classes + * @param array[]|string[]|ClassGenerator[] $classes * @return FileGenerator */ public function setClasses(array $classes) @@ -364,10 +362,14 @@ public function getBody() return $this->body; } - /** @return static */ + /** + * @param DeclareStatement[] $declares + * @return static + */ public function setDeclares(array $declares) { foreach ($declares as $declare) { + /** @psalm-suppress DocblockTypeContradiction $declare should be always DeclareStatement */ if (! $declare instanceof DeclareStatement) { throw new InvalidArgumentException(sprintf( '%s is expecting an array of %s objects', diff --git a/src/Generator/InterfaceGenerator.php b/src/Generator/InterfaceGenerator.php index 77d91bf3..cde3a214 100644 --- a/src/Generator/InterfaceGenerator.php +++ b/src/Generator/InterfaceGenerator.php @@ -50,9 +50,11 @@ public static function fromReflection(ClassReflection $classReflection) } foreach ($classReflection->getMethods() as $reflectionMethod) { - $className = $cg->getNamespaceName() - ? $cg->getNamespaceName() . '\\' . $cg->getName() - : $cg->getName(); + $className = $cg->getName(); + $namespaceName = $cg->getNamespaceName(); + if ($namespaceName !== null) { + $className = $namespaceName . '\\' . $className; + } if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); diff --git a/src/Generator/MethodGenerator.php b/src/Generator/MethodGenerator.php index f067f89c..9c20d3a1 100644 --- a/src/Generator/MethodGenerator.php +++ b/src/Generator/MethodGenerator.php @@ -24,20 +24,16 @@ class MethodGenerator extends AbstractMemberGenerator { - /** @var DocBlockGenerator */ - protected $docBlock; + protected ?DocBlockGenerator $docBlock = null; /** @var ParameterGenerator[] */ - protected $parameters = []; + protected array $parameters = []; - /** @var string */ - protected $body; + protected string $body = ''; - /** @var null|TypeGenerator */ - private $returnType; + private ?TypeGenerator $returnType = null; - /** @var bool */ - private $returnsReference = false; + private bool $returnsReference = false; /** * @return MethodGenerator @@ -186,11 +182,11 @@ public static function fromArray(array $array) } /** - * @param string $name - * @param array $parameters - * @param int $flags - * @param string $body - * @param DocBlockGenerator|string $docBlock + * @param ?string $name + * @param ParameterGenerator[]|array[]|string[] $parameters + * @param int|int[] $flags + * @param ?string $body + * @param DocBlockGenerator|string|null $docBlock */ public function __construct( $name = null, @@ -217,7 +213,7 @@ public function __construct( } /** - * @param array $parameters + * @param ParameterGenerator[]|array[]|string[] $parameters * @return MethodGenerator */ public function setParameters(array $parameters) diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index 0a1b52d4..9e6b0420 100644 --- a/src/Generator/ParameterGenerator.php +++ b/src/Generator/ParameterGenerator.php @@ -16,26 +16,19 @@ class ParameterGenerator extends AbstractGenerator { - /** @var string */ - protected $name; + protected string $name = ''; - /** @var TypeGenerator|null */ - protected $type; + protected ?TypeGenerator $type = null; - /** @var ValueGenerator */ - protected $defaultValue; + protected ?ValueGenerator $defaultValue = null; - /** @var int */ - protected $position; + protected int $position = 0; - /** @var bool */ - protected $passedByReference = false; + protected bool $passedByReference = false; - /** @var bool */ - private $variadic = false; + private bool $variadic = false; - /** @var bool */ - private $omitDefaultValue = false; + private bool $omitDefaultValue = false; /** * @return ParameterGenerator @@ -128,10 +121,10 @@ public static function fromArray(array $array) } /** - * @param string $name - * @param string $type - * @param mixed $defaultValue - * @param int $position + * @param ?string $name + * @param ?string $type + * @param ?mixed $defaultValue + * @param ?int $position * @param bool $passByReference */ public function __construct( @@ -216,7 +209,7 @@ public function setDefaultValue($defaultValue) } /** - * @return ValueGenerator + * @return ?ValueGenerator */ public function getDefaultValue() { diff --git a/src/Generator/PropertyGenerator.php b/src/Generator/PropertyGenerator.php index 42095d35..9ca6503d 100644 --- a/src/Generator/PropertyGenerator.php +++ b/src/Generator/PropertyGenerator.php @@ -18,14 +18,11 @@ class PropertyGenerator extends AbstractMemberGenerator { public const FLAG_CONSTANT = 0x08; - /** @var bool */ - protected $isConst; + protected bool $isConst = false; - /** @var PropertyValueGenerator */ - protected $defaultValue; + protected ?PropertyValueGenerator $defaultValue = null; - /** @var bool */ - private $omitDefaultValue = false; + private bool $omitDefaultValue = false; /** * @return static @@ -128,8 +125,8 @@ public static function fromArray(array $array) } /** - * @param string $name - * @param PropertyValueGenerator|string|array $defaultValue + * @param ?string $name + * @param PropertyValueGenerator|string|array|null $defaultValue * @param int $flags */ public function __construct($name = null, $defaultValue = null, $flags = self::FLAG_PUBLIC) @@ -189,7 +186,7 @@ public function setDefaultValue( } /** - * @return PropertyValueGenerator + * @return ?PropertyValueGenerator */ public function getDefaultValue() { diff --git a/src/Generator/PropertyValueGenerator.php b/src/Generator/PropertyValueGenerator.php index db10bb38..c65e219c 100644 --- a/src/Generator/PropertyValueGenerator.php +++ b/src/Generator/PropertyValueGenerator.php @@ -10,8 +10,7 @@ class PropertyValueGenerator extends ValueGenerator { - /** @var int */ - protected $arrayDepth = 1; + protected int $arrayDepth = 1; /** * @return string diff --git a/src/Generator/TraitGenerator.php b/src/Generator/TraitGenerator.php index 0e6c59fc..0caa3f19 100644 --- a/src/Generator/TraitGenerator.php +++ b/src/Generator/TraitGenerator.php @@ -49,9 +49,11 @@ public static function fromReflection(ClassReflection $classReflection) $methods = []; foreach ($classReflection->getMethods() as $reflectionMethod) { - $className = $cg->getNamespaceName() - ? $cg->getNamespaceName() . '\\' . $cg->getName() - : $cg->getName(); + $className = $cg->getName(); + $namespaceName = $cg->getNamespaceName(); + if ($namespaceName !== null) { + $className = $namespaceName . '\\' . $className; + } if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } @@ -109,8 +111,8 @@ public static function fromArray(array $array) } /** - * @param array|string $flags - * @return self + * @inheritDoc + * @param int[]|int $flags */ public function setFlags($flags) { @@ -118,7 +120,7 @@ public function setFlags($flags) } /** - * @param string $flag + * @param int $flag * @return self */ public function addFlag($flag) @@ -127,7 +129,7 @@ public function addFlag($flag) } /** - * @param string $flag + * @param int $flag * @return self */ public function removeFlag($flag) @@ -136,8 +138,7 @@ public function removeFlag($flag) } /** - * @param bool $isFinal - * @return self + * @inheritDoc */ public function setFinal($isFinal) { @@ -145,7 +146,7 @@ public function setFinal($isFinal) } /** - * @param string $extendedClass + * @param ?string $extendedClass * @return self */ public function setExtendedClass($extendedClass) @@ -154,8 +155,7 @@ public function setExtendedClass($extendedClass) } /** - * @param array $implementedInterfaces - * @return self + * @inheritDoc */ public function setImplementedInterfaces(array $implementedInterfaces) { @@ -163,8 +163,7 @@ public function setImplementedInterfaces(array $implementedInterfaces) } /** - * @param bool $isAbstract - * @return self + * @inheritDoc */ public function setAbstract($isAbstract) { diff --git a/src/Generator/TraitUsageGenerator.php b/src/Generator/TraitUsageGenerator.php index 79896f1b..39cdee30 100644 --- a/src/Generator/TraitUsageGenerator.php +++ b/src/Generator/TraitUsageGenerator.php @@ -26,20 +26,19 @@ class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterface { - /** @var ClassGenerator */ - protected $classGenerator; + protected ClassGenerator $classGenerator; - /** @var array Array of trait names */ - protected $traits = []; + /** @psalm-var array Array of trait names */ + protected array $traits = []; /** @var array Array of trait aliases */ - protected $traitAliases = []; + protected array $traitAliases = []; /** @var array Array of trait overrides */ - protected $traitOverrides = []; + protected array $traitOverrides = []; /** @var array Array of string names */ - protected $uses = []; + protected array $uses = []; public function __construct(ClassGenerator $classGenerator) { @@ -166,7 +165,6 @@ public function removeUseAlias($use) */ public function addTrait($trait) { - $traitName = $trait; if (is_array($trait)) { if (! array_key_exists('traitName', $trait)) { throw new Exception\InvalidArgumentException('Missing required value for traitName'); @@ -184,6 +182,8 @@ public function addTrait($trait) $this->addTraitOverride($insteadof); } } + } else { + $traitName = $trait; } if (! $this->hasTrait($traitName)) { diff --git a/src/Generator/TraitUsageInterface.php b/src/Generator/TraitUsageInterface.php index c199d7d3..9ac25fd8 100644 --- a/src/Generator/TraitUsageInterface.php +++ b/src/Generator/TraitUsageInterface.php @@ -41,7 +41,8 @@ public function getUses(); * key: method value: @see self::addTraitOverride * key: traitToReplace value: @see self::addTraitOverride * - * @param mixed $trait String | Array + * @param string|array $trait + * @psalm-param string|array{traitName: string, aliases?: array, insteadof?: array} $trait * @return self */ public function addTrait($trait); @@ -51,6 +52,7 @@ public function addTrait($trait); * configurations * * @param array $traits Array of string names or configurations (@see addTrait) + * @psalm-param list $traits * @return self */ public function addTraits(array $traits); diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index e77a79b3..0c3862bf 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -59,17 +59,14 @@ class ValueGenerator extends AbstractGenerator /** @var mixed */ protected $value; - /** @var string */ - protected $type = self::TYPE_AUTO; + protected string $type = self::TYPE_AUTO; - /** @var int */ - protected $arrayDepth = 0; + protected int $arrayDepth = 0; - /** @var string */ - protected $outputMode = self::OUTPUT_MULTIPLE_LINE; + protected string $outputMode = self::OUTPUT_MULTIPLE_LINE; /** @var array */ - protected $allowedTypes; + protected array $allowedTypes = []; /** * Autodetectable constants diff --git a/test/Generator/ClassGeneratorTest.php b/test/Generator/ClassGeneratorTest.php index 62eefaf4..8220bbb1 100644 --- a/test/Generator/ClassGeneratorTest.php +++ b/test/Generator/ClassGeneratorTest.php @@ -20,7 +20,9 @@ use LaminasTest\Code\TestAsset\FooClass; use PHPUnit\Framework\TestCase; use ReflectionMethod; +use Serializable; use stdClass; +use Throwable; use function current; use function fclose; @@ -97,21 +99,21 @@ public function testImplementedInterfacesAccessors() public function testHasImplementedInterface() { $classGenerator = new ClassGenerator(); - $classGenerator->setImplementedInterfaces(['Class1', 'Class2']); + $classGenerator->setImplementedInterfaces([Throwable::class, Serializable::class]); - self::assertTrue($classGenerator->hasImplementedInterface('Class1')); + self::assertTrue($classGenerator->hasImplementedInterface(Throwable::class)); } public function testRemoveImplementedInterface() { $classGenerator = new ClassGenerator(); - $classGenerator->setImplementedInterfaces(['Class1', 'Class2']); + $classGenerator->setImplementedInterfaces([Throwable::class, Serializable::class]); - self::assertTrue($classGenerator->hasImplementedInterface('Class1')); + self::assertTrue($classGenerator->hasImplementedInterface(Throwable::class)); - $classGenerator->removeImplementedInterface('Class1'); - self::assertFalse($classGenerator->hasImplementedInterface('Class1')); - self::assertTrue($classGenerator->hasImplementedInterface('Class2')); + $classGenerator->removeImplementedInterface(Throwable::class); + self::assertFalse($classGenerator->hasImplementedInterface(Throwable::class)); + self::assertTrue($classGenerator->hasImplementedInterface(Serializable::class)); } public function testPropertyAccessors() @@ -596,7 +598,10 @@ public function testCanAddConstant() self::assertInstanceOf(PropertyGenerator::class, $constant); self::assertTrue($constant->isConst()); - self::assertEquals('value', $constant->getDefaultValue()->getValue()); + + $defaultValue = $constant->getDefaultValue(); + self::assertNotNull($defaultValue); + self::assertEquals('value', $defaultValue->getValue()); } /** @@ -612,8 +617,13 @@ public function testCanAddConstantsWithArrayOfGenerators() ]); self::assertCount(2, $classGenerator->getConstants()); - self::assertEquals('value1', $classGenerator->getConstant('x')->getDefaultValue()->getValue()); - self::assertEquals('value2', $classGenerator->getConstant('y')->getDefaultValue()->getValue()); + $valueX = $classGenerator->getConstant('x')->getDefaultValue(); + self::assertNotNull($valueX); + self::assertEquals('value1', $valueX->getValue()); + + $valueY = $classGenerator->getConstant('y')->getDefaultValue(); + self::assertNotNull($valueY); + self::assertEquals('value2', $valueY->getValue()); } /** @@ -629,8 +639,14 @@ public function testCanAddConstantsWithArrayOfKeyValues() ]); self::assertCount(2, $classGenerator->getConstants()); - self::assertEquals('value1', $classGenerator->getConstant('x')->getDefaultValue()->getValue()); - self::assertEquals('value2', $classGenerator->getConstant('y')->getDefaultValue()->getValue()); + + $valueX = $classGenerator->getConstant('x')->getDefaultValue(); + self::assertNotNull($valueX); + self::assertEquals('value1', $valueX->getValue()); + + $valueY = $classGenerator->getConstant('y')->getDefaultValue(); + self::assertNotNull($valueY); + self::assertEquals('value2', $valueY->getValue()); } /** @@ -664,13 +680,33 @@ public function testAddConstantAcceptsMixedScalars() $classGenerator->addConstant('f', ['v1' => ['v2' => 'v3']]); $classGenerator->addConstant('g', null); - self::assertEquals('v', $classGenerator->getConstant('a')->getDefaultValue()->getValue()); - self::assertEquals(123, $classGenerator->getConstant('b')->getDefaultValue()->getValue()); - self::assertEquals(123.456, $classGenerator->getConstant('c')->getDefaultValue()->getValue()); - self::assertEquals([], $classGenerator->getConstant('d')->getDefaultValue()->getValue()); - self::assertEquals(['v1' => 'v2'], $classGenerator->getConstant('e')->getDefaultValue()->getValue()); - self::assertEquals(['v1' => ['v2' => 'v3']], $classGenerator->getConstant('f')->getDefaultValue()->getValue()); - self::assertEquals(null, $classGenerator->getConstant('g')->getDefaultValue()->getValue()); + $valueA = $classGenerator->getConstant('a')->getDefaultValue(); + self::assertNotNull($valueA); + self::assertEquals('v', $valueA->getValue()); + + $valueB = $classGenerator->getConstant('b')->getDefaultValue(); + self::assertNotNull($valueB); + self::assertEquals(123, $valueB->getValue()); + + $valueC = $classGenerator->getConstant('c')->getDefaultValue(); + self::assertNotNull($valueC); + self::assertEquals(123.456, $valueC->getValue()); + + $valueD = $classGenerator->getConstant('d')->getDefaultValue(); + self::assertNotNull($valueD); + self::assertEquals([], $valueD->getValue()); + + $valueE = $classGenerator->getConstant('e')->getDefaultValue(); + self::assertNotNull($valueE); + self::assertEquals(['v1' => 'v2'], $valueE->getValue()); + + $valueF = $classGenerator->getConstant('f')->getDefaultValue(); + self::assertNotNull($valueF); + self::assertEquals(['v1' => ['v2' => 'v3']], $valueF->getValue()); + + $valueG = $classGenerator->getConstant('g')->getDefaultValue(); + self::assertNotNull($valueG); + self::assertEquals(null, $valueG->getValue()); } public function testAddConstantRejectsObjectConstantValue() @@ -714,7 +750,7 @@ public function testAddConstantThrowsExceptionOnDuplicate() $classGenerator = new ClassGenerator(); $classGenerator->addConstant('x', 'value1'); - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $classGenerator->addConstant('x', 'value1'); } @@ -737,7 +773,9 @@ public function testAddPropertyIsBackwardsCompatibleWithConstants() $classGenerator->addProperty('x', 'value1', PropertyGenerator::FLAG_CONSTANT); - self::assertEquals('value1', $classGenerator->getConstant('x')->getDefaultValue()->getValue()); + $valueX = $classGenerator->getConstant('x')->getDefaultValue(); + self::assertNotNull($valueX); + self::assertEquals('value1', $valueX->getValue()); } /** @@ -754,8 +792,13 @@ public function testAddPropertiesIsBackwardsCompatibleWithConstants() $classGenerator->addProperties($constants); self::assertCount(2, $classGenerator->getConstants()); - self::assertEquals('value1', $classGenerator->getConstant('x')->getDefaultValue()->getValue()); - self::assertEquals('value2', $classGenerator->getConstant('y')->getDefaultValue()->getValue()); + $valueX = $classGenerator->getConstant('x')->getDefaultValue(); + self::assertNotNull($valueX); + self::assertEquals('value1', $valueX->getValue()); + + $valueY = $classGenerator->getConstant('y')->getDefaultValue(); + self::assertNotNull($valueY); + self::assertEquals('value2', $valueY->getValue()); } /** @@ -767,7 +810,9 @@ public function testConstantsAddedFromReflection() $classGenerator = ClassGenerator::fromReflection($reflector); $constant = $classGenerator->getConstant('FOO'); - self::assertEquals('foo', $constant->getDefaultValue()->getValue()); + $constantValue = $constant->getDefaultValue(); + self::assertNotNull($constantValue); + self::assertEquals('foo', $constantValue->getValue()); } /** @@ -1170,7 +1215,7 @@ public function testCorrectlyExtendsFullyQualifiedParentClass() $classGenerator = new ClassGenerator(); $classGenerator->setName('ClassName'); $classGenerator->setNamespaceName('SomeNamespace'); - $classGenerator->setExtendedClass('DateTime'); + $classGenerator->setExtendedClass(DateTime::class); self::assertStringContainsString('class ClassName extends \DateTime', $classGenerator->generate()); } @@ -1181,7 +1226,7 @@ public function testCorrectlyExtendsRelativeParentClass() { $classGenerator = new ClassGenerator(); $classGenerator->setName('ClassName'); - $classGenerator->setExtendedClass('DateTime'); + $classGenerator->setExtendedClass(DateTime::class); self::assertStringContainsString('class ClassName extends DateTime', $classGenerator->generate()); } @@ -1207,8 +1252,10 @@ public function testCorrectlyExtendsProvidedAliasIfUseAliasExists() $classGenerator = new ClassGenerator(); $classGenerator->setName('ClassName'); $classGenerator->setNamespaceName('SomeNamespace'); - $classGenerator->addUse('Foo\\Bar', 'BarAlias'); - $classGenerator->setExtendedClass('BarAlias'); + /** @psalm-var class-string $useAlias */ + $useAlias = 'BarAlias'; + $classGenerator->addUse('Foo\\Bar', $useAlias); + $classGenerator->setExtendedClass($useAlias); $generated = $classGenerator->generate(); self::assertStringContainsString('class ClassName extends BarAlias', $generated); } @@ -1219,7 +1266,9 @@ public function testCorrectlyExtendsProvidedNamespaceAliasIfUseAliasExistsForNam $classGenerator->setName('ClassName'); $classGenerator->setNamespaceName('SomeNamespace'); $classGenerator->addUse('Foo\\Bar', 'BarAlias'); - $classGenerator->setExtendedClass('BarAlias\\FooBar'); + /** @psalm-var class-string $extendedClass */ + $extendedClass = 'BarAlias\\FooBar'; + $classGenerator->setExtendedClass($extendedClass); $generated = $classGenerator->generate(); self::assertStringContainsString('class ClassName extends BarAlias\\FooBar', $generated); } @@ -1230,7 +1279,10 @@ public function testCorrectlyExtendsAliasOfProvidedFQCNIfUseAliasExists() $classGenerator->setName('ClassName'); $classGenerator->setNamespaceName('SomeNamespace'); $classGenerator->addUse('Foo\\Bar', 'BarAlias'); - $classGenerator->setExtendedClass('Foo\\Bar'); + + /** @psalm-var class-string $extendedClass */ + $extendedClass = 'Foo\\Bar'; + $classGenerator->setExtendedClass($extendedClass); $generated = $classGenerator->generate(); self::assertStringContainsString('class ClassName extends BarAlias', $generated); } @@ -1241,7 +1293,10 @@ public function testCorrectlyExtendsWithNamespaceAliasOfProvidedFQCNIfUseAliasEx $classGenerator->setName('ClassName'); $classGenerator->setNamespaceName('SomeNamespace'); $classGenerator->addUse('Foo\\Bar', 'BarAlias'); - $classGenerator->setExtendedClass('Foo\\Bar\\FooBar'); + + /** @psalm-var class-string */ + $extendedClass = 'Foo\\Bar\\FooBar'; + $classGenerator->setExtendedClass($extendedClass); $generated = $classGenerator->generate(); self::assertStringContainsString('class ClassName extends BarAlias\\FooBar', $generated); } @@ -1252,11 +1307,14 @@ public function testCorrectImplementNames() $classGenerator->setName('ClassName'); $classGenerator->setNamespaceName('SomeNamespace'); $classGenerator->addUse(GeneratorInterface::class); - $classGenerator->setImplementedInterfaces([ + + /** @psalm-var array */ + $implementedInterfaces = [ 'SomeNamespace\ClassInterface', GeneratorInterface::class, 'Iteratable', - ]); + ]; + $classGenerator->setImplementedInterfaces($implementedInterfaces); $expected = 'class ClassName implements ClassInterface, GeneratorInterface, \Iteratable'; self::assertStringContainsString($expected, $classGenerator->generate()); diff --git a/test/Generator/FileGeneratorTest.php b/test/Generator/FileGeneratorTest.php index 0066a125..1d5034dd 100644 --- a/test/Generator/FileGeneratorTest.php +++ b/test/Generator/FileGeneratorTest.php @@ -360,6 +360,7 @@ public function testWrongDeclareTypeShouldRaiseException(): void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('setDeclares is expecting an array of Laminas\\Code\\DeclareStatement objects'); + /** @psalm-suppress InvalidArgument */ $generator->setDeclares([new stdClass()]); } diff --git a/test/Generator/InterfaceGeneratorTest.php b/test/Generator/InterfaceGeneratorTest.php index 307cca94..91e5eeaa 100644 --- a/test/Generator/InterfaceGeneratorTest.php +++ b/test/Generator/InterfaceGeneratorTest.php @@ -9,6 +9,7 @@ namespace LaminasTest\Code\Generator; use Countable; +use DateTime; use IteratorAggregate; use Laminas\Code\Generator\DocBlockGenerator; use Laminas\Code\Generator\Exception\InvalidArgumentException; @@ -36,7 +37,7 @@ public function testAbstractAccessorsReturnsFalse() public function testExtendedClassAccessors() { $classGenerator = new InterfaceGenerator(); - $classGenerator->setExtendedClass('ExtendedClass'); + $classGenerator->setExtendedClass(DateTime::class); self::assertNull($classGenerator->getExtendedClass()); } @@ -103,7 +104,7 @@ public function testSetextendedclassShouldIgnoreEmptyClassnameOnGenerate() $classGeneratorClass = new InterfaceGenerator(); $classGeneratorClass ->setName('MyInterface') - ->setExtendedClass(''); + ->setExtendedClass(null); $expected = <<setName('MyInterface') - ->setExtendedClass('MyInterface'); + ->setExtendedClass(DateTime::class); $expected = <<getDefaultValue()); - self::assertNull($firstParameter->getDefaultValue()->getSourceContent()); + $valueGenerator = $firstParameter->getDefaultValue(); + self::assertInstanceOf(ValueGenerator::class, $valueGenerator); + self::assertSame('', $valueGenerator->getSourceContent()); } public function testFromReflectionGetArrayHint() diff --git a/test/Generator/PropertyGeneratorTest.php b/test/Generator/PropertyGeneratorTest.php index 34267f86..51a58c99 100644 --- a/test/Generator/PropertyGeneratorTest.php +++ b/test/Generator/PropertyGeneratorTest.php @@ -173,7 +173,9 @@ public function testPropertyWillLoadFromReflection(): void $cgProp = PropertyGenerator::fromReflection($reflProp); self::assertEquals('_bazProperty', $cgProp->getName()); - self::assertEquals([true, false, true], $cgProp->getDefaultValue()->getValue()); + $bazPropertyValue = $cgProp->getDefaultValue(); + self::assertNotNull($bazPropertyValue); + self::assertEquals([true, false, true], $bazPropertyValue->getValue()); self::assertEquals('private', $cgProp->getVisibility()); $reflProp = $reflectionClass->getProperty('_bazStaticProperty'); @@ -182,7 +184,9 @@ public function testPropertyWillLoadFromReflection(): void $cgProp = PropertyGenerator::fromReflection($reflProp); self::assertEquals('_bazStaticProperty', $cgProp->getName()); - self::assertEquals(TestAsset\TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue()); + $bazStaticValue = $cgProp->getDefaultValue(); + self::assertNotNull($bazStaticValue); + self::assertEquals(TestAsset\TestClassWithManyProperties::FOO, $bazStaticValue->getValue()); self::assertTrue($cgProp->isStatic()); self::assertEquals('private', $cgProp->getVisibility()); } @@ -296,8 +300,10 @@ public function testSetDefaultValue(string $type, $value): void $property = new PropertyGenerator(); $property->setDefaultValue($value, $type); - self::assertEquals($type, $property->getDefaultValue()->getType()); - self::assertEquals($value, $property->getDefaultValue()->getValue()); + $valueGenerator = $property->getDefaultValue(); + self::assertNotNull($valueGenerator); + self::assertEquals($type, $valueGenerator->getType()); + self::assertEquals($value, $valueGenerator->getValue()); } public function testOmitType() diff --git a/test/Generator/TraitGeneratorTest.php b/test/Generator/TraitGeneratorTest.php index e7166fc0..6b0ae5d7 100644 --- a/test/Generator/TraitGeneratorTest.php +++ b/test/Generator/TraitGeneratorTest.php @@ -8,6 +8,7 @@ namespace LaminasTest\Code\Generator; +use DateTime; use Laminas\Code\Generator\DocBlockGenerator; use Laminas\Code\Generator\Exception\ExceptionInterface; use Laminas\Code\Generator\Exception\InvalidArgumentException; @@ -16,6 +17,8 @@ use Laminas\Code\Generator\TraitGenerator; use Laminas\Code\Reflection\ClassReflection; use PHPUnit\Framework\TestCase; +use Serializable; +use Throwable; use function current; @@ -65,7 +68,7 @@ public function testExtendedClassAccessors() public function testImplementedInterfacesAccessors() { $classGenerator = new TraitGenerator(); - $classGenerator->setImplementedInterfaces(['Class1', 'Class2']); + $classGenerator->setImplementedInterfaces([Serializable::class, Throwable::class]); self::assertCount(0, $classGenerator->getImplementedInterfaces()); } @@ -271,7 +274,7 @@ public function testSetextendedclassShouldIgnoreEmptyClassnameOnGenerate() $classGeneratorClass = new TraitGenerator(); $classGeneratorClass ->setName('MyClass') - ->setExtendedClass(''); + ->setExtendedClass(null); $expected = <<setName('MyClass') - ->setExtendedClass('ParentClass'); + ->setExtendedClass(DateTime::class); $expected = <<