-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - Backward Compatible PHPStan API rules
- Loading branch information
1 parent
184bf9e
commit 8a05e0d
Showing
13 changed files
with
279 additions
and
1 deletion.
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
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,80 @@ | ||
<?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\Expr\New_> | ||
*/ | ||
class ApiInstantiationRule 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\Expr\New_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->apiRuleHelper->isInPhpStanNamespace($scope->getNamespace())) { | ||
return []; | ||
} | ||
|
||
if (!$node->class instanceof Node\Name) { | ||
return []; | ||
} | ||
|
||
$className = $scope->resolveName($node->class); | ||
if (!$this->reflectionProvider->hasClass($className)) { | ||
return []; | ||
} | ||
|
||
$classReflection = $this->reflectionProvider->getClass($className); | ||
if (!$this->apiRuleHelper->isInPhpStanNamespace($classReflection->getName())) { | ||
return []; | ||
} | ||
|
||
$ruleError = RuleErrorBuilder::message(sprintf( | ||
'Creating new %s is not covered by backward compatibility promise. The class might change in a minor PHPStan version.', | ||
$classReflection->getDisplayName() | ||
))->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' | ||
))->build(); | ||
|
||
if (!$classReflection->hasConstructor()) { | ||
return [$ruleError]; | ||
} | ||
|
||
$constructor = $classReflection->getConstructor(); | ||
$docComment = $constructor->getDocComment(); | ||
if ($docComment === null) { | ||
return [$ruleError]; | ||
} | ||
|
||
if (strpos($docComment, '@api') === false) { | ||
return [$ruleError]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
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,21 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
class ApiRuleHelper | ||
{ | ||
|
||
public function isInPhpStanNamespace(?string $namespace): bool | ||
{ | ||
if ($namespace === null) { | ||
return false; | ||
} | ||
|
||
if (strtolower($namespace) === 'phpstan') { | ||
return true; | ||
} | ||
|
||
return stripos($namespace, 'PHPStan\\') === 0; | ||
} | ||
|
||
} |
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
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,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ApiInstantiationRule> | ||
*/ | ||
class ApiInstantiationRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new ApiInstantiationRule( | ||
new ApiRuleHelper(), | ||
$this->createReflectionProvider() | ||
); | ||
} | ||
|
||
public function testRuleInPhpStan(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/new-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/new-out-of-phpstan.php'], [ | ||
[ | ||
'Creating new PHPStan\Type\FileTypeMapper is not covered by backward compatibility promise. The class might change in a minor PHPStan version.', | ||
17, | ||
$tip, | ||
], | ||
[ | ||
'Creating new PHPStan\DependencyInjection\NeonAdapter is not covered by backward compatibility promise. The class might change in a minor PHPStan version.', | ||
18, | ||
$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,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class ApiRuleHelperTest extends TestCase | ||
{ | ||
|
||
public function dataIsInPhpStanNamespace(): array | ||
{ | ||
return [ | ||
[ | ||
null, | ||
false, | ||
], | ||
[ | ||
'PHPStan', | ||
true, | ||
], | ||
[ | ||
'PhpStan', | ||
true, | ||
], | ||
[ | ||
'PHPStan\\Foo', | ||
true, | ||
], | ||
[ | ||
'PhpStan\\Foo', | ||
true, | ||
], | ||
[ | ||
'App\\Foo', | ||
false, | ||
], | ||
[ | ||
'PHPStanWorkshop', | ||
false, | ||
], | ||
[ | ||
'PHPStanWorkshop\\', | ||
false, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataIsInPhpStanNamespace | ||
* @param string|null $namespace | ||
* @param bool $expected | ||
*/ | ||
public function testIsInPhpStanNamespace(?string $namespace, bool $expected): void | ||
{ | ||
$rule = new ApiRuleHelper(); | ||
$this->assertSame($expected, $rule->isInPhpStanNamespace($namespace)); | ||
} | ||
|
||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace PHPStan\Foo; | ||
|
||
use PHPStan\DependencyInjection\NeonAdapter; | ||
use PHPStan\Type\FileTypeMapper; | ||
use PHPStan\Type\IntegerType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
new Nonexistent(); | ||
new Bar(); | ||
new IntegerType(); | ||
new FileTypeMapper(); | ||
new NeonAdapter(); | ||
} | ||
|
||
} | ||
|
||
class Bar | ||
{ | ||
|
||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use PHPStan\DependencyInjection\NeonAdapter; | ||
use PHPStan\Type\FileTypeMapper; | ||
use PHPStan\Type\IntegerType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
new Nonexistent(); | ||
new Bar(); | ||
new IntegerType(); | ||
new FileTypeMapper(); // error - has constructor | ||
new NeonAdapter(); // error - does not have a constructor | ||
} | ||
|
||
} | ||
|
||
class Bar | ||
{ | ||
|
||
} |