1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 23:18:40 +01:00
psalm/src/Psalm/Internal/PhpVisitor/OffsetShifterVisitor.php

73 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2020-03-15 04:54:42 +01:00
namespace Psalm\Internal\PhpVisitor;
use PhpParser;
/**
* Shifts all nodes in a given AST by a set amount
2022-01-03 07:55:32 +01:00
*
* @internal
*/
class OffsetShifterVisitor extends PhpParser\NodeVisitorAbstract
{
/** @var int */
private $file_offset;
/** @var int */
private $line_offset;
/** @var array<int, int> */
private $extra_offsets;
/**
* @param array<int, int> $extra_offsets
*/
public function __construct(int $offset, int $line_offset, array $extra_offsets)
{
$this->file_offset = $offset;
$this->line_offset = $line_offset;
$this->extra_offsets = $extra_offsets;
}
2021-10-04 00:03:06 +02:00
public function enterNode(PhpParser\Node $node): ?int
{
/** @var array{startFilePos: int, endFilePos: int, startLine: int} */
$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->getStartLine() + $this->line_offset,
$c->getStartFilePos() + $this->file_offset + ($this->extra_offsets[$c->getStartFilePos()] ?? 0)
2018-10-26 17:22:43 +02:00
);
} else {
$new_comments[] = new PhpParser\Comment(
$c->getText(),
$c->getStartLine() + $this->line_offset,
$c->getStartFilePos() + $this->file_offset + ($this->extra_offsets[$c->getStartFilePos()] ?? 0)
2018-10-26 17:22:43 +02:00
);
}
}
$node->setAttribute('comments', $new_comments);
}
$node->setAttribute(
'startFilePos',
$attrs['startFilePos'] + $this->file_offset + ($this->extra_offsets[$attrs['startFilePos']] ?? 0)
);
$node->setAttribute(
'endFilePos',
$attrs['endFilePos'] + $this->file_offset + ($this->extra_offsets[$attrs['endFilePos']] ?? 0)
);
$node->setAttribute('startLine', $attrs['startLine'] + $this->line_offset);
2021-09-25 16:49:46 +02:00
return null;
}
}