2018-10-26 06:59:14 +02:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Visitor;
|
2018-10-26 06:59:14 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
2018-10-26 06:59:14 +02:00
|
|
|
|
2018-10-27 18:27:21 +02:00
|
|
|
/**
|
|
|
|
* Shifts all nodes in a given AST by a set amount
|
|
|
|
*/
|
2018-10-26 06:59:14 +02:00
|
|
|
class OffsetShifterVisitor extends PhpParser\NodeVisitorAbstract implements PhpParser\NodeVisitor
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
private $file_offset;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $line_offset;
|
|
|
|
|
|
|
|
public function __construct(int $offset, int $line_offset)
|
|
|
|
{
|
|
|
|
$this->file_offset = $offset;
|
|
|
|
$this->line_offset = $line_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PhpParser\Node $node
|
|
|
|
*
|
|
|
|
* @return null|int
|
|
|
|
*/
|
|
|
|
public function enterNode(PhpParser\Node $node)
|
|
|
|
{
|
|
|
|
$attrs = $node->getAttributes();
|
|
|
|
|
2018-10-26 17:22:43 +02:00
|
|
|
if ($cs = $node->getComments()) {
|
|
|
|
$new_comments = [];
|
|
|
|
|
|
|
|
foreach ($cs as $c) {
|
|
|
|
if ($c instanceof PhpParser\Comment\Doc) {
|
|
|
|
$new_comments[] = new PhpParser\Comment\Doc(
|
|
|
|
$c->getText(),
|
|
|
|
$c->getLine() + $this->line_offset,
|
|
|
|
$c->getFilePos() + $this->file_offset
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$new_comments[] = new PhpParser\Comment(
|
|
|
|
$c->getText(),
|
|
|
|
$c->getLine() + $this->line_offset,
|
|
|
|
$c->getFilePos() + $this->file_offset
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->setAttribute('comments', $new_comments);
|
2018-10-26 06:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress MixedOperand */
|
|
|
|
$node->setAttribute('startFilePos', $attrs['startFilePos'] + $this->file_offset);
|
|
|
|
/** @psalm-suppress MixedOperand */
|
|
|
|
$node->setAttribute('endFilePos', $attrs['endFilePos'] + $this->file_offset);
|
|
|
|
/** @psalm-suppress MixedOperand */
|
|
|
|
$node->setAttribute('startLine', $attrs['startLine'] + $this->line_offset);
|
|
|
|
}
|
|
|
|
}
|