Skip to content

Commit

Permalink
Bleeding edge - check existing classes in @phpstan-self-out
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 23, 2024
1 parent 7fc5ab8 commit 6838669
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function checkClassMethod(
/** @var ParametersAcceptorWithPhpDocs $parametersAcceptor */
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

return $this->checkParametersAcceptor(
$errors = $this->checkParametersAcceptor(
$parametersAcceptor,
$methodNode,
$parameterMessage,
Expand All @@ -256,6 +256,39 @@ public function checkClassMethod(
$unresolvableParameterTypeMessage,
$unresolvableReturnTypeMessage,
);

$selfOutType = $methodReflection->getSelfOutType();
if ($selfOutType !== null && $this->absentTypeChecks) {
$selfOutTypeReferencedClasses = $selfOutType->getReferencedClasses();

foreach ($selfOutTypeReferencedClasses as $class) {
if (!$this->reflectionProvider->hasClass($class)) {
$errors[] = RuleErrorBuilder::message(sprintf($returnMessage, $class))
->line($methodNode->getStartLine())
->identifier('class.notFound')
->build();
continue;
}
if (!$this->reflectionProvider->getClass($class)->isTrait()) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf($returnMessage, $class))
->line($methodNode->getStartLine())
->identifier('selfOut.trait')
->build();
}

$errors = array_merge(
$errors,
$this->classCheck->checkClassNames(
array_map(static fn (string $class): ClassNameNodePair => new ClassNameNodePair($class, $methodNode), $selfOutTypeReferencedClasses),
$this->checkClassCaseSensitivity,
),
);
}

return $errors;
}

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

public function testSelfOut(): void
{
$this->analyse([__DIR__ . '/data/self-out.php'], [
[
'Method SelfOutClasses\Foo::doFoo() has invalid return type SelfOutClasses\Nonexistent.',
16,
],
[
'Method SelfOutClasses\Foo::doBar() has invalid return type SelfOutClasses\FooTrait.',
24,
],
[
'Class SelfOutClasses\Foo referenced with incorrect case: SelfOutClasses\fOO.',
32,
],
]);
}

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

namespace SelfOutClasses;

trait FooTrait
{

}

class Foo
{

/**
* @phpstan-self-out Nonexistent
*/
public function doFoo(): void
{

}

/**
* @phpstan-self-out FooTrait
*/
public function doBar(): void
{

}

/**
* @phpstan-self-out fOO
*/
public function doBaz(): void
{

}

}

0 comments on commit 6838669

Please sign in to comment.