Skip to content

Commit

Permalink
[PHPStan] Make Custom PHPStan Rules Experimental (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: onairmarc <[email protected]>
  • Loading branch information
onairmarc and onairmarc authored Oct 3, 2024
1 parent 6b42aa6 commit 306bfdf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ abb6a720fb031c0cb6aae78b0068e3d31853c8ba
fecc1eb0893900aa5f0e840b8b0c94d2472731c2
c709aedfb70105dd006ffbbbf8f82d9716db9614
c10c3ac2613e54de290a9135afe024036174ef20
aef6f8b59ab2ff054b1a823efe242e0cb0cd1164
9 changes: 1 addition & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,5 @@
"src/ObjectHelpers/val_helpers.php"
]
},
"minimum-stability": "stable",
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
}
}
"minimum-stability": "stable"
}
4 changes: 2 additions & 2 deletions extension.neon
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
23 changes: 0 additions & 23 deletions src/Support/PHPStan/Rules/Functions/NotNull.php

This file was deleted.

49 changes: 49 additions & 0 deletions src/Support/PHPStan/Rules/NotNullTypeSpecifyingExtension.php
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);
}
}

0 comments on commit 306bfdf

Please sign in to comment.