2018-10-26 06:59:14 +02:00
|
|
|
<?php
|
2020-03-15 04:54:42 +01:00
|
|
|
namespace Psalm\Internal\PhpVisitor;
|
2018-10-26 06:59:14 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
2018-10-27 18:27:21 +02:00
|
|
|
/**
|
|
|
|
* Shifts all nodes in a given AST by a set amount
|
|
|
|
*/
|
2020-10-15 19:23:35 +02:00
|
|
|
class OffsetShifterVisitor extends PhpParser\NodeVisitorAbstract
|
2018-10-26 06:59:14 +02:00
|
|
|
{
|
|
|
|
/** @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|int
|
|
|
|
*/
|
|
|
|
public function enterNode(PhpParser\Node $node)
|
|
|
|
{
|
2020-02-22 06:29:59 +01:00
|
|
|
/** @var array{startFilePos: int, endFilePos: int, startLine: int} */
|
2018-10-26 06:59:14 +02:00
|
|
|
$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(),
|
2020-09-20 14:56:49 +02:00
|
|
|
$c->getStartLine() + $this->line_offset,
|
|
|
|
$c->getStartFilePos() + $this->file_offset
|
2018-10-26 17:22:43 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$new_comments[] = new PhpParser\Comment(
|
|
|
|
$c->getText(),
|
2020-09-20 14:56:49 +02:00
|
|
|
$c->getStartLine() + $this->line_offset,
|
|
|
|
$c->getStartFilePos() + $this->file_offset
|
2018-10-26 17:22:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->setAttribute('comments', $new_comments);
|
2018-10-26 06:59:14 +02:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:44:43 +02:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedOperand
|
|
|
|
*/
|
2018-10-26 06:59:14 +02:00
|
|
|
$node->setAttribute('startFilePos', $attrs['startFilePos'] + $this->file_offset);
|
|
|
|
$node->setAttribute('endFilePos', $attrs['endFilePos'] + $this->file_offset);
|
|
|
|
$node->setAttribute('startLine', $attrs['startLine'] + $this->line_offset);
|
|
|
|
}
|
|
|
|
}
|