Skip to content

foreach and locals->offset #67

@drzraf

Description

@drzraf

Previously on #66

Reproducer:

<?php

use Phpactor\WorseReflection\ReflectorBuilder;
use Phpactor\WorseReflection\Core\SourceCode as WorseSourceCode;

require_once('vendor/autoload.php');

function search(string $source) {
    $sourceCode = WorseSourceCode::fromString($source);
    $build = ReflectorBuilder::create()->addSource($sourceCode)->build();
    $var = $build->reflectClass('A')
                 ->methods()->get('a')
                 ->frame()->locals()->byName('class')->first();
    $name = $var->name();
    $offset = $var->offset()->toInt();
    printf('local variable "%s" identified at offset %d' . PHP_EOL, $name, $offset);
    return $offset;
}

$str = <<<'EOF'
<?php
class A
{
  public function a($classes)
  {
    foreach ($classes as $class) {}
  }
}
EOF;

$offset = search($str);
printf('but offset %d actually contains "%s"' . PHP_EOL, $offset, substr($str, $offset, 10));
local variable "class" identified at offset 54
but offset 54 actually contains "foreach ($"

My problem is that according to discussion on phpactor/code-transform#31 (comment), Worse Reflection seems the privileged (if not the only) way to fetch variable offsets.
My use-case involve calling the TolerantRenameVariable() which would throw on non-exact offset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions