From 567299677857c096cd2a0dea04a9ea9d4d7a5c97 Mon Sep 17 00:00:00 2001 From: Marc Beinder Date: Wed, 2 Oct 2024 11:32:28 -0500 Subject: [PATCH] [PHPStan] Create PHPStan Extension --- .idea/php.xml | 2 -- .idea/stdlib.iml | 2 -- composer.json | 11 ++++-- md5.txt | 1 - .../PHPStan/Rules/Functions/NotNull.php | 23 +++++++++++++ src/Support/PHPStan/extension.neon | 5 +++ .../PHPStan/Rules/Functions/NotNullTest.php | 34 +++++++++++++++++++ 7 files changed, 71 insertions(+), 7 deletions(-) delete mode 100644 md5.txt create mode 100644 src/Support/PHPStan/Rules/Functions/NotNull.php create mode 100644 src/Support/PHPStan/extension.neon create mode 100644 tests/Unit/PHPStan/Rules/Functions/NotNullTest.php diff --git a/.idea/php.xml b/.idea/php.xml index 54a7d89..7ef2f4f 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -26,7 +26,6 @@ - @@ -41,7 +40,6 @@ - diff --git a/.idea/stdlib.iml b/.idea/stdlib.iml index b79b755..b1830d2 100644 --- a/.idea/stdlib.iml +++ b/.idea/stdlib.iml @@ -7,7 +7,6 @@ - @@ -24,7 +23,6 @@ - diff --git a/composer.json b/composer.json index 910f333..94edb2b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "license": "proprietary", "config": { - "optimize-autoloader": true, + "optimize-autoloader": false, "preferred-install": "dist", "sort-packages": true, "allow-plugins": { @@ -19,6 +19,7 @@ "ramsey/uuid": "^4.7" }, "require-dev": { + "mockery/mockery": "^1.6", "pestphp/pest": "^2.34 || ^3.0", "phpstan/extension-installer": "^1.3", "phpstan/phpstan": "^1.10", @@ -45,5 +46,11 @@ ] }, "minimum-stability": "stable", - "extra": {} + "extra": { + "phpstan": { + "includes": [ + "src/Support/PHPStan/extension.neon" + ] + } + } } diff --git a/md5.txt b/md5.txt deleted file mode 100644 index e781305..0000000 --- a/md5.txt +++ /dev/null @@ -1 +0,0 @@ -d41d8cd98f00b204e9800998ecf8427e \ No newline at end of file diff --git a/src/Support/PHPStan/Rules/Functions/NotNull.php b/src/Support/PHPStan/Rules/Functions/NotNull.php new file mode 100644 index 0000000..0eed211 --- /dev/null +++ b/src/Support/PHPStan/Rules/Functions/NotNull.php @@ -0,0 +1,23 @@ +getName() === 'not_null'; + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, Node\Expr\FuncCall $functionCall, Scope $scope): Type + { + return new BooleanType(); + } +} \ No newline at end of file diff --git a/src/Support/PHPStan/extension.neon b/src/Support/PHPStan/extension.neon new file mode 100644 index 0000000..bf243cf --- /dev/null +++ b/src/Support/PHPStan/extension.neon @@ -0,0 +1,5 @@ +services: + - + class: EncoreDigitalGroup\StdLib\Support\PHPStan\Rules\TypeSpecifiers\Function_NotNull + tags: + - phpstan.broker.dynamicFunctionReturnTypeExtension \ No newline at end of file diff --git a/tests/Unit/PHPStan/Rules/Functions/NotNullTest.php b/tests/Unit/PHPStan/Rules/Functions/NotNullTest.php new file mode 100644 index 0000000..f95df80 --- /dev/null +++ b/tests/Unit/PHPStan/Rules/Functions/NotNullTest.php @@ -0,0 +1,34 @@ +shouldReceive('getName')->andReturn('not_null'); + + $notNull = new NotNull(); + expect($notNull->isFunctionSupported($functionReflection))->toBeTrue(); +}); + +test('NotNull does not support other functions', function () { + $functionReflection = Mockery::mock(FunctionReflection::class); + $functionReflection->shouldReceive('getName')->andReturn('other_function'); + + $notNull = new NotNull(); + expect($notNull->isFunctionSupported($functionReflection))->toBeFalse(); +}); + +it('returns BooleanType from function call', function () { + $functionReflection = Mockery::mock(FunctionReflection::class); + $functionCall = Mockery::mock(Node\Expr\FuncCall::class); + $scope = Mockery::mock(Scope::class); + + $notNull = new NotNull(); + $type = $notNull->getTypeFromFunctionCall($functionReflection, $functionCall, $scope); + + expect($type)->toBeInstanceOf(BooleanType::class); +}); \ No newline at end of file