Skip to content

Commit 03b3159

Browse files
committed
add bleeding-edge feature toggle
1 parent c48c398 commit 03b3159

10 files changed

+42
-6
lines changed

conf/bleedingEdge.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ parameters:
88
reportPreciseLineForUnusedFunctionParameter: true
99
internalTag: true
1010
newStaticInAbstractClassStaticMethod: true
11+
reportTooWideBool: true

conf/config.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ parameters:
3131
reportPreciseLineForUnusedFunctionParameter: false
3232
internalTag: false
3333
newStaticInAbstractClassStaticMethod: false
34+
reportTooWideBool: false
3435
fileExtensions:
3536
- php
3637
checkAdvancedIsset: false

conf/parametersSchema.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ parametersSchema:
3535
reportPreciseLineForUnusedFunctionParameter: bool()
3636
internalTag: bool()
3737
newStaticInAbstractClassStaticMethod: bool()
38+
reportTooWideBool: bool()
3839
])
3940
fileExtensions: listOf(string())
4041
checkAdvancedIsset: bool()

src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php

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

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\AutowiredParameter;
78
use PHPStan\DependencyInjection\RegisteredRule;
89
use PHPStan\Node\FunctionReturnStatementsNode;
910
use PHPStan\Rules\Rule;
@@ -18,6 +19,8 @@ final class TooWideFunctionReturnTypehintRule implements Rule
1819

1920
public function __construct(
2021
private TooWideTypeCheck $check,
22+
#[AutowiredParameter(ref: '%featureToggles.reportTooWideBool%')]
23+
private bool $reportTooWideBool,
2124
)
2225
{
2326
}
@@ -40,6 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4043
$function->getName(),
4144
),
4245
false,
46+
$this->reportTooWideBool,
4347
);
4448
}
4549

src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public function __construct(
2121
#[AutowiredParameter(ref: '%checkTooWideReturnTypesInProtectedAndPublicMethods%')]
2222
private bool $checkProtectedAndPublicMethods,
2323
private TooWideTypeCheck $check,
24+
#[AutowiredParameter(ref: '%featureToggles.reportTooWideBool%')]
25+
private bool $reportTooWideBool,
2426
)
2527
{
2628
}
@@ -59,6 +61,7 @@ public function processNode(Node $node, Scope $scope): array
5961
$method->getName(),
6062
),
6163
!$isFirstDeclaration && !$method->isPrivate(),
64+
$this->reportTooWideBool,
6265
);
6366
}
6467

src/Rules/TooWideTypehints/TooWidePropertyTypeRule.php

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

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\AutowiredParameter;
78
use PHPStan\DependencyInjection\RegisteredRule;
89
use PHPStan\Node\ClassPropertiesNode;
910
use PHPStan\Reflection\PropertyReflection;
@@ -26,6 +27,8 @@ public function __construct(
2627
private ReadWritePropertiesExtensionProvider $extensionProvider,
2728
private PropertyReflectionFinder $propertyReflectionFinder,
2829
private TooWideTypeCheck $check,
30+
#[AutowiredParameter(ref: '%featureToggles.reportTooWideBool%')]
31+
private bool $reportTooWideBool,
2932
)
3033
{
3134
}
@@ -57,8 +60,10 @@ public function processNode(Node $node, Scope $scope): array
5760

5861
$propertyReflection = $classReflection->getNativeProperty($propertyName);
5962
$propertyType = $propertyReflection->getWritableType();
60-
if (!$propertyType instanceof UnionType && !$propertyType->isBoolean()->yes()) {
61-
continue;
63+
if (!$propertyType instanceof UnionType) {
64+
if (!$propertyType->isBoolean()->yes() || !$this->reportTooWideBool) {
65+
continue;
66+
}
6267
}
6368
foreach ($this->extensionProvider->getExtensions() as $extension) {
6469
if ($extension->isAlwaysRead($propertyReflection, $propertyName)) {

src/Rules/TooWideTypehints/TooWideTypeCheck.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ public function checkFunction(
6666
Type $functionReturnType,
6767
string $functionDescription,
6868
bool $checkDescendantClass,
69+
bool $reportTooWideBool,
6970
): array
7071
{
7172
$functionReturnType = TypeUtils::resolveLateResolvableTypes($functionReturnType);
72-
if (!$functionReturnType instanceof UnionType && !$functionReturnType->isBoolean()->yes()) {
73-
return [];
73+
74+
if (!$functionReturnType instanceof UnionType) {
75+
if (!$functionReturnType->isBoolean()->yes() || !$reportTooWideBool) {
76+
return [];
77+
}
7478
}
7579
$statementResult = $node->getStatementResult();
7680
if ($statementResult->hasYield()) {

tests/PHPStan/Analyser/nsrt/assert-docblock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function validateStringArray(array $arr) : void {}
1313
/**
1414
* @param mixed[] $arr
1515
* @phpstan-assert-if-true string[] $arr
16+
* @return true
1617
*/
1718
function validateStringArrayIfTrue(array $arr) : bool {
1819
return true;

tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
class TooWideFunctionReturnTypehintRuleTest extends RuleTestCase
1212
{
1313

14+
private bool $reportTooWideBool = false;
15+
1416
protected function getRule(): Rule
1517
{
16-
return new TooWideFunctionReturnTypehintRule(new TooWideTypeCheck());
18+
return new TooWideFunctionReturnTypehintRule(new TooWideTypeCheck(), $this->reportTooWideBool);
1719
}
1820

1921
public function testRule(): void
@@ -63,6 +65,7 @@ public function testBug11980(): void
6365

6466
public function testBug13384c(): void
6567
{
68+
$this->reportTooWideBool = true;
6669
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
6770
[
6871
'Function Bug13384c\doFoo() never returns true so it can be removed from the return type.',
@@ -75,4 +78,9 @@ public function testBug13384c(): void
7578
]);
7679
}
7780

81+
public function testBug13384cOff(): void
82+
{
83+
$this->analyse([__DIR__ . '/data/bug-13384c.php'], []);
84+
}
85+
7886
}

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ class TooWideMethodReturnTypehintRuleTest extends RuleTestCase
1515

1616
private bool $checkProtectedAndPublicMethods = true;
1717

18+
private bool $reportTooWideBool = false;
19+
1820
protected function getRule(): Rule
1921
{
20-
return new TooWideMethodReturnTypehintRule($this->checkProtectedAndPublicMethods, new TooWideTypeCheck());
22+
return new TooWideMethodReturnTypehintRule($this->checkProtectedAndPublicMethods, new TooWideTypeCheck(), $this->reportTooWideBool);
2123
}
2224

2325
public function testPrivate(): void
@@ -201,6 +203,7 @@ public function testBug11980(): void
201203

202204
public function testBug13384c(): void
203205
{
206+
$this->reportTooWideBool = true;
204207
$this->analyse([__DIR__ . '/data/bug-13384c.php'], [
205208
[
206209
'Method Bug13384c\Bug13384c::doBar() never returns true so it can be removed from the return type.',
@@ -221,4 +224,9 @@ public function testBug13384c(): void
221224
]);
222225
}
223226

227+
public function testBug13384cOff(): void
228+
{
229+
$this->analyse([__DIR__ . '/data/bug-13384c.php'], []);
230+
}
231+
224232
}

0 commit comments

Comments
 (0)