-
Notifications
You must be signed in to change notification settings - Fork 471
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
1 parent
995bc29
commit 929d7c4
Showing
5 changed files
with
140 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\TraitUse> | ||
*/ | ||
class ApiTraitUseRule implements Rule | ||
{ | ||
|
||
private ApiRuleHelper $apiRuleHelper; | ||
|
||
private ReflectionProvider $reflectionProvider; | ||
|
||
public function __construct( | ||
ApiRuleHelper $apiRuleHelper, | ||
ReflectionProvider $reflectionProvider | ||
) | ||
{ | ||
$this->apiRuleHelper = $apiRuleHelper; | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\TraitUse::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->apiRuleHelper->isInPhpStanNamespace($scope->getNamespace())) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
$tip = sprintf( | ||
"If you think it should be covered by backward compatibility promise, open a discussion:\n %s\n\n See also:\n https://phpstan.org/developing-extensions/backward-compatibility-promise", | ||
'https://github.com/phpstan/phpstan/discussions' | ||
); | ||
foreach ($node->traits as $traitName) { | ||
$traitName = $traitName->toString(); | ||
if (!$this->reflectionProvider->hasClass($traitName)) { | ||
continue; | ||
} | ||
|
||
$traitReflection = $this->reflectionProvider->getClass($traitName); | ||
if (!$this->apiRuleHelper->isInPhpStanNamespace($traitReflection->getName())) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Using %s is not covered by backward compatibility promise. The trait might change in a minor PHPStan version.', | ||
$traitReflection->getDisplayName() | ||
))->tip($tip)->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ApiTraitUseRule> | ||
*/ | ||
class ApiTraitUseRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new ApiTraitUseRule(new ApiRuleHelper(), $this->createReflectionProvider()); | ||
} | ||
|
||
public function testRuleInPhpStan(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/trait-use-in-phpstan.php'], []); | ||
} | ||
|
||
public function testRuleOutOfPhpStan(): void | ||
{ | ||
$tip = sprintf( | ||
"If you think it should be covered by backward compatibility promise, open a discussion:\n %s\n\n See also:\n https://phpstan.org/developing-extensions/backward-compatibility-promise", | ||
'https://github.com/phpstan/phpstan/discussions' | ||
); | ||
|
||
$this->analyse([__DIR__ . '/data/trait-use-out-of-phpstan.php'], [ | ||
[ | ||
'Using PHPStan\Type\JustNullableTypeTrait is not covered by backward compatibility promise. The trait might change in a minor PHPStan version.', | ||
15, | ||
$tip, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\TraitUse; | ||
|
||
use PHPStan\Type\JustNullableTypeTrait; | ||
|
||
class Foo | ||
{ | ||
|
||
use JustNullableTypeTrait; | ||
|
||
} |
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,18 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace App\TraitUse; | ||
|
||
use PHPStan\Type\JustNullableTypeTrait; | ||
|
||
trait FooTrait | ||
{ | ||
|
||
} | ||
|
||
class Foo | ||
{ | ||
|
||
use JustNullableTypeTrait; | ||
use FooTrait; | ||
|
||
} |