Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix inside Enum_ and Trait_
Browse files Browse the repository at this point in the history
samsonasik committed May 6, 2024
1 parent e1ebc99 commit e13648e
Showing 3 changed files with 16 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/Instrument/Transformer/SelfValueVisitor.php
Original file line number Diff line number Diff line change
@@ -26,9 +26,12 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\Node\UnionType;
use PhpParser\NodeVisitorAbstract;
use UnexpectedValueException;
@@ -55,6 +58,8 @@ final class SelfValueVisitor extends NodeVisitorAbstract
*/
protected ?Name $className = null;

private bool $isInsideTraitOrEnum = false;

/**
* Returns list of changed `self` nodes
*
@@ -109,6 +114,8 @@ public function enterNode(Node $node)
foreach ($node->types as &$type) {
$type = $this->resolveClassName($type);
}
} elseif ($node instanceof ClassLike) {
$this->isInsideTraitOrEnum = $node instanceof Trait_ || $node instanceof Enum_;
}

return null;
@@ -128,6 +135,10 @@ protected function resolveClassName(Name $name): Name
return $name;
}

if ($this->isInsideTraitOrEnum) {
return $name;
}

// Save the original name
$originalName = $name;
$name = clone $originalName;
Original file line number Diff line number Diff line change
@@ -55,11 +55,11 @@ public function label(): string {
return static::getLabel($this);
}

public static function getLabel(\Go\ParserReflection\Stub\ClassWithPhp81ReadOnlyProperties $value): string {
public static function getLabel(self $value): string {
return match ($value) {
\Go\ParserReflection\Stub\ClassWithPhp81ReadOnlyProperties::OK => 'OK',
\Go\ParserReflection\Stub\ClassWithPhp81ReadOnlyProperties::ACCESS_DENIED => 'Access Denied',
\Go\ParserReflection\Stub\ClassWithPhp81ReadOnlyProperties::NOT_FOUND => 'Page Not Found',
self::OK => 'OK',
self::ACCESS_DENIED => 'Access Denied',
self::NOT_FOUND => 'Page Not Found',
};
}
}
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ trait TraitWithPhp82Constant

protected function ensureVersion(): void
{
if (\Go\ParserReflection\Stub\ClassWithPhp82NullFalseTypes::CURRENT_VERSION < \Go\ParserReflection\Stub\ClassWithPhp82NullFalseTypes::MIN_VERSION) {
if (self::CURRENT_VERSION < self::MIN_VERSION) {
throw new \Exception('Current version is too old');
}
}

0 comments on commit e13648e

Please sign in to comment.