Skip to content

Commit 30c4b9e

Browse files
committed
Bleeding edge - check existing classes in @param-out
1 parent 30d3c0a commit 30c4b9e

10 files changed

+103
-0
lines changed

conf/bleedingEdge.neon

+1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ parameters:
6161
noImplicitWildcard: true
6262
tooWidePropertyType: true
6363
explicitThrow: true
64+
absentTypeChecks: true
6465
stubFiles:
6566
- ../stubs/bleedingEdge/Rule.stub

conf/config.neon

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ parameters:
9797
narrowPregMatches: true
9898
tooWidePropertyType: false
9999
explicitThrow: false
100+
absentTypeChecks: false
100101
fileExtensions:
101102
- php
102103
checkAdvancedIsset: false
@@ -970,6 +971,7 @@ services:
970971
arguments:
971972
checkClassCaseSensitivity: %checkClassCaseSensitivity%
972973
checkThisOnly: %checkThisOnly%
974+
absentTypeChecks: %featureToggles.absentTypeChecks%
973975

974976
-
975977
class: PHPStan\Rules\FunctionReturnTypeCheck

conf/parametersSchema.neon

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ parametersSchema:
9292
narrowPregMatches: bool()
9393
tooWidePropertyType: bool()
9494
explicitThrow: bool()
95+
absentTypeChecks: bool()
9596
])
9697
fileExtensions: listOf(string())
9798
checkAdvancedIsset: bool()

src/Rules/FunctionDefinitionCheck.php

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct(
5252
private PhpVersion $phpVersion,
5353
private bool $checkClassCaseSensitivity,
5454
private bool $checkThisOnly,
55+
private bool $absentTypeChecks,
5556
)
5657
{
5758
}
@@ -583,9 +584,15 @@ private function getParameterReferencedClasses(ParameterReflection $parameter):
583584
return $parameter->getNativeType()->getReferencedClasses();
584585
}
585586

587+
$outTypeClasses = [];
588+
if ($parameter->getOutType() !== null && $this->absentTypeChecks) {
589+
$outTypeClasses = $parameter->getOutType()->getReferencedClasses();
590+
}
591+
586592
return array_merge(
587593
$parameter->getNativeType()->getReferencedClasses(),
588594
$parameter->getPhpDocType()->getReferencedClasses(),
595+
$outTypeClasses,
589596
);
590597
}
591598

tests/PHPStan/Rules/Functions/ExistingClassesInArrowFunctionTypehintsRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function getRule(): Rule
3434
new PhpVersion($this->phpVersionId),
3535
true,
3636
false,
37+
true,
3738
),
3839
new PhpVersion(PHP_VERSION_ID),
3940
);

tests/PHPStan/Rules/Functions/ExistingClassesInClosureTypehintsRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function getRule(): Rule
3434
new PhpVersion($this->phpVersionId),
3535
true,
3636
false,
37+
true,
3738
),
3839
);
3940
}

tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function getRule(): Rule
3434
new PhpVersion($this->phpVersionId),
3535
true,
3636
false,
37+
true,
3738
),
3839
);
3940
}
@@ -451,4 +452,22 @@ public function testTemplateInParamOut(): void
451452
]);
452453
}
453454

455+
public function testParamOutClasses(): void
456+
{
457+
$this->analyse([__DIR__ . '/data/param-out-classes.php'], [
458+
[
459+
'Parameter $p of function ParamOutClasses\doFoo() has invalid type ParamOutClasses\Nonexistent.',
460+
20,
461+
],
462+
[
463+
'Parameter $q of function ParamOutClasses\doFoo() has invalid type ParamOutClasses\FooTrait.',
464+
20,
465+
],
466+
[
467+
'Class ParamOutClasses\Foo referenced with incorrect case: ParamOutClasses\fOO.',
468+
20,
469+
],
470+
]);
471+
}
472+
454473
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace ParamOutClasses;
4+
5+
trait FooTrait
6+
{
7+
8+
}
9+
10+
class Foo
11+
{
12+
13+
}
14+
15+
/**
16+
* @param-out Nonexistent $p
17+
* @param-out FooTrait $q
18+
* @param-out fOO $r
19+
*/
20+
function doFoo(&$p, &$q, $r): void
21+
{
22+
23+
}

tests/PHPStan/Rules/Methods/ExistingClassesInTypehintsRuleTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function getRule(): Rule
3434
new PhpVersion($this->phpVersionId),
3535
true,
3636
false,
37+
true,
3738
),
3839
);
3940
}
@@ -471,4 +472,22 @@ public function testTemplateInParamOut(): void
471472
]);
472473
}
473474

475+
public function testParamOutClasses(): void
476+
{
477+
$this->analyse([__DIR__ . '/data/param-out-classes.php'], [
478+
[
479+
'Parameter $p of method ParamOutClassesMethods\Bar::doFoo() has invalid type ParamOutClassesMethods\Nonexistent.',
480+
23,
481+
],
482+
[
483+
'Parameter $q of method ParamOutClassesMethods\Bar::doFoo() has invalid type ParamOutClassesMethods\FooTrait.',
484+
23,
485+
],
486+
[
487+
'Class ParamOutClassesMethods\Foo referenced with incorrect case: ParamOutClassesMethods\fOO.',
488+
23,
489+
],
490+
]);
491+
}
492+
474493
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ParamOutClassesMethods;
4+
5+
trait FooTrait
6+
{
7+
8+
}
9+
10+
class Foo
11+
{
12+
13+
}
14+
15+
class Bar
16+
{
17+
18+
/**
19+
* @param-out Nonexistent $p
20+
* @param-out FooTrait $q
21+
* @param-out fOO $r
22+
*/
23+
public function doFoo(&$p, &$q, $r): void
24+
{
25+
26+
}
27+
28+
29+
}

0 commit comments

Comments
 (0)