Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Source which encapsulates file-based reflection #37

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

thekid
Copy link
Member

@thekid thekid commented Jul 30, 2023

API

public class lang.reflection.Source implements lang.Value {
  public function __construct(ReflectionClass|ReflectionMethod $reflect)

  public function fileName(): string
  public function startLine(): int
  public function endLine(): int
  public function usedTypes(): [:string]
  public function usedConstants(): [:string]
  public function usedFunctions(): [:string]
  public function toString(): string
  public function hashCode(): string
  public function compareTo(var $value): int
}

Declaration site

 1 | <?php
 2 |
 3 | $source= Reflection::type(new class() { })->source();
 4 |
 5 | $source->fileName();  // current file
 6 | $source->startLine(); // 3
 7 | $source->endLine();   // 3, the anonymous type is declared in one line!

Imports

 1 | <?php
 2 |
 3 | use util\{Date as DateTime, Money};
 4 | use function strncmp;
 5 |
 6 | class T { }
 7 |
 8 | $source= Reflection::type(T::class)->source();
 9 | $source->usedTypes();     // ['DateTime' => 'util\\Date', 'Money' => 'util\\Money']
10 | $source->usedFunctions(); // ['strncmp' => 'strncmp']

Example

This code inside test.execution.TestClass could be rewritten:

-return $this->context->type->class()->reflect()->getFileName();
+return $this->context->type->source->fileName();

@thekid thekid changed the title Add Source which encapsulates filename, start and end lines for types and methods Add Source which encapsulates file-based reflection Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant