Skip to content

Commit

Permalink
ReflectionProvider cannot be extended/implemented by 3rd party code
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 18, 2022
1 parent 4a8ccc6 commit e60d6d6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Rules/Api/ApiClassImplementsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Type;
use function array_merge;
use function count;
use function in_array;
use function sprintf;

/**
Expand Down Expand Up @@ -65,7 +66,10 @@ private function checkName(Scope $scope, Node\Name $name): array
'https://github.com/phpstan/phpstan/discussions',
))->build();

if ($implementedClassReflection->getName() === Type::class) {
if (in_array($implementedClassReflection->getName(), [
Type::class,
ReflectionProvider::class,
], true)) {
return [$ruleError];
}

Expand Down
6 changes: 5 additions & 1 deletion src/Rules/Api/ApiInterfaceExtendsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Type;
use function array_merge;
use function count;
use function in_array;
use function sprintf;

/**
Expand Down Expand Up @@ -65,7 +66,10 @@ private function checkName(Scope $scope, Node\Name $name): array
'https://github.com/phpstan/phpstan/discussions',
))->build();

if ($extendedInterfaceReflection->getName() === Type::class) {
if (in_array($extendedInterfaceReflection->getName(), [
Type::class,
ReflectionProvider::class,
], true)) {
return [$ruleError];
}

Expand Down
9 changes: 7 additions & 2 deletions tests/PHPStan/Rules/Api/ApiClassImplementsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public function testRuleOutOfPhpStan(): void
$this->analyse([__DIR__ . '/data/class-implements-out-of-phpstan.php'], [
[
'Implementing PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
17,
18,
$tip,
],
[
'Implementing PHPStan\Type\Type is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
51,
52,
$tip,
],
[
'Implementing PHPStan\Reflection\ReflectionProvider is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
312,
$tip,
],
]);
Expand Down
12 changes: 11 additions & 1 deletion tests/PHPStan/Rules/Api/ApiInterfaceExtendsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ public function testRuleOutOfPhpStan(): void
$this->analyse([__DIR__ . '/data/interface-extends-out-of-phpstan.php'], [
[
'Extending PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
8,
9,
$tip,
],
[
'Extending PHPStan\Type\Type is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
19,
$tip,
],
[
'Extending PHPStan\Reflection\ReflectionProvider is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
24,
$tip,
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Generic\TemplateTypeReference;
Expand Down Expand Up @@ -307,3 +308,8 @@ public static function __set_state(array $properties): \PHPStan\Type\Type


}

abstract class Dolor implements ReflectionProvider
{

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Api/data/interface-extends-out-of-phpstan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\InterfaceExtends;

use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;

interface Foo extends DynamicThrowTypeExtensionProvider
Expand All @@ -14,3 +15,13 @@ interface Bar extends DynamicFunctionThrowTypeExtension
{

}

interface Lorem extends \PHPStan\Type\Type
{

}

interface Dolor extends ReflectionProvider
{

}

0 comments on commit e60d6d6

Please sign in to comment.