Skip to content

Commit

Permalink
[PHPStan] Create PHPStan Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
onairmarc committed Oct 2, 2024
1 parent e02f51f commit 5672996
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions .idea/stdlib.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "proprietary",
"config": {
"optimize-autoloader": true,
"optimize-autoloader": false,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
Expand All @@ -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",
Expand All @@ -45,5 +46,11 @@
]
},
"minimum-stability": "stable",
"extra": {}
"extra": {
"phpstan": {
"includes": [
"src/Support/PHPStan/extension.neon"
]
}
}
}
1 change: 0 additions & 1 deletion md5.txt

This file was deleted.

23 changes: 23 additions & 0 deletions src/Support/PHPStan/Rules/Functions/NotNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace EncoreDigitalGroup\StdLib\Support\PHPStan\Rules\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;

class NotNull implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'not_null';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, Node\Expr\FuncCall $functionCall, Scope $scope): Type
{
return new BooleanType();
}
}
5 changes: 5 additions & 0 deletions src/Support/PHPStan/extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
-
class: EncoreDigitalGroup\StdLib\Support\PHPStan\Rules\TypeSpecifiers\Function_NotNull
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
34 changes: 34 additions & 0 deletions tests/Unit/PHPStan/Rules/Functions/NotNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use EncoreDigitalGroup\StdLib\Support\PHPStan\Rules\Functions\NotNull;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Analyser\Scope;
use PhpParser\Node;
use PHPStan\Type\BooleanType;

test('NotNull supports the not_null function', function () {
$functionReflection = Mockery::mock(FunctionReflection::class);
$functionReflection->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);
});

0 comments on commit 5672996

Please sign in to comment.