Skip to content

Commit

Permalink
[Php81] Handle crash backed Enum not implemented on FirstClassCallabl…
Browse files Browse the repository at this point in the history
…eRector (#2815)

* Compatibility with Enums

* Closes #2787 Fixes rectorphp/rector#7370

* cleanup

* Fixed 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fixed 🎉

Co-authored-by: Brad Jones <brad@kinksters.dating>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people authored Aug 21, 2022
1 parent ffa1221 commit a4799d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\Fixture;

enum SkipEnumData: int {

case FULLY_REGISTERED = 0x000001;
case MEMBER = 0x000010;

public static function getPublic(): array {
return [
self::FULLY_REGISTERED,
self::MEMBER,
];
}

}
43 changes: 29 additions & 14 deletions src/PhpParser/Node/Value/ValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ public function getValue(Expr $expr, bool $resolvedClassReference = false): mixe
}
}

try {
$constExprEvaluator = $this->getConstExprEvaluator();
$value = $constExprEvaluator->evaluateDirectly($expr);
} catch (ConstExprEvaluationException) {
$value = null;
}
$value = $this->resolveExprValueForConst($expr);

if ($value !== null) {
return $value;
Expand Down Expand Up @@ -161,6 +156,17 @@ public function areValuesEqual(array $nodes, array $expectedValues): bool
return true;
}

private function resolveExprValueForConst(Expr $expr): mixed
{
try {
$constExprEvaluator = $this->getConstExprEvaluator();
return $constExprEvaluator->evaluateDirectly($expr);
} catch (ConstExprEvaluationException) {
}

return null;
}

private function processConcat(Concat $concat, bool $resolvedClassReference): string
{
return $this->getValue($concat->left, $resolvedClassReference) . $this->getValue(
Expand Down Expand Up @@ -273,17 +279,26 @@ private function resolveClassConstFetch(ClassConstFetch $classConstFetch)
return constant($classConstantReference);
}

if ($this->reflectionProvider->hasClass($class)) {
$classReflection = $this->reflectionProvider->getClass($class);
if (! $this->reflectionProvider->hasClass($class)) {
// fallback to constant reference itself, to avoid fatal error
return $classConstantReference;
}

if ($classReflection->hasConstant($constant)) {
$constantReflection = $classReflection->getConstant($constant);
return $constantReflection->getValue();
}
$classReflection = $this->reflectionProvider->getClass($class);

if (! $classReflection->hasConstant($constant)) {
// fallback to constant reference itself, to avoid fatal error
return $classConstantReference;
}

$constantReflection = $classReflection->getConstant($constant);
$valueExpr = $constantReflection->getValueExpr();

if ($valueExpr instanceof ConstFetch) {
return $this->resolveExprValueForConst($valueExpr);
}

// fallback to constant reference itself, to avoid fatal error
return $classConstantReference;
return $this->getValue($valueExpr);
}

private function resolveClassFromSelfStaticParent(ClassConstFetch $classConstFetch, string $class): string
Expand Down

0 comments on commit a4799d9

Please sign in to comment.