Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d2b44b

Browse files
InvocationMocker class no longer exists
1 parent 1f36fc5 commit 4d2b44b

File tree

6 files changed

+43
-127
lines changed

6 files changed

+43
-127
lines changed

‎extension.neon

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ parameters:
1212
- stubs/Assert.stub
1313
- stubs/AssertionFailedError.stub
1414
- stubs/ExpectationFailedException.stub
15-
- stubs/InvocationMocker.stub
1615
- stubs/MockBuilder.stub
1716
- stubs/MockObject.stub
1817
- stubs/Stub.stub
@@ -42,18 +41,10 @@ services:
4241
class: PHPStan\Type\PHPUnit\Assert\AssertStaticMethodTypeSpecifyingExtension
4342
tags:
4443
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
45-
-
46-
class: PHPStan\Type\PHPUnit\InvocationMockerDynamicReturnTypeExtension
47-
tags:
48-
- phpstan.broker.dynamicMethodReturnTypeExtension
4944
-
5045
class: PHPStan\Type\PHPUnit\MockBuilderDynamicReturnTypeExtension
5146
tags:
5247
- phpstan.broker.dynamicMethodReturnTypeExtension
53-
-
54-
class: PHPStan\Type\PHPUnit\MockObjectDynamicReturnTypeExtension
55-
tags:
56-
- phpstan.broker.dynamicMethodReturnTypeExtension
5748
-
5849
class: PHPStan\Rules\PHPUnit\CoversHelper
5950
-

‎src/Rules/PHPUnit/MockMethodCallRule.php

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\MethodCall;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\IdentifierRuleError;
89
use PHPStan\Rules\Rule;
910
use PHPStan\Rules\RuleErrorBuilder;
10-
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
11+
use PHPStan\Type\Type;
1112
use PHPUnit\Framework\MockObject\MockObject;
1213
use PHPUnit\Framework\MockObject\Stub;
1314
use function array_filter;
@@ -47,44 +48,58 @@ public function processNode(Node $node, Scope $scope): array
4748
$method = $constantString->getValue();
4849
$type = $scope->getType($node->var);
4950

50-
if (
51-
(
52-
in_array(MockObject::class, $type->getObjectClassNames(), true)
53-
|| in_array(Stub::class, $type->getObjectClassNames(), true)
54-
)
55-
&& !$type->hasMethod($method)->yes()
56-
) {
57-
$mockClasses = array_filter($type->getObjectClassNames(), static fn (string $class): bool => $class !== MockObject::class && $class !== Stub::class);
58-
if (count($mockClasses) === 0) {
59-
continue;
60-
}
61-
62-
$errors[] = RuleErrorBuilder::message(sprintf(
63-
'Trying to mock an undefined method %s() on class %s.',
64-
$method,
65-
implode('&', $mockClasses),
66-
))->identifier('phpunit.mockMethod')->build();
51+
$error = $this->checkCallOnType($type, $method);
52+
if ($error !== null) {
53+
$errors[] = $error;
6754
continue;
6855
}
6956

70-
$mockedClassObject = $type->getTemplateType(InvocationMocker::class, 'TMockedClass');
71-
if ($mockedClassObject->hasMethod($method)->yes()) {
57+
if (!$node->var instanceof MethodCall) {
7258
continue;
7359
}
7460

75-
$classNames = $mockedClassObject->getObjectClassNames();
76-
if (count($classNames) === 0) {
61+
if (!$node->var->name instanceof Node\Identifier) {
7762
continue;
7863
}
7964

80-
$errors[] = RuleErrorBuilder::message(sprintf(
65+
if ($node->var->name->toLowerString() !== 'expects') {
66+
continue;
67+
}
68+
69+
$varType = $scope->getType($node->var->var);
70+
$error = $this->checkCallOnType($varType, $method);
71+
if ($error === null) {
72+
continue;
73+
}
74+
75+
$errors[] = $error;
76+
}
77+
78+
return $errors;
79+
}
80+
81+
private function checkCallOnType(Type $type, string $method): ?IdentifierRuleError
82+
{
83+
if (
84+
(
85+
in_array(MockObject::class, $type->getObjectClassNames(), true)
86+
|| in_array(Stub::class, $type->getObjectClassNames(), true)
87+
)
88+
&& !$type->hasMethod($method)->yes()
89+
) {
90+
$mockClasses = array_filter($type->getObjectClassNames(), static fn (string $class): bool => $class !== MockObject::class && $class !== Stub::class);
91+
if (count($mockClasses) === 0) {
92+
return null;
93+
}
94+
95+
return RuleErrorBuilder::message(sprintf(
8196
'Trying to mock an undefined method %s() on class %s.',
8297
$method,
83-
implode('|', $classNames),
98+
implode('&', $mockClasses),
8499
))->identifier('phpunit.mockMethod')->build();
85100
}
86101

87-
return $errors;
102+
return null;
88103
}
89104

90105
}

‎src/Type/PHPUnit/InvocationMockerDynamicReturnTypeExtension.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎src/Type/PHPUnit/MockObjectDynamicReturnTypeExtension.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎stubs/InvocationMocker.stub

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎tests/Rules/PHPUnit/MockMethodCallRuleTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7-
use function interface_exists;
87

98
/**
109
* @extends RuleTestCase<MockMethodCallRule>
@@ -28,14 +27,11 @@ public function testRule(): void
2827
'Trying to mock an undefined method doBadThing() on class MockMethodCall\Bar.',
2928
20,
3029
],
31-
];
32-
33-
if (interface_exists('PHPUnit\Framework\MockObject\Builder\InvocationStubber')) {
34-
$expectedErrors[] = [
30+
[
3531
'Trying to mock an undefined method doBadThing() on class MockMethodCall\Bar.',
3632
36,
37-
];
38-
}
33+
],
34+
];
3935

4036
$this->analyse([__DIR__ . '/data/mock-method-call.php'], $expectedErrors);
4137
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /