This repository was archived by the owner on Aug 11, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExpectationSemanticAnalyzer.php
More file actions
107 lines (86 loc) · 3.61 KB
/
Copy pathExpectationSemanticAnalyzer.php
File metadata and controls
107 lines (86 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace PestStan\Analysis\Expectation;
use PestStan\Diagnostics\PestDiagnostic;
use PestStan\Diagnostics\PestDiagnostics;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Type\VerbosityLevel;
final class ExpectationSemanticAnalyzer
{
private readonly ExpectationChainStateResolver $chainStateResolver;
public function __construct(?ExpectationChainStateResolver $chainStateResolver = null)
{
$this->chainStateResolver = $chainStateResolver ?? new ExpectationChainStateResolver;
}
public function analyzeInvalidMatcherType(MethodCall $methodCall, Scope $scope): ?PestDiagnostic
{
$state = $this->chainStateResolver->resolve($methodCall, $scope);
if (! $state instanceof ExpectationChainState || ! $state->stepResult instanceof ExpectationAssertionResult) {
return null;
}
if (! $state->stepResult->isInvalidRequirement() || ! $state->stepResult->metadata instanceof MatcherSemanticMetadata) {
return null;
}
if ($state->stepResult->metadata->requirement === null) {
return null;
}
return PestDiagnostics::invalidMatcherType(
$state->stepResult->methodName,
$state->stepResult->incomingValueType->describe(VerbosityLevel::typeOnly()),
$state->stepResult->metadata->requirement,
);
}
public function analyzeImpossibleExpectation(MethodCall $methodCall, Scope $scope): ?PestDiagnostic
{
$state = $this->chainStateResolver->resolve($methodCall, $scope);
if (! $state instanceof ExpectationChainState || ! $state->stepResult instanceof ExpectationAssertionResult) {
return null;
}
if (! $state->stepResult->isImpossible()) {
return null;
}
return PestDiagnostics::impossibleExpectation(
$state->stepResult->methodName,
$state->stepResult->incomingValueType->describe(VerbosityLevel::typeOnly()),
);
}
public function analyzeRedundantExpectation(MethodCall $methodCall, Scope $scope): ?PestDiagnostic
{
$state = $this->chainStateResolver->resolve($methodCall, $scope);
if (! $state instanceof ExpectationChainState || ! $state->stepResult instanceof ExpectationAssertionResult) {
return null;
}
if (! $state->stepResult->isRedundant()) {
return null;
}
if ($this->shouldSuppressRedundantExpectation($methodCall, $scope)) {
return null;
}
return PestDiagnostics::redundantExpectation(
$state->stepResult->methodName,
$state->stepResult->incomingValueType->describe(VerbosityLevel::typeOnly()),
);
}
private function shouldSuppressRedundantExpectation(MethodCall $methodCall, Scope $scope): bool
{
if (! $methodCall->var instanceof MethodCall) {
return false;
}
$previousState = $this->chainStateResolver->resolve($methodCall->var, $scope);
if (! $previousState instanceof ExpectationChainState || ! $previousState->stepResult instanceof ExpectationAssertionResult) {
return false;
}
if (! $previousState->stepResult->metadata instanceof MatcherSemanticMetadata) {
return false;
}
if ($previousState->stepResult->metadata->assertion === null) {
return false;
}
return in_array(
$previousState->stepResult->status,
[ExpectationAssertionResult::ASSERTED, ExpectationAssertionResult::REDUNDANT],
true,
);
}
}