From d5c06a8af4270fe4e7e293d312baea54896eea66 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 26 Sep 2021 19:29:26 +0700 Subject: [PATCH 1/2] [Php74] Apply PHP 7.4 syntax and typed properties (for private modifier) Signed-off-by: Abdul Malik Ikhsan --- src/Generator/ClassGenerator.php | 7 +++---- src/Generator/DocBlock/Tag/VarTag.php | 3 +-- src/Generator/FileGenerator.php | 7 ++++--- src/Generator/MethodGenerator.php | 8 +++++--- test/Generator/DocBlock/Tag/VarTagTest.php | 6 ++---- test/Generator/MethodGeneratorTest.php | 10 +++------- test/Generator/ParameterGeneratorTest.php | 14 +++++--------- .../TestAsset/TestClassWithManyProperties.php | 4 ++-- test/Generator/TypeGeneratorTest.php | 4 +--- 9 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index afbe4729..f07d4388 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -446,10 +446,9 @@ public function removeExtentedClass() */ public function setImplementedInterfaces(array $implementedInterfaces) { - array_map(function ($implementedInterface) { - // This loop is just validating that the given `$implementedInterfaces` contains valid syntax/symbols - return TypeGenerator::fromTypeString($implementedInterface); - }, $implementedInterfaces); + // This loop is just validating that the given `$implementedInterfaces` contains valid syntax/symbols + array_map(static fn($implementedInterface) => + TypeGenerator::fromTypeString($implementedInterface), $implementedInterfaces); $this->implementedInterfaces = $implementedInterfaces; return $this; diff --git a/src/Generator/DocBlock/Tag/VarTag.php b/src/Generator/DocBlock/Tag/VarTag.php index db670eff..e67919bd 100644 --- a/src/Generator/DocBlock/Tag/VarTag.php +++ b/src/Generator/DocBlock/Tag/VarTag.php @@ -6,8 +6,7 @@ class VarTag extends AbstractTypeableTag implements TagInterface { - /** @var string|null */ - private $variableName; + private ?string $variableName = null; /** * @param string|string[] $types diff --git a/src/Generator/FileGenerator.php b/src/Generator/FileGenerator.php index dadd7063..871a3bab 100644 --- a/src/Generator/FileGenerator.php +++ b/src/Generator/FileGenerator.php @@ -100,9 +100,10 @@ public static function fromArray(array $values) $fileGenerator->setRequiredFiles($value); break; case 'declares': - $fileGenerator->setDeclares(array_map(static function ($directive, $value) { - return DeclareStatement::fromArray([$directive => $value]); - }, array_keys($value), $value)); + $fileGenerator->setDeclares( + array_map(static fn($directive, $value) => + DeclareStatement::fromArray([$directive => $value]), array_keys($value), $value) + ); break; default: if (property_exists($fileGenerator, $name)) { diff --git a/src/Generator/MethodGenerator.php b/src/Generator/MethodGenerator.php index 6e6be65e..20ba54f7 100644 --- a/src/Generator/MethodGenerator.php +++ b/src/Generator/MethodGenerator.php @@ -315,9 +315,11 @@ public function setReturnsReference($returnsReference) */ private function sortParameters(): void { - uasort($this->parameters, static function (ParameterGenerator $item1, ParameterGenerator $item2) { - return $item1->getPosition() <=> $item2->getPosition(); - }); + uasort( + $this->parameters, + static fn(ParameterGenerator $item1, ParameterGenerator $item2) + => $item1->getPosition() <=> $item2->getPosition() + ); } /** diff --git a/test/Generator/DocBlock/Tag/VarTagTest.php b/test/Generator/DocBlock/Tag/VarTagTest.php index dea2ed3c..e714a6c8 100644 --- a/test/Generator/DocBlock/Tag/VarTagTest.php +++ b/test/Generator/DocBlock/Tag/VarTagTest.php @@ -13,11 +13,9 @@ */ class VarTagTest extends TestCase { - /** @var VarTag */ - private $tag; + private VarTag $tag; - /** @var TagManager */ - private $tagManager; + private TagManager $tagManager; protected function setUp(): void { diff --git a/test/Generator/MethodGeneratorTest.php b/test/Generator/MethodGeneratorTest.php index 5e424a54..1dbc8853 100644 --- a/test/Generator/MethodGeneratorTest.php +++ b/test/Generator/MethodGeneratorTest.php @@ -102,9 +102,7 @@ public function testSetMethodParameters() $params = $methodGenerator->getParameters(); - $sorting = array_map(static function (ParameterGenerator $parameter): string { - return $parameter->getName(); - }, $params); + $sorting = array_map(static fn(ParameterGenerator $parameter): string => $parameter->getName(), $params); self::assertEquals(['foo' => 'foo', 'baz' => 'baz', 'bar' => 'bar'], $sorting); } @@ -427,10 +425,8 @@ public function returnTypeHintClasses() return array_values(array_filter( $parameters, - function (array $parameter) { - return PHP_VERSION_ID >= 80000 - || $parameter[0] !== Php80Types::class; - } + static fn(array $parameter) => PHP_VERSION_ID >= 80000 + || $parameter[0] !== Php80Types::class )); } diff --git a/test/Generator/ParameterGeneratorTest.php b/test/Generator/ParameterGeneratorTest.php index b11ac888..88b3afa4 100644 --- a/test/Generator/ParameterGeneratorTest.php +++ b/test/Generator/ParameterGeneratorTest.php @@ -480,20 +480,16 @@ public function reflectionHints() $compatibleParameters = array_filter( $parameters, - function (array $parameter) { - return PHP_VERSION_ID >= 70200 - || ( - false === strpos($parameter[3], 'object') - ); - } + static fn(array $parameter) => PHP_VERSION_ID >= 70200 + || ( + false === strpos($parameter[3], 'object') + ) ); // just re-organizing the keys so that the phpunit data set makes sense in errors: return array_combine( array_map( - function (array $definition) { - return $definition[0] . '#' . $definition[1]; - }, + static fn(array $definition) => $definition[0] . '#' . $definition[1], $compatibleParameters ), $compatibleParameters diff --git a/test/Generator/TestAsset/TestClassWithManyProperties.php b/test/Generator/TestAsset/TestClassWithManyProperties.php index 572f4c43..08e3aa84 100644 --- a/test/Generator/TestAsset/TestClassWithManyProperties.php +++ b/test/Generator/TestAsset/TestClassWithManyProperties.php @@ -18,9 +18,9 @@ class TestClassWithManyProperties protected $_barProperty = 1.1115; - private static $_bazStaticProperty = self::FOO; + private static string $_bazStaticProperty = self::FOO; - private $_bazProperty = [true, false, true]; + private array $_bazProperty = [true, false, true]; protected $_complexType = [ 5, diff --git a/test/Generator/TypeGeneratorTest.php b/test/Generator/TypeGeneratorTest.php index 3dcba2ca..a52e3646 100644 --- a/test/Generator/TypeGeneratorTest.php +++ b/test/Generator/TypeGeneratorTest.php @@ -231,9 +231,7 @@ public function validClassName() { return array_filter( $this->validType(), - function (array $pair) { - return 0 === strpos($pair[1], '\\'); - } + static fn(array $pair) => 0 === strpos($pair[1], '\\') ); } From 4a5b03db8172d131307f85946fd4e76342589764 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 26 Sep 2021 20:00:04 +0700 Subject: [PATCH 2/2] drop validating that the given `$implementedInterfaces` contains valid syntax/symbols Signed-off-by: Abdul Malik Ikhsan --- src/Generator/ClassGenerator.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index f07d4388..3ac3dd23 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -446,10 +446,6 @@ public function removeExtentedClass() */ public function setImplementedInterfaces(array $implementedInterfaces) { - // This loop is just validating that the given `$implementedInterfaces` contains valid syntax/symbols - array_map(static fn($implementedInterface) => - TypeGenerator::fromTypeString($implementedInterface), $implementedInterfaces); - $this->implementedInterfaces = $implementedInterfaces; return $this; }