-
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.
[PHPStan] Make Custom PHPStan Rules Experimental (#23)
Co-authored-by: onairmarc <[email protected]>
- Loading branch information
Showing
5 changed files
with
53 additions
and
33 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
services: | ||
- | ||
class: EncoreDigitalGroup\StdLib\Support\PHPStan\Rules\Functions\NotNull | ||
class: EncoreDigitalGroup\StdLib\Support\PHPStan\NotNullTypeSpecifyingExtension | ||
tags: | ||
- phpstan.broker.dynamicFunctionReturnTypeExtension | ||
- phpstan.typeSpecifier.extension |
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/Support/PHPStan/Rules/NotNullTypeSpecifyingExtension.php
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,49 @@ | ||
<?php | ||
|
||
namespace EncoreDigitalGroup\StdLib\Support\PHPStan\Type; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeSpecifier; | ||
use PHPStan\Type\TypeSpecifierAwareExtension; | ||
use PHPStan\Type\TypeSpecifyingExtension; | ||
|
||
/** @experimental */ | ||
class NotNullTypeSpecifyingExtension implements TypeSpecifierAwareExtension, TypeSpecifyingExtension | ||
{ | ||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function isFunctionSupported( | ||
FunctionReflection $functionReflection, | ||
FuncCall $funcCall, | ||
Scope $scope | ||
): bool { | ||
// Support the "not_null" function | ||
return $functionReflection->getName() === 'not_null'; | ||
} | ||
|
||
public function specifyTypes( | ||
FunctionReflection $functionReflection, | ||
FuncCall $funcCall, | ||
Scope $scope, | ||
TypeSpecifierContext $context | ||
): \PHPStan\Type\Specifier\TypeSpecifierResult { | ||
// Get the type of the argument passed to "not_null" | ||
$argExpr = $funcCall->getArgs()[0]->value; | ||
$argType = $scope->getType($argExpr); | ||
|
||
// Remove the nullability from the type | ||
$specifiedType = TypeCombinator::removeNull($argType); | ||
|
||
// Return the modified type | ||
return $this->typeSpecifier->create($argExpr, $specifiedType, $context); | ||
} | ||
} |