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

foreach and locals->offset #67

Open
drzraf opened this issue Dec 17, 2019 · 1 comment
Open

foreach and locals->offset #67

drzraf opened this issue Dec 17, 2019 · 1 comment

Comments

@drzraf
Copy link
Contributor

drzraf commented Dec 17, 2019

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.

@drzraf drzraf mentioned this issue Dec 17, 2019
@dantleech
Copy link
Collaborator

btw, it might be worth investigating Rector if you're not aware of it, which is all about scripted refactoring.

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

No branches or pull requests

2 participants