-
Notifications
You must be signed in to change notification settings - Fork 0
Added optional MockObject rules and fixtures to phpstan #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f338082
Fixed PHPStan rule error identifiers
mikadamczyk e492dc2
Updated identifier for closure return type rule error message
mikadamczyk 332ae1a
Added optional MockObject rules and fixtures to phpstan
mikadamczyk 1306295
Added opt-in MockObject rules and fixtures
mikadamczyk 85258a8
Normalized PHPStan rule identifiers
mikadamczyk 42c5f02
Adjusted mock-return rule to require concrete type
mikadamczyk c5c2920
Added .gitignore to keep local caches and vendor out of version control
mikadamczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 @@ | ||
| /vendor/ | ||
| /.php-cs-fixer.cache | ||
| /.phpunit.result.cache | ||
| /composer.lock |
This file contains hidden or 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,3 @@ | ||
| rules: | ||
| - Ibexa\PHPStan\Rules\RequireMockObjectInPropertyTypeRule | ||
| - Ibexa\PHPStan\Rules\RequireConcreteTypeForMockReturnRule |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,4 +3,6 @@ parameters: | |
| paths: | ||
| - rules | ||
| - tests | ||
| excludePaths: | ||
| - tests/rules/Fixtures/* | ||
| checkMissingCallableSignature: true | ||
This file contains hidden or 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 hidden or 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 hidden or 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,170 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\PHPStan\Rules; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\StaticCall; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\IntersectionType; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\NullableType; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\UnionType; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
|
|
||
| /** | ||
| * @implements Rule<ClassMethod> | ||
| */ | ||
| final readonly class RequireConcreteTypeForMockReturnRule implements Rule | ||
| { | ||
| public function getNodeType(): string | ||
| { | ||
| return ClassMethod::class; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<\PHPStan\Rules\IdentifierRuleError> | ||
| */ | ||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if ($node->returnType === null || $node->stmts === null) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$this->returnsMock($node)) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$this->typeNodeIsMockObjectOnly($node->returnType)) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message('Method returns a mock and declares only MockObject as return type. Use an intersection with a concrete type.') | ||
| ->identifier('Ibexa.requireConcreteTypeForMockReturn') | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| private function returnsMock(ClassMethod $node): bool | ||
| { | ||
| $mockVariables = []; | ||
| foreach ($node->getStmts() ?? [] as $stmt) { | ||
| if ($stmt instanceof Node\Stmt\Expression && $stmt->expr instanceof Node\Expr\Assign) { | ||
| $assign = $stmt->expr; | ||
| if ($assign->var instanceof Variable && is_string($assign->var->name)) { | ||
| if ($assign->expr instanceof MethodCall && $this->isCreateMockCall($assign->expr)) { | ||
| $mockVariables[$assign->var->name] = true; | ||
| } | ||
|
|
||
| if ($assign->expr instanceof StaticCall && $this->isCreateMockCall($assign->expr)) { | ||
| $mockVariables[$assign->var->name] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!$stmt instanceof Node\Stmt\Return_ || $stmt->expr === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $expr = $stmt->expr; | ||
| if ($expr instanceof MethodCall && $this->isCreateMockCall($expr)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($expr instanceof StaticCall && $this->isCreateMockCall($expr)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($expr instanceof Variable && is_string($expr->name) && isset($mockVariables[$expr->name])) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $call | ||
| */ | ||
| private function isCreateMockCall(Node $call): bool | ||
| { | ||
| if (!$call->name instanceof Node\Identifier) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($call->name->toString() !== 'createMock') { | ||
| return false; | ||
| } | ||
|
|
||
| if ($call instanceof MethodCall) { | ||
| return $call->var instanceof Variable && $call->var->name === 'this'; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private function typeNodeIsMockObjectOnly(Node $type): bool | ||
| { | ||
| if ($type instanceof NullableType) { | ||
| return $this->typeNodeIsMockObjectOnly($type->type); | ||
| } | ||
|
|
||
| if ($type instanceof IntersectionType) { | ||
| $hasMockObject = false; | ||
| foreach ($type->types as $innerType) { | ||
| if ($this->isMockObjectType($innerType)) { | ||
| $hasMockObject = true; | ||
| continue; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return $hasMockObject; | ||
| } | ||
|
|
||
| if ($type instanceof UnionType) { | ||
| $hasMockObject = false; | ||
| foreach ($type->types as $innerType) { | ||
| if ($innerType instanceof Name && $innerType->getLast() === 'null') { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->isMockObjectType($innerType)) { | ||
| $hasMockObject = true; | ||
| continue; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return $hasMockObject; | ||
| } | ||
|
|
||
| return $this->isMockObjectType($type); | ||
| } | ||
|
|
||
| private function isMockObjectType(Node $type): bool | ||
| { | ||
| if ($type instanceof Identifier) { | ||
| return $type->toString() === 'MockObject'; | ||
| } | ||
|
|
||
| if ($type instanceof Name) { | ||
| return $type->getLast() === 'MockObject'; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
This file contains hidden or 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,92 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\PHPStan\Rules; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\IntersectionType; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\NullableType; | ||
| use PhpParser\Node\Stmt\Property; | ||
| use PhpParser\Node\UnionType; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
|
|
||
| /** | ||
| * @implements Rule<Property> | ||
| */ | ||
| final readonly class RequireMockObjectInPropertyTypeRule implements Rule | ||
| { | ||
| public function getNodeType(): string | ||
| { | ||
| return Property::class; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<\PHPStan\Rules\IdentifierRuleError> | ||
| */ | ||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if ($node->type === null) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$this->docCommentIncludesMockObject($node)) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($this->typeNodeIncludesMockObject($node->type)) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message('Property typed as MockObject only in PHPDoc. Use intersection type with MockObject.') | ||
| ->identifier('Ibexa.requireMockObjectPropertyType') | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| private function typeNodeIncludesMockObject(Node $type): bool | ||
| { | ||
| if ($type instanceof NullableType) { | ||
| return $this->typeNodeIncludesMockObject($type->type); | ||
| } | ||
|
|
||
| if ($type instanceof UnionType || $type instanceof IntersectionType) { | ||
| foreach ($type->types as $innerType) { | ||
| if ($this->typeNodeIncludesMockObject($innerType)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| if ($type instanceof Identifier) { | ||
| return $type->toString() === 'MockObject'; | ||
| } | ||
|
|
||
| if ($type instanceof Name) { | ||
| return $type->getLast() === 'MockObject'; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private function docCommentIncludesMockObject(Property $property): bool | ||
| { | ||
| $docComment = $property->getDocComment(); | ||
| if ($docComment === null) { | ||
| return false; | ||
| } | ||
|
|
||
| return preg_match('/@var\\s+[^\\n]*MockObject/', $docComment->getText()) === 1; | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
tests/rules/Fixtures/RequireConcreteTypeForMockReturnFixture.php
This file contains hidden or 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 | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\PHPStan\Rules\Fixtures; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class ConcreteMockReturnTypeFixture extends TestCase | ||
| { | ||
| private function createFoo(): Foo | ||
| { | ||
| $foo = $this->createMock(Foo::class); | ||
|
|
||
| return $foo; | ||
| } | ||
Steveb-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private function createFooOk(): Foo&MockObject | ||
| { | ||
| return $this->createMock(Foo::class); | ||
| } | ||
|
|
||
| private function createMockObjectOnly(): MockObject | ||
| { | ||
| return $this->createMock(Foo::class); | ||
| } | ||
| } | ||
|
|
||
| final class Foo | ||
| { | ||
| } | ||
|
|
||
| interface MockObject | ||
| { | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
tests/rules/Fixtures/RequireMockObjectInPropertyTypeFixture.php
This file contains hidden or 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,25 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\PHPStan\Rules\Fixtures; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class PropertyMockTypeTest extends TestCase | ||
mikadamczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /** @var Foo&MockObject */ | ||
| private Foo $foo; | ||
| } | ||
|
|
||
| final class Foo | ||
| { | ||
| } | ||
|
|
||
| interface MockObject | ||
| { | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.