Skip to content

Commit

Permalink
Bleeding edge - check existing classes in @param-closure-this
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 23, 2024
1 parent b04b3bf commit 2fa539a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,20 @@ private function getParameterReferencedClasses(ParameterReflection $parameter):
return $parameter->getNativeType()->getReferencedClasses();
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,22 @@ public function testParamOutClasses(): void
]);
}

public function testParamClosureThisClasses(): void
{
$this->analyse([__DIR__ . '/data/param-closure-this-classes.php'], [
[
'Parameter $a of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\Nonexistent.',
24,
],
[
'Parameter $b of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\FooTrait.',
25,
],
[
'Class ParamClosureThisClasses\Foo referenced with incorrect case: ParamClosureThisClasses\fOO.',
26,
],
]);
}

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

namespace ParamClosureThisClassesFunctions;

trait FooTrait
{

}

class Foo
{

}

/**
* @param-closure-this Nonexistent $a
* @param-closure-this FooTrait $b
* @param-closure-this fOO $c
*/
function doFoo(
callable $a,
callable $b,
callable $c
): void
{

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Methods/ExistingClassesInTypehintsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,22 @@ public function testParamOutClasses(): void
]);
}

public function testParamClosureThisClasses(): void
{
$this->analyse([__DIR__ . '/data/param-closure-this-classes.php'], [
[
'Parameter $a of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\Nonexistent.',
24,
],
[
'Parameter $b of method ParamClosureThisClasses\Bar::doFoo() has invalid type ParamClosureThisClasses\FooTrait.',
25,
],
[
'Class ParamClosureThisClasses\Foo referenced with incorrect case: ParamClosureThisClasses\fOO.',
26,
],
]);
}

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

namespace ParamClosureThisClasses;

trait FooTrait
{

}

class Foo
{

}

class Bar
{

/**
* @param-closure-this Nonexistent $a
* @param-closure-this FooTrait $b
* @param-closure-this fOO $c
*/
public function doFoo(
callable $a,
callable $b,
callable $c
): void
{

}

}

0 comments on commit 2fa539a

Please sign in to comment.