Skip to content

Commit

Permalink
Bleeding edge - check existing classes in @param-out
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 22, 2024
1 parent 30d3c0a commit 30c4b9e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ parameters:
noImplicitWildcard: true
tooWidePropertyType: true
explicitThrow: true
absentTypeChecks: true
stubFiles:
- ../stubs/bleedingEdge/Rule.stub
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ parameters:
narrowPregMatches: true
tooWidePropertyType: false
explicitThrow: false
absentTypeChecks: false
fileExtensions:
- php
checkAdvancedIsset: false
Expand Down Expand Up @@ -970,6 +971,7 @@ services:
arguments:
checkClassCaseSensitivity: %checkClassCaseSensitivity%
checkThisOnly: %checkThisOnly%
absentTypeChecks: %featureToggles.absentTypeChecks%

-
class: PHPStan\Rules\FunctionReturnTypeCheck
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ parametersSchema:
narrowPregMatches: bool()
tooWidePropertyType: bool()
explicitThrow: bool()
absentTypeChecks: bool()
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
Expand Down
7 changes: 7 additions & 0 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct(
private PhpVersion $phpVersion,
private bool $checkClassCaseSensitivity,
private bool $checkThisOnly,
private bool $absentTypeChecks,
)
{
}
Expand Down Expand Up @@ -583,9 +584,15 @@ private function getParameterReferencedClasses(ParameterReflection $parameter):
return $parameter->getNativeType()->getReferencedClasses();
}

$outTypeClasses = [];
if ($parameter->getOutType() !== null && $this->absentTypeChecks) {
$outTypeClasses = $parameter->getOutType()->getReferencedClasses();
}

return array_merge(
$parameter->getNativeType()->getReferencedClasses(),
$parameter->getPhpDocType()->getReferencedClasses(),
$outTypeClasses,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function getRule(): Rule
new PhpVersion($this->phpVersionId),
true,
false,
true,
),
new PhpVersion(PHP_VERSION_ID),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function getRule(): Rule
new PhpVersion($this->phpVersionId),
true,
false,
true,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function getRule(): Rule
new PhpVersion($this->phpVersionId),
true,
false,
true,
),
);
}
Expand Down Expand Up @@ -451,4 +452,22 @@ public function testTemplateInParamOut(): void
]);
}

public function testParamOutClasses(): void
{
$this->analyse([__DIR__ . '/data/param-out-classes.php'], [
[
'Parameter $p of function ParamOutClasses\doFoo() has invalid type ParamOutClasses\Nonexistent.',
20,
],
[
'Parameter $q of function ParamOutClasses\doFoo() has invalid type ParamOutClasses\FooTrait.',
20,
],
[
'Class ParamOutClasses\Foo referenced with incorrect case: ParamOutClasses\fOO.',
20,
],
]);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Functions/data/param-out-classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ParamOutClasses;

trait FooTrait
{

}

class Foo
{

}

/**
* @param-out Nonexistent $p
* @param-out FooTrait $q
* @param-out fOO $r
*/
function doFoo(&$p, &$q, $r): void
{

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Methods/ExistingClassesInTypehintsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function getRule(): Rule
new PhpVersion($this->phpVersionId),
true,
false,
true,
),
);
}
Expand Down Expand Up @@ -471,4 +472,22 @@ public function testTemplateInParamOut(): void
]);
}

public function testParamOutClasses(): void
{
$this->analyse([__DIR__ . '/data/param-out-classes.php'], [
[
'Parameter $p of method ParamOutClassesMethods\Bar::doFoo() has invalid type ParamOutClassesMethods\Nonexistent.',
23,
],
[
'Parameter $q of method ParamOutClassesMethods\Bar::doFoo() has invalid type ParamOutClassesMethods\FooTrait.',
23,
],
[
'Class ParamOutClassesMethods\Foo referenced with incorrect case: ParamOutClassesMethods\fOO.',
23,
],
]);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/data/param-out-classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ParamOutClassesMethods;

trait FooTrait
{

}

class Foo
{

}

class Bar
{

/**
* @param-out Nonexistent $p
* @param-out FooTrait $q
* @param-out fOO $r
*/
public function doFoo(&$p, &$q, $r): void
{

}


}

0 comments on commit 30c4b9e

Please sign in to comment.