From 2503785a31f1151c6078a2985be2adbd9a6817c2 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Fri, 21 Aug 2020 12:46:57 +0200 Subject: [PATCH 01/12] Add true and false as PseudoTypes In this change, I have made more allowance for identifying types as PseudoTypes and to ensure they are identified as both their underlying and pseudo-type. Using instanceof, you can identify whether it is the PseudoType, a pseudotype, but also as the underlying type. --- src/PseudoType.php | 19 ++++++++++++ src/PseudoTypes/False_.php | 22 ++++++++++++++ src/PseudoTypes/True_.php | 22 ++++++++++++++ src/TypeResolver.php | 4 +-- src/Types/Boolean.php | 2 +- tests/unit/PseudoTypes/FalseTest.php | 43 ++++++++++++++++++++++++++++ tests/unit/PseudoTypes/TrueTest.php | 43 ++++++++++++++++++++++++++++ 7 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 src/PseudoType.php create mode 100644 src/PseudoTypes/False_.php create mode 100644 src/PseudoTypes/True_.php create mode 100644 tests/unit/PseudoTypes/FalseTest.php create mode 100644 tests/unit/PseudoTypes/TrueTest.php diff --git a/src/PseudoType.php b/src/PseudoType.php new file mode 100644 index 0000000..dd91ed7 --- /dev/null +++ b/src/PseudoType.php @@ -0,0 +1,19 @@ + Types\Scalar::class, 'callback' => Types\Callable_::class, 'callable' => Types\Callable_::class, - 'false' => Types\Boolean::class, - 'true' => Types\Boolean::class, + 'false' => PseudoTypes\False_::class, + 'true' => PseudoTypes\False_::class, 'self' => Types\Self_::class, '$this' => Types\This::class, 'static' => Types\Static_::class, diff --git a/src/Types/Boolean.php b/src/Types/Boolean.php index 2490917..d5d2405 100644 --- a/src/Types/Boolean.php +++ b/src/Types/Boolean.php @@ -18,7 +18,7 @@ /** * Value Object representing a Boolean type. */ -final class Boolean implements Type +class Boolean implements Type { /** * Returns a rendered output of the Type as it would be used in a DocBlock. diff --git a/tests/unit/PseudoTypes/FalseTest.php b/tests/unit/PseudoTypes/FalseTest.php new file mode 100644 index 0000000..df6230d --- /dev/null +++ b/tests/unit/PseudoTypes/FalseTest.php @@ -0,0 +1,43 @@ +assertInstanceOf(Boolean::class, $false->underlyingType()); + } + + /** + * @covers ::__toString + */ + public function testFalseStringifyCorrectly() : void + { + $false = new False_(); + + $this->assertSame('false', (string) $false); + } +} diff --git a/tests/unit/PseudoTypes/TrueTest.php b/tests/unit/PseudoTypes/TrueTest.php new file mode 100644 index 0000000..d78135c --- /dev/null +++ b/tests/unit/PseudoTypes/TrueTest.php @@ -0,0 +1,43 @@ +assertInstanceOf(Boolean::class, $true->underlyingType()); + } + + /** + * @covers ::__toString + */ + public function testTrueStringifyCorrectly() : void + { + $true = new True_(); + + $this->assertSame('true', (string) $true); + } +} From 8d29b5d0da1740dbbc27f84a5ea62cf8b4ac4525 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Fri, 21 Aug 2020 20:05:35 +0200 Subject: [PATCH 02/12] Correct issues in tests --- tests/unit/PseudoTypes/FalseTest.php | 4 ++-- tests/unit/PseudoTypes/TrueTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/PseudoTypes/FalseTest.php b/tests/unit/PseudoTypes/FalseTest.php index df6230d..9ab7ffc 100644 --- a/tests/unit/PseudoTypes/FalseTest.php +++ b/tests/unit/PseudoTypes/FalseTest.php @@ -17,9 +17,9 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \phpDocumentor\Reflection\Types\False_ + * @coversDefaultClass \phpDocumentor\Reflection\PseudoTypes\False_ */ -class FalseTest extends TestCase +final class FalseTest extends TestCase { /** * @covers ::underlyingType diff --git a/tests/unit/PseudoTypes/TrueTest.php b/tests/unit/PseudoTypes/TrueTest.php index d78135c..00deea4 100644 --- a/tests/unit/PseudoTypes/TrueTest.php +++ b/tests/unit/PseudoTypes/TrueTest.php @@ -17,7 +17,7 @@ use PHPUnit\Framework\TestCase; /** - * @coversDefaultClass \phpDocumentor\Reflection\Types\True_ + * @coversDefaultClass \phpDocumentor\Reflection\PseudoTypes\True_ */ class TrueTest extends TestCase { From b59ba3002ded620be9f52e825d842b1d77d963b8 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Fri, 21 Aug 2020 20:08:02 +0200 Subject: [PATCH 03/12] Position return type : correctly --- src/PseudoType.php | 2 +- src/PseudoTypes/False_.php | 42 +++++++++++++++++++------------------- src/PseudoTypes/True_.php | 42 +++++++++++++++++++------------------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/PseudoType.php b/src/PseudoType.php index dd91ed7..f94cff5 100644 --- a/src/PseudoType.php +++ b/src/PseudoType.php @@ -15,5 +15,5 @@ interface PseudoType extends Type { - public function underlyingType(): Type; + public function underlyingType() : Type; } diff --git a/src/PseudoTypes/False_.php b/src/PseudoTypes/False_.php index 3802fd1..7882618 100644 --- a/src/PseudoTypes/False_.php +++ b/src/PseudoTypes/False_.php @@ -1,22 +1,22 @@ - Date: Sat, 22 Aug 2020 11:04:12 +0200 Subject: [PATCH 04/12] Fix linting issues --- src/PseudoTypes/False_.php | 14 +++++++++++++- src/PseudoTypes/True_.php | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/PseudoTypes/False_.php b/src/PseudoTypes/False_.php index 7882618..6193d3b 100644 --- a/src/PseudoTypes/False_.php +++ b/src/PseudoTypes/False_.php @@ -2,12 +2,24 @@ declare(strict_types=1); +/** + * This file is part of phpDocumentor. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @link http://phpdoc.org + */ + namespace phpDocumentor\Reflection\PseudoTypes; use phpDocumentor\Reflection\PseudoType; use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Types\Boolean; +/** + * Value Object representing the PseudoType 'False', which is a Boolean type. + */ final class False_ extends Boolean implements PseudoType { public function underlyingType() : Type @@ -19,4 +31,4 @@ public function __toString() : string { return 'false'; } -} \ No newline at end of file +} diff --git a/src/PseudoTypes/True_.php b/src/PseudoTypes/True_.php index 21d46fd..63f7595 100644 --- a/src/PseudoTypes/True_.php +++ b/src/PseudoTypes/True_.php @@ -2,12 +2,24 @@ declare(strict_types=1); +/** + * This file is part of phpDocumentor. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @link http://phpdoc.org + */ + namespace phpDocumentor\Reflection\PseudoTypes; use phpDocumentor\Reflection\PseudoType; use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Types\Boolean; +/** + * Value Object representing the PseudoType 'False', which is a Boolean type. + */ final class True_ extends Boolean implements PseudoType { public function underlyingType() : Type @@ -19,4 +31,4 @@ public function __toString() : string { return 'true'; } -} \ No newline at end of file +} From 5d82f7903e901380d00702e483b61b91bdd8f757 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Sat, 22 Aug 2020 11:37:05 +0200 Subject: [PATCH 05/12] Added immutable annotations --- src/PseudoTypes/False_.php | 2 ++ src/PseudoTypes/True_.php | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/PseudoTypes/False_.php b/src/PseudoTypes/False_.php index 6193d3b..c01dae3 100644 --- a/src/PseudoTypes/False_.php +++ b/src/PseudoTypes/False_.php @@ -19,6 +19,8 @@ /** * Value Object representing the PseudoType 'False', which is a Boolean type. + * + * @psalm-immutable */ final class False_ extends Boolean implements PseudoType { diff --git a/src/PseudoTypes/True_.php b/src/PseudoTypes/True_.php index 63f7595..16bd6a8 100644 --- a/src/PseudoTypes/True_.php +++ b/src/PseudoTypes/True_.php @@ -8,7 +8,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @link http://phpdoc.org + * @link http://phpdoc.org */ namespace phpDocumentor\Reflection\PseudoTypes; @@ -19,6 +19,8 @@ /** * Value Object representing the PseudoType 'False', which is a Boolean type. + * + * @psalm-immutable */ final class True_ extends Boolean implements PseudoType { From 316ba16d4e93f4a9e6163ab1e39ae4fb4f374a8a Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Sat, 22 Aug 2020 11:40:19 +0200 Subject: [PATCH 06/12] Add test for Boolean type too --- tests/unit/Types/BooleanTest.php | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/unit/Types/BooleanTest.php diff --git a/tests/unit/Types/BooleanTest.php b/tests/unit/Types/BooleanTest.php new file mode 100644 index 0000000..a46ee29 --- /dev/null +++ b/tests/unit/Types/BooleanTest.php @@ -0,0 +1,33 @@ +assertSame('bool', (string) $type); + } +} From a883b342718f1ce204e0d8a13cd49ed1fa731d8e Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Sat, 22 Aug 2020 11:48:04 +0200 Subject: [PATCH 07/12] Retarget to 1.x and fix conflicts --- src/TypeResolver.php | 2 +- src/Types/False_.php | 29 ----------------------------- src/Types/True_.php | 29 ----------------------------- tests/unit/TypeResolverTest.php | 4 ++-- 4 files changed, 3 insertions(+), 61 deletions(-) delete mode 100644 src/Types/False_.php delete mode 100644 src/Types/True_.php diff --git a/src/TypeResolver.php b/src/TypeResolver.php index 0e07403..4d65516 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -88,7 +88,7 @@ final class TypeResolver 'callback' => Types\Callable_::class, 'callable' => Types\Callable_::class, 'false' => PseudoTypes\False_::class, - 'true' => PseudoTypes\False_::class, + 'true' => PseudoTypes\True_::class, 'self' => Types\Self_::class, '$this' => Types\This::class, 'static' => Types\Static_::class, diff --git a/src/Types/False_.php b/src/Types/False_.php deleted file mode 100644 index 39666fb..0000000 --- a/src/Types/False_.php +++ /dev/null @@ -1,29 +0,0 @@ - Date: Sat, 22 Aug 2020 12:07:01 +0200 Subject: [PATCH 08/12] Fix linting and pre-commit test --- Makefile | 4 ++-- tests/unit/Types/BooleanTest.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 763adce..f3e88f2 100644 --- a/Makefile +++ b/Makefile @@ -19,14 +19,14 @@ phpcs: phpcbf: docker run -it --rm -v${PWD}:/opt/project -w /opt/project phpdoc/phpcs-ga:latest phpcbf - .PHONY: phpstan phpstan: docker run -it --rm -v${PWD}:/opt/project -w /opt/project phpdoc/phpstan-ga:latest analyse src --no-progress --configuration phpstan.neon -.PHONY: psaml +.PHONY: psalm psalm: docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.2 tools/psalm + .PHONY: test test: docker run -it --rm -v${PWD}:/opt/project -w /opt/project php:7.2 tools/phpunit diff --git a/tests/unit/Types/BooleanTest.php b/tests/unit/Types/BooleanTest.php index a46ee29..9e2b0e2 100644 --- a/tests/unit/Types/BooleanTest.php +++ b/tests/unit/Types/BooleanTest.php @@ -13,7 +13,6 @@ namespace phpDocumentor\Reflection\Types; -use phpDocumentor\Reflection\Types\Boolean; use PHPUnit\Framework\TestCase; /** From 65b307038b240dd320e09a1fe0254b7ba53a046f Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Mon, 24 Aug 2020 13:04:58 +0200 Subject: [PATCH 09/12] Support deprecated FQSENs for BC --- src/PseudoTypes/False_.php | 2 ++ src/PseudoTypes/True_.php | 2 ++ tests/unit/PseudoTypes/FalseTest.php | 10 ++++++++++ tests/unit/PseudoTypes/TrueTest.php | 10 ++++++++++ 4 files changed, 24 insertions(+) diff --git a/src/PseudoTypes/False_.php b/src/PseudoTypes/False_.php index c01dae3..26afb33 100644 --- a/src/PseudoTypes/False_.php +++ b/src/PseudoTypes/False_.php @@ -34,3 +34,5 @@ public function __toString() : string return 'false'; } } + +class_alias('\phpDocumentor\Reflection\PseudoTypes\False_', 'phpDocumentor\Reflection\Types\False_', false); \ No newline at end of file diff --git a/src/PseudoTypes/True_.php b/src/PseudoTypes/True_.php index 16bd6a8..474607b 100644 --- a/src/PseudoTypes/True_.php +++ b/src/PseudoTypes/True_.php @@ -34,3 +34,5 @@ public function __toString() : string return 'true'; } } + +class_alias('\phpDocumentor\Reflection\PseudoTypes\True_', 'phpDocumentor\Reflection\Types\True_', false); \ No newline at end of file diff --git a/tests/unit/PseudoTypes/FalseTest.php b/tests/unit/PseudoTypes/FalseTest.php index 9ab7ffc..6e22351 100644 --- a/tests/unit/PseudoTypes/FalseTest.php +++ b/tests/unit/PseudoTypes/FalseTest.php @@ -40,4 +40,14 @@ public function testFalseStringifyCorrectly() : void $this->assertSame('false', (string) $false); } + + /** + * @covers \phpDocumentor\Reflection\PseudoTypes\False_ + */ + public function testCanBeInstantiatedUsingDeprecatedFqsen() : void + { + $false = new \phpDocumentor\Reflection\Types\False_(); + + $this->assertSame('false', (string) $false); + } } diff --git a/tests/unit/PseudoTypes/TrueTest.php b/tests/unit/PseudoTypes/TrueTest.php index 00deea4..518225d 100644 --- a/tests/unit/PseudoTypes/TrueTest.php +++ b/tests/unit/PseudoTypes/TrueTest.php @@ -40,4 +40,14 @@ public function testTrueStringifyCorrectly() : void $this->assertSame('true', (string) $true); } + + /** + * @covers \phpDocumentor\Reflection\PseudoTypes\True_ + */ + public function testCanBeInstantiatedUsingDeprecatedFqsen() : void + { + $true = new \phpDocumentor\Reflection\Types\True_(); + + $this->assertSame('true', (string) $true); + } } From 069ab05252275a9adaf623601589e7a98c048a02 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Tue, 25 Aug 2020 07:50:39 +0200 Subject: [PATCH 10/12] Fix styling issues --- src/PseudoTypes/False_.php | 5 +++-- src/PseudoTypes/True_.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PseudoTypes/False_.php b/src/PseudoTypes/False_.php index 26afb33..fea5baa 100644 --- a/src/PseudoTypes/False_.php +++ b/src/PseudoTypes/False_.php @@ -8,7 +8,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @link http://phpdoc.org + * @link https://phpdoc.org */ namespace phpDocumentor\Reflection\PseudoTypes; @@ -16,6 +16,7 @@ use phpDocumentor\Reflection\PseudoType; use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Types\Boolean; +use function class_alias; /** * Value Object representing the PseudoType 'False', which is a Boolean type. @@ -35,4 +36,4 @@ public function __toString() : string } } -class_alias('\phpDocumentor\Reflection\PseudoTypes\False_', 'phpDocumentor\Reflection\Types\False_', false); \ No newline at end of file +class_alias('\phpDocumentor\Reflection\PseudoTypes\False_', 'phpDocumentor\Reflection\Types\False_', false); diff --git a/src/PseudoTypes/True_.php b/src/PseudoTypes/True_.php index 474607b..766c26c 100644 --- a/src/PseudoTypes/True_.php +++ b/src/PseudoTypes/True_.php @@ -8,7 +8,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * - * @link http://phpdoc.org + * @link https://phpdoc.org */ namespace phpDocumentor\Reflection\PseudoTypes; @@ -16,6 +16,7 @@ use phpDocumentor\Reflection\PseudoType; use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Types\Boolean; +use function class_alias; /** * Value Object representing the PseudoType 'False', which is a Boolean type. @@ -35,4 +36,4 @@ public function __toString() : string } } -class_alias('\phpDocumentor\Reflection\PseudoTypes\True_', 'phpDocumentor\Reflection\Types\True_', false); \ No newline at end of file +class_alias('\phpDocumentor\Reflection\PseudoTypes\True_', 'phpDocumentor\Reflection\Types\True_', false); From 068f4b3b078e0d0106e7f33102bd43f3452b1300 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Tue, 25 Aug 2020 07:58:55 +0200 Subject: [PATCH 11/12] Disable phpcs sniff for BC compat In order to keep BC compatiblity, I had to introduce class_alias calls in the True_ and False_ classes. This has a side-effect, which the phpcs fails on. --- phpcs.xml.dist | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 1cb3002..82280e1 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,6 +1,6 @@ - The coding standard for phpDocumentor. + The coding standard for phpDocumentor. src tests/unit @@ -13,4 +13,9 @@ */src/*/Abstract*.php + + + */src/PseudoTypes/False_.php + */src/PseudoTypes/True_.php + From c5c988947109bfc610d3ea2b636eb197c3335482 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Wed, 2 Sep 2020 23:21:45 +0200 Subject: [PATCH 12/12] Add extra check for BC --- tests/unit/PseudoTypes/FalseTest.php | 2 ++ tests/unit/PseudoTypes/TrueTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/unit/PseudoTypes/FalseTest.php b/tests/unit/PseudoTypes/FalseTest.php index 6e22351..5367e24 100644 --- a/tests/unit/PseudoTypes/FalseTest.php +++ b/tests/unit/PseudoTypes/FalseTest.php @@ -49,5 +49,7 @@ public function testCanBeInstantiatedUsingDeprecatedFqsen() : void $false = new \phpDocumentor\Reflection\Types\False_(); $this->assertSame('false', (string) $false); + $this->assertInstanceOf(False_::class, $false); + $this->assertInstanceOf(\phpDocumentor\Reflection\Types\False_::class, $false); } } diff --git a/tests/unit/PseudoTypes/TrueTest.php b/tests/unit/PseudoTypes/TrueTest.php index 518225d..d1d5b16 100644 --- a/tests/unit/PseudoTypes/TrueTest.php +++ b/tests/unit/PseudoTypes/TrueTest.php @@ -49,5 +49,7 @@ public function testCanBeInstantiatedUsingDeprecatedFqsen() : void $true = new \phpDocumentor\Reflection\Types\True_(); $this->assertSame('true', (string) $true); + $this->assertInstanceOf(True_::class, $true); + $this->assertInstanceOf(\phpDocumentor\Reflection\Types\True_::class, $true); } }