-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |