Skip to content

Commit 4df1a1b

Browse files
committed
Extract TooWideReturnTypeCheck
1 parent d36a97f commit 4df1a1b

File tree

5 files changed

+64
-43
lines changed

5 files changed

+64
-43
lines changed

src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use PHPStan\DependencyInjection\RegisteredRule;
88
use PHPStan\Node\InArrowFunctionNode;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleErrorBuilder;
1110
use PHPStan\Type\UnionType;
12-
use PHPStan\Type\VerbosityLevel;
13-
use function sprintf;
1411

1512
/**
1613
* @implements Rule<InArrowFunctionNode>
@@ -19,6 +16,12 @@
1916
final class TooWideArrowFunctionReturnTypehintRule implements Rule
2017
{
2118

19+
public function __construct(
20+
private TooWideReturnTypeCheck $check,
21+
)
22+
{
23+
}
24+
2225
public function getNodeType(): string
2326
{
2427
return InArrowFunctionNode::class;
@@ -41,23 +44,7 @@ public function processNode(Node $node, Scope $scope): array
4144
return [];
4245
}
4346

44-
$returnType = $scope->getType($expr);
45-
if ($returnType->isNull()->yes()) {
46-
return [];
47-
}
48-
$messages = [];
49-
foreach ($functionReturnType->getTypes() as $type) {
50-
if (!$type->isSuperTypeOf($returnType)->no()) {
51-
continue;
52-
}
53-
54-
$messages[] = RuleErrorBuilder::message(sprintf(
55-
'Anonymous function never returns %s so it can be removed from the return type.',
56-
$type->describe(VerbosityLevel::getRecommendedLevelByType($type)),
57-
))->identifier('return.unusedType')->build();
58-
}
59-
60-
return $messages;
47+
return $this->check->checkAnonymousFunction($scope->getType($expr), $functionReturnType);
6148
}
6249

6350
}

src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
use PHPStan\DependencyInjection\RegisteredRule;
88
use PHPStan\Node\ClosureReturnStatementsNode;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleErrorBuilder;
1110
use PHPStan\Type\TypeCombinator;
1211
use PHPStan\Type\UnionType;
13-
use PHPStan\Type\VerbosityLevel;
1412
use function count;
15-
use function sprintf;
1613

1714
/**
1815
* @implements Rule<ClosureReturnStatementsNode>
@@ -21,6 +18,12 @@
2118
final class TooWideClosureReturnTypehintRule implements Rule
2219
{
2320

21+
public function __construct(
22+
private TooWideReturnTypeCheck $check,
23+
)
24+
{
25+
}
26+
2427
public function getNodeType(): string
2528
{
2629
return ClosureReturnStatementsNode::class;
@@ -62,24 +65,7 @@ public function processNode(Node $node, Scope $scope): array
6265
return [];
6366
}
6467

65-
$returnType = TypeCombinator::union(...$returnTypes);
66-
if ($returnType->isNull()->yes()) {
67-
return [];
68-
}
69-
70-
$messages = [];
71-
foreach ($closureReturnType->getTypes() as $type) {
72-
if (!$type->isSuperTypeOf($returnType)->no()) {
73-
continue;
74-
}
75-
76-
$messages[] = RuleErrorBuilder::message(sprintf(
77-
'Anonymous function never returns %s so it can be removed from the return type.',
78-
$type->describe(VerbosityLevel::getRecommendedLevelByType($type)),
79-
))->identifier('return.unusedType')->build();
80-
}
81-
82-
return $messages;
68+
return $this->check->checkAnonymousFunction(TypeCombinator::union(...$returnTypes), $closureReturnType);
8369
}
8470

8571
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\TooWideTypehints;
4+
5+
use PHPStan\DependencyInjection\AutowiredService;
6+
use PHPStan\Rules\IdentifierRuleError;
7+
use PHPStan\Rules\RuleErrorBuilder;
8+
use PHPStan\Type\Type;
9+
use PHPStan\Type\UnionType;
10+
use PHPStan\Type\VerbosityLevel;
11+
use function sprintf;
12+
13+
#[AutowiredService]
14+
final class TooWideReturnTypeCheck
15+
{
16+
17+
/**
18+
* @return list<IdentifierRuleError>
19+
*/
20+
public function checkAnonymousFunction(
21+
Type $returnType,
22+
UnionType $functionReturnType,
23+
): array
24+
{
25+
if ($returnType->isNull()->yes()) {
26+
return [];
27+
}
28+
$messages = [];
29+
foreach ($functionReturnType->getTypes() as $type) {
30+
if (!$type->isSuperTypeOf($returnType)->no()) {
31+
continue;
32+
}
33+
34+
$messages[] = RuleErrorBuilder::message(sprintf(
35+
'%s never returns %s so it can be removed from the return type.',
36+
'Anonymous function',
37+
$type->describe(VerbosityLevel::getRecommendedLevelByType($type)),
38+
))->identifier('return.unusedType')->build();
39+
}
40+
41+
return $messages;
42+
}
43+
44+
}

tests/PHPStan/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRuleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class TooWideArrowFunctionReturnTypehintRuleTest extends RuleTestCase
1313

1414
protected function getRule(): Rule
1515
{
16-
return new TooWideArrowFunctionReturnTypehintRule();
16+
return new TooWideArrowFunctionReturnTypehintRule(
17+
new TooWideReturnTypeCheck(),
18+
);
1719
}
1820

1921
public function testRule(): void

tests/PHPStan/Rules/TooWideTypehints/TooWideClosureReturnTypehintRuleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class TooWideClosureReturnTypehintRuleTest extends RuleTestCase
1313

1414
protected function getRule(): Rule
1515
{
16-
return new TooWideClosureReturnTypehintRule();
16+
return new TooWideClosureReturnTypehintRule(
17+
new TooWideReturnTypeCheck(),
18+
);
1719
}
1820

1921
public function testRule(): void

0 commit comments

Comments
 (0)