Skip to content

Commit

Permalink
[TypeDeclaration] Handle crash on interface Mixin on ReturnTypeFromSt…
Browse files Browse the repository at this point in the history
…rictConstantReturnRector (#5884)

* [TypeDeclaration] Handle crash on interface Mixin on ReturnTypeFromStrictConstantReturnRector

* run rector

* move repetivive string message to constant
  • Loading branch information
samsonasik committed May 16, 2024
1 parent 6bd2b87 commit 6a5f6c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// the namespace Webmozart\Assert on purpose to reproduce crash
// @see https://github.com/rectorphp/rector/issues/8624
// @see https://getrector.com/demo/e6b2e871-1b41-4897-8864-2f9aaad48de1
namespace Webmozart\Assert;

interface Mixin
{
public static function nullOrString($value, $message = '');
}
17 changes: 15 additions & 2 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ final class PHPStanNodeScopeResolver

private bool $hasUnreachableStatementNode = false;

/**
* @var string
*/
private const PHPSTAN_INTERNAL_ERROR_MESSAGE = 'Internal error.';

/**
* @param ScopeResolverNodeVisitorInterface[] $nodeVisitors
*/
Expand Down Expand Up @@ -230,7 +235,7 @@ private function nodeScopeResolverProcessNodes(
try {
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);
} catch (Throwable $throwable) {
if ($throwable->getMessage() !== 'Internal error.') {
if ($throwable->getMessage() !== self::PHPSTAN_INTERNAL_ERROR_MESSAGE) {
throw $throwable;
}
}
Expand Down Expand Up @@ -385,7 +390,15 @@ private function resolveClassOrInterfaceScope(
$context = $this->privatesAccessor->getPrivateProperty($mutatingScope, 'context');
$this->privatesAccessor->setPrivateProperty($context, 'classReflection', null);

return $mutatingScope->enterClass($classReflection);
try {
return $mutatingScope->enterClass($classReflection);
} catch (Throwable $throwable) {
if ($throwable->getMessage() !== self::PHPSTAN_INTERNAL_ERROR_MESSAGE) {
throw $throwable;
}

return $mutatingScope;
}
}

private function resolveClassName(Class_ | Interface_ | Trait_| Enum_ $classLike): string
Expand Down

0 comments on commit 6a5f6c5

Please sign in to comment.