Skip to content

Commit

Permalink
Fix ObjectType resolving inside bound Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Jul 24, 2022
1 parent fbdec4a commit a6950a1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,7 @@ public function resolveTypeByName(Name $name): TypeWithClassName

$originalClass = $this->resolveName($name);
if ($this->isInClass()) {
if ($this->inClosureBindScopeClass !== null && $this->inClosureBindScopeClass !== 'static' && $originalClass === $this->getClassReflection()->getName()) {
if ($this->inClosureBindScopeClass === $originalClass) {
if ($this->reflectionProvider->hasClass($this->inClosureBindScopeClass)) {
return new ThisType($this->reflectionProvider->getClass($this->inClosureBindScopeClass));
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,10 @@ public function testRuleWithNullsafeVariant(): void
$this->analyse([__DIR__ . '/data/class-constant-nullsafe.php'], []);
}

public function testBug7675(): void
{
$this->phpVersion = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/bug-7675.php'], []);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-7675.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace Bug7675;

use Closure;
use Throwable;

class Handler
{
}

class SpladeCore
{
const HEADER_SPLADE = 'x-splade';
public static function exceptionHandler(Handler $exceptionHandler): Closure
{
return Closure::bind(function (Throwable $e, $request) {
if (!$request->header(SpladeCore::HEADER_SPLADE)) {
return null;
}

return true;
}, $exceptionHandler, get_class($exceptionHandler));
}
}

0 comments on commit a6950a1

Please sign in to comment.