From 6f246fcf1223aef4f5d6491aa057adf211da439b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 2 Nov 2024 17:15:26 +0700 Subject: [PATCH] Update to latest PHP 8.1 syntax Signed-off-by: Abdul Malik Ikhsan --- psalm-baseline.xml | 43 ------------------- src/DeclareStatement.php | 9 +--- src/Generator/ClassGenerator.php | 6 +-- src/Generator/EnumGenerator/EnumGenerator.php | 9 +--- src/Generator/EnumGenerator/Name.php | 8 +--- src/Generator/MethodGenerator.php | 9 ++-- src/Generator/ParameterGenerator.php | 2 +- src/Generator/PromotedParameterGenerator.php | 7 +-- src/Generator/PropertyGenerator.php | 10 +---- src/Generator/TraitUsageGenerator.php | 5 +-- src/Generator/TypeGenerator.php | 3 +- src/Generator/TypeGenerator/AtomicType.php | 14 ++---- src/Generator/ValueGenerator.php | 6 +-- src/Reflection/ClassReflection.php | 5 +-- src/Reflection/DocBlock/Tag/AuthorTag.php | 6 ++- src/Reflection/DocBlock/Tag/GenericTag.php | 11 ++--- src/Reflection/DocBlock/Tag/LicenseTag.php | 6 ++- src/Reflection/DocBlock/Tag/MethodTag.php | 6 ++- src/Reflection/DocBlock/Tag/PropertyTag.php | 7 +-- src/Reflection/DocBlock/Tag/VarTag.php | 4 +- src/Reflection/DocBlockReflection.php | 4 +- src/Reflection/FunctionReflection.php | 4 +- src/Reflection/MethodReflection.php | 5 +-- src/Scanner/DocBlockScanner.php | 10 ++--- test/Reflection/ClassReflectionTest.php | 20 ++++----- 25 files changed, 64 insertions(+), 155 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 43d9a082..8c180ae5 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -373,9 +373,6 @@ DocBlockGenerator::fromArray($value) ParameterGenerator::fromArray($parameter) - - __toString - $value @@ -603,9 +600,6 @@ $constants instanceof SplArrayObject || $constants instanceof StdlibArrayObject $constants instanceof StdlibArrayObject - - __toString - initEnvironmentConstants @@ -678,9 +672,6 @@ #[ReturnTypeWillChange] #[ReturnTypeWillChange] - - __toString - #[ReturnTypeWillChange] #[ReturnTypeWillChange] @@ -705,11 +696,6 @@ getStartLine - - - __toString - - contentSplitCharacter]]> @@ -717,9 +703,6 @@ string|null - - __toString - name]]> @@ -727,33 +710,15 @@ getContent - - - __toString - - - - - __toString - - getDescription - - - __toString - - ! is_string($filter) - - __toString - @@ -831,15 +796,10 @@ #[ReturnTypeWillChange] #[ReturnTypeWillChange] - public function __toString() - - __toString - #[ReturnTypeWillChange] #[ReturnTypeWillChange] - public function __toString() $returnTypes @@ -897,9 +857,6 @@ #[ReturnTypeWillChange] #[ReturnTypeWillChange] - - __toString - #[ReturnTypeWillChange] #[ReturnTypeWillChange] diff --git a/src/DeclareStatement.php b/src/DeclareStatement.php index 0bdaa61e..2d79a535 100644 --- a/src/DeclareStatement.php +++ b/src/DeclareStatement.php @@ -26,16 +26,9 @@ class DeclareStatement self::ENCODING => 'string', ]; - protected string $directive; - - /** @var int|string */ - protected $value; - /** @param int|string $value */ - private function __construct(string $directive, $value) + private function __construct(protected string $directive, protected $value) { - $this->directive = $directive; - $this->value = $value; } public function getDirective(): string diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index 261b2302..5a01ac59 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -532,11 +532,7 @@ public function removeImplementedInterface($implementedInterface) */ public function getConstant($constantName) { - if (isset($this->constants[$constantName])) { - return $this->constants[$constantName]; - } - - return false; + return $this->constants[$constantName] ?? false; } /** diff --git a/src/Generator/EnumGenerator/EnumGenerator.php b/src/Generator/EnumGenerator/EnumGenerator.php index 5f30ea13..e565a1fd 100644 --- a/src/Generator/EnumGenerator/EnumGenerator.php +++ b/src/Generator/EnumGenerator/EnumGenerator.php @@ -23,18 +23,11 @@ final class EnumGenerator */ private const INDENTATION = ' '; - private Name $name; - - /** @var BackedCases|PureCases */ - private $cases; - /** * @param BackedCases|PureCases $cases */ - private function __construct(Name $name, $cases) + private function __construct(private readonly Name $name, private $cases) { - $this->name = $name; - $this->cases = $cases; } public function generate(): string diff --git a/src/Generator/EnumGenerator/Name.php b/src/Generator/EnumGenerator/Name.php index 5cb09bd8..87f62de6 100644 --- a/src/Generator/EnumGenerator/Name.php +++ b/src/Generator/EnumGenerator/Name.php @@ -12,14 +12,8 @@ */ final class Name { - private string $name; - - private ?string $namespace; - - private function __construct(string $name, ?string $namespace) + private function __construct(private readonly string $name, private readonly ?string $namespace) { - $this->name = $name; - $this->namespace = $namespace; } public function getName(): string diff --git a/src/Generator/MethodGenerator.php b/src/Generator/MethodGenerator.php index d2539a4c..584e8ee0 100644 --- a/src/Generator/MethodGenerator.php +++ b/src/Generator/MethodGenerator.php @@ -3,6 +3,7 @@ namespace Laminas\Code\Generator; use Laminas\Code\Reflection\MethodReflection; +use Stringable; use function array_map; use function explode; @@ -12,13 +13,14 @@ use function preg_replace; use function sprintf; use function str_replace; +use function str_starts_with; use function strlen; use function strtolower; use function substr; use function trim; use function uasort; -class MethodGenerator extends AbstractMemberGenerator +class MethodGenerator extends AbstractMemberGenerator implements Stringable { protected ?DocBlockGenerator $docBlock = null; @@ -106,7 +108,7 @@ protected static function clearBodyIndention($body) $indention = str_replace(trim($lines[1]), '', $lines[1]); foreach ($lines as $key => $line) { - if (substr($line, 0, strlen($indention)) == $indention) { + if (str_starts_with($line, $indention)) { $lines[$key] = substr($line, strlen($indention)); } } @@ -397,8 +399,7 @@ public function generate() return $output; } - /** @return string */ - public function __toString() + public function __toString(): string { return $this->generate(); } diff --git a/src/Generator/ParameterGenerator.php b/src/Generator/ParameterGenerator.php index d87d5221..1a86c278 100644 --- a/src/Generator/ParameterGenerator.php +++ b/src/Generator/ParameterGenerator.php @@ -46,7 +46,7 @@ public static function fromReflection(ParameterReflection $reflectionParameter) if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) { try { $param->setDefaultValue($reflectionParameter->getDefaultValue()); - } catch (ReflectionException $e) { + } catch (ReflectionException) { $param->setDefaultValue(null); } } diff --git a/src/Generator/PromotedParameterGenerator.php b/src/Generator/PromotedParameterGenerator.php index 56cd02cf..a558827d 100644 --- a/src/Generator/PromotedParameterGenerator.php +++ b/src/Generator/PromotedParameterGenerator.php @@ -15,9 +15,6 @@ final class PromotedParameterGenerator extends ParameterGenerator public const VISIBILITY_PROTECTED = 'protected'; public const VISIBILITY_PRIVATE = 'private'; - /** @psalm-var PromotedParameterGenerator::VISIBILITY_* */ - private string $visibility; - /** * @psalm-param non-empty-string $name * @psalm-param ?non-empty-string $type @@ -26,7 +23,7 @@ final class PromotedParameterGenerator extends ParameterGenerator public function __construct( string $name, ?string $type = null, - string $visibility = self::VISIBILITY_PUBLIC, + private readonly string $visibility = self::VISIBILITY_PUBLIC, ?int $position = null, bool $passByReference = false ) { @@ -37,8 +34,6 @@ public function __construct( $position, $passByReference, ); - - $this->visibility = $visibility; } /** @psalm-return non-empty-string */ diff --git a/src/Generator/PropertyGenerator.php b/src/Generator/PropertyGenerator.php index 677af845..65992177 100644 --- a/src/Generator/PropertyGenerator.php +++ b/src/Generator/PropertyGenerator.php @@ -18,8 +18,6 @@ class PropertyGenerator extends AbstractMemberGenerator protected bool $isConst = false; - protected ?TypeGenerator $type = null; - protected ?PropertyValueGenerator $defaultValue = null; private bool $omitDefaultValue = false; @@ -32,7 +30,7 @@ public function __construct( ?string $name = null, $defaultValue = null, $flags = self::FLAG_PUBLIC, - ?TypeGenerator $type = null + protected ?TypeGenerator $type = null ) { parent::__construct(); @@ -45,8 +43,6 @@ public function __construct( if ($flags !== self::FLAG_PUBLIC) { $this->setFlags($flags); } - - $this->type = $type; } /** @return static */ @@ -232,9 +228,7 @@ public function isReadonly(): bool /** @inheritDoc */ public function setFlags($flags) { - $flags = array_reduce((array) $flags, static function (int $a, int $b): int { - return $a | $b; - }, 0); + $flags = array_reduce((array) $flags, static fn(int $a, int $b): int => $a | $b, 0); if ($flags & self::FLAG_READONLY && $flags & self::FLAG_STATIC) { throw new Exception\RuntimeException('Modifier "readonly" in combination with "static" not permitted.'); diff --git a/src/Generator/TraitUsageGenerator.php b/src/Generator/TraitUsageGenerator.php index 536fac8b..44df4c3d 100644 --- a/src/Generator/TraitUsageGenerator.php +++ b/src/Generator/TraitUsageGenerator.php @@ -22,8 +22,6 @@ /** @psalm-type Visibility = ReflectionMethod::IS_PRIVATE|ReflectionMethod::IS_PROTECTED|ReflectionMethod::IS_PUBLIC */ class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterface { - protected ClassGenerator $classGenerator; - /** @psalm-var array Array of trait names */ protected array $traits = []; /** @@ -43,9 +41,8 @@ class TraitUsageGenerator extends AbstractGenerator implements TraitUsageInterfa /** @var array Array of string names */ protected array $uses = []; - public function __construct(ClassGenerator $classGenerator) + public function __construct(protected ClassGenerator $classGenerator) { - $this->classGenerator = $classGenerator; } /** diff --git a/src/Generator/TypeGenerator.php b/src/Generator/TypeGenerator.php index ee77d324..a5b2661c 100644 --- a/src/Generator/TypeGenerator.php +++ b/src/Generator/TypeGenerator.php @@ -13,6 +13,7 @@ use ReflectionIntersectionType; use ReflectionNamedType; use ReflectionUnionType; +use Stringable; use function array_map; use function sprintf; @@ -21,7 +22,7 @@ use function substr; /** @psalm-immutable */ -final class TypeGenerator implements GeneratorInterface +final class TypeGenerator implements GeneratorInterface, Stringable { private const NULL_MARKER = '?'; diff --git a/src/Generator/TypeGenerator/AtomicType.php b/src/Generator/TypeGenerator/AtomicType.php index df9a6211..d6b88d4d 100644 --- a/src/Generator/TypeGenerator/AtomicType.php +++ b/src/Generator/TypeGenerator/AtomicType.php @@ -62,20 +62,14 @@ final class AtomicType private const VALID_IDENTIFIER_MATCHER = '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*' . '(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/'; - /** @psalm-var value-of|0 */ - public int $sortIndex; - - /** @psalm-var non-empty-string */ - public string $type; - /** * @psalm-param non-empty-string $type * @psalm-param value-of|0 $sortIndex */ - private function __construct(string $type, int $sortIndex) - { - $this->type = $type; - $this->sortIndex = $sortIndex; + private function __construct( + public string $type, + public int $sortIndex + ) { } /** diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index 5e647a0b..904fb413 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -5,6 +5,7 @@ use ArrayObject as SplArrayObject; use Laminas\Code\Exception\InvalidArgumentException; use Laminas\Stdlib\ArrayObject as StdlibArrayObject; +use Stringable; use UnitEnum; use function addcslashes; @@ -25,7 +26,7 @@ use function str_contains; use function str_repeat; -class ValueGenerator extends AbstractGenerator +class ValueGenerator extends AbstractGenerator implements Stringable { /**#@+ * Constant values @@ -500,8 +501,7 @@ public function getOutputMode() return $this->outputMode; } - /** @return string */ - public function __toString() + public function __toString(): string { return $this->generate(); } diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index cf4ca7dd..d8788613 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -202,10 +202,7 @@ public function toString() return parent::__toString(); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return parent::__toString(); } diff --git a/src/Reflection/DocBlock/Tag/AuthorTag.php b/src/Reflection/DocBlock/Tag/AuthorTag.php index 02c7a7ef..1694107b 100644 --- a/src/Reflection/DocBlock/Tag/AuthorTag.php +++ b/src/Reflection/DocBlock/Tag/AuthorTag.php @@ -2,10 +2,12 @@ namespace Laminas\Code\Reflection\DocBlock\Tag; +use Stringable; + use function preg_match; use function rtrim; -class AuthorTag implements TagInterface +class AuthorTag implements TagInterface, Stringable { /** @var string|null */ protected $authorName; @@ -50,7 +52,7 @@ public function getAuthorEmail() } /** @return non-empty-string */ - public function __toString() + public function __toString(): string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/GenericTag.php b/src/Reflection/DocBlock/Tag/GenericTag.php index 8bc364b7..8c7289c3 100644 --- a/src/Reflection/DocBlock/Tag/GenericTag.php +++ b/src/Reflection/DocBlock/Tag/GenericTag.php @@ -3,11 +3,12 @@ namespace Laminas\Code\Reflection\DocBlock\Tag; use Laminas\Code\Generic\Prototype\PrototypeGenericInterface; +use Stringable; use function explode; use function trim; -class GenericTag implements TagInterface, PrototypeGenericInterface +class GenericTag implements TagInterface, PrototypeGenericInterface, Stringable { /** @var string|null */ protected $name; @@ -15,18 +16,14 @@ class GenericTag implements TagInterface, PrototypeGenericInterface /** @var string|null */ protected $content; - /** @var string */ - protected $contentSplitCharacter; - /** @var list */ protected $values = []; /** * @param string $contentSplitCharacter */ - public function __construct($contentSplitCharacter = ' ') + public function __construct(protected $contentSplitCharacter = ' ') { - $this->contentSplitCharacter = $contentSplitCharacter; } /** @inheritDoc */ @@ -66,7 +63,7 @@ public function returnValue($position) } /** @return non-empty-string */ - public function __toString() + public function __toString(): string { return 'DocBlock Tag [ * @' . $this->name . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/LicenseTag.php b/src/Reflection/DocBlock/Tag/LicenseTag.php index d580c734..4f127e3d 100644 --- a/src/Reflection/DocBlock/Tag/LicenseTag.php +++ b/src/Reflection/DocBlock/Tag/LicenseTag.php @@ -2,10 +2,12 @@ namespace Laminas\Code\Reflection\DocBlock\Tag; +use Stringable; + use function preg_match; use function trim; -class LicenseTag implements TagInterface +class LicenseTag implements TagInterface, Stringable { /** @var string|null */ protected $url; @@ -50,7 +52,7 @@ public function getLicenseName() } /** @return non-empty-string */ - public function __toString() + public function __toString(): string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/MethodTag.php b/src/Reflection/DocBlock/Tag/MethodTag.php index edb1dee2..e20d0ca7 100644 --- a/src/Reflection/DocBlock/Tag/MethodTag.php +++ b/src/Reflection/DocBlock/Tag/MethodTag.php @@ -2,11 +2,13 @@ namespace Laminas\Code\Reflection\DocBlock\Tag; +use Stringable; + use function explode; use function preg_match; use function rtrim; -class MethodTag implements TagInterface, PhpDocTypedTagInterface +class MethodTag implements TagInterface, PhpDocTypedTagInterface, Stringable { /** @var list */ protected $types = []; @@ -91,7 +93,7 @@ public function isStatic() } /** @return non-empty-string */ - public function __toString() + public function __toString(): string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/PropertyTag.php b/src/Reflection/DocBlock/Tag/PropertyTag.php index 1bfdcdb7..0c40dd4b 100644 --- a/src/Reflection/DocBlock/Tag/PropertyTag.php +++ b/src/Reflection/DocBlock/Tag/PropertyTag.php @@ -2,11 +2,13 @@ namespace Laminas\Code\Reflection\DocBlock\Tag; +use Stringable; + use function explode; use function preg_match; use function rtrim; -class PropertyTag implements TagInterface, PhpDocTypedTagInterface +class PropertyTag implements TagInterface, PhpDocTypedTagInterface, Stringable { /** @var list */ protected $types = []; @@ -83,10 +85,9 @@ public function getDescription() } /** - * @return string * @psalm-return non-empty-string */ - public function __toString() + public function __toString(): string { return 'DocBlock Tag [ * @' . $this->getName() . ' ]' . "\n"; } diff --git a/src/Reflection/DocBlock/Tag/VarTag.php b/src/Reflection/DocBlock/Tag/VarTag.php index 5100f620..d234287e 100644 --- a/src/Reflection/DocBlock/Tag/VarTag.php +++ b/src/Reflection/DocBlock/Tag/VarTag.php @@ -2,13 +2,15 @@ namespace Laminas\Code\Reflection\DocBlock\Tag; +use Stringable; + use function explode; use function preg_match; use function rtrim; use const PHP_EOL; -class VarTag implements TagInterface, PhpDocTypedTagInterface +class VarTag implements TagInterface, PhpDocTypedTagInterface, Stringable { /** * @var string[] diff --git a/src/Reflection/DocBlockReflection.php b/src/Reflection/DocBlockReflection.php index 60335630..7dd42236 100644 --- a/src/Reflection/DocBlockReflection.php +++ b/src/Reflection/DocBlockReflection.php @@ -270,10 +270,8 @@ public function toString() * Serialize to string * * Required by the Reflector interface - * - * @return string */ - public function __toString() + public function __toString(): string { return $this->toString(); } diff --git a/src/Reflection/FunctionReflection.php b/src/Reflection/FunctionReflection.php index ba84cc94..0ec1d3b9 100644 --- a/src/Reflection/FunctionReflection.php +++ b/src/Reflection/FunctionReflection.php @@ -280,9 +280,9 @@ public function toString() /** * Required due to bug in php * - * @return string + * @psalm-external-mutation-free */ - public function __toString() + public function __toString(): string { return parent::__toString(); } diff --git a/src/Reflection/MethodReflection.php b/src/Reflection/MethodReflection.php index 45b1e425..cd8d8d9c 100644 --- a/src/Reflection/MethodReflection.php +++ b/src/Reflection/MethodReflection.php @@ -475,10 +475,7 @@ public function toString() return parent::__toString(); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return parent::__toString(); } diff --git a/src/Scanner/DocBlockScanner.php b/src/Scanner/DocBlockScanner.php index de13ef1a..459af0a9 100644 --- a/src/Scanner/DocBlockScanner.php +++ b/src/Scanner/DocBlockScanner.php @@ -2,6 +2,7 @@ namespace Laminas\Code\Scanner; +use function array_key_last; use function array_pop; use function array_push; use function current; @@ -21,9 +22,6 @@ class DocBlockScanner /** @var bool */ protected $isScanned = false; - /** @var string */ - protected $docComment; - /** @var string */ protected $shortDescription = ''; @@ -36,9 +34,8 @@ class DocBlockScanner /** * @param string $docComment */ - public function __construct($docComment) + public function __construct(protected $docComment) { - $this->docComment = $docComment; } /** @@ -120,8 +117,7 @@ protected function scan() 'name' => $token[1], 'value' => '', ]); - end($this->tags); - $tagIndex = key($this->tags); + $tagIndex = array_key_last($this->tags); $mode = 3; goto SCANNER_CONTINUE; //goto no break needed diff --git a/test/Reflection/ClassReflectionTest.php b/test/Reflection/ClassReflectionTest.php index c0f04686..e5d747fc 100644 --- a/test/Reflection/ClassReflectionTest.php +++ b/test/Reflection/ClassReflectionTest.php @@ -73,27 +73,27 @@ public function testGetContentsReturnsContents() $target = <<_prop1; } - + public function getProp2(\$param1, TestSampleClass \$param2) { return \$this->_prop2; } - + public function getIterator(): \Traversable { return new \EmptyIterator(); } - + } EOS; $contents = $reflectionClass->getContents(); @@ -108,27 +108,27 @@ public function testGetContentsReturnsContentsWithImplementsOnSeparateLine() $target = <<_prop1; } - + public function getProp2(\$param1, TestSampleClass \$param2) { return \$this->_prop2; } - + public function getIterator(): \Traversable { return new \EmptyIterator(); } - + } EOS; $contents = $reflectionClass->getContents();