1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 10:17:33 +01:00
psalm/src/Psalm/FileManipulation.php
Jeffrey Yoo ed61bcafda Issue 1926 unused variable (#1967)
* add support for Psalter to remove UnusedVariable

* fix issues found by Psalm

* fix stylistic issues found by phpcs

* added more stylistic changes and suppressed UndefinedClass error for Psalm

* suppress TypeDoesNotContainType error for CheckTrivialExprVisitor

* fix whitespace issue raised by phpcs

* fix bug where partial removal of assignment by ref does not process '&' symbol

* Remove workspace files

* remove 'Array_' and 'ArrayItem' classes from blacklist and add 'New_' class to the blacklist
2019-07-24 16:48:54 -04:00

85 lines
2.3 KiB
PHP

<?php
namespace Psalm;
use function sha1;
use function strlen;
use function strrpos;
use function substr;
use function trim;
class FileManipulation
{
/** @var int */
public $start;
/** @var int */
public $end;
/** @var string */
public $insertion_text;
/** @var bool */
public $preserve_indentation;
/** @var bool */
public $remove_trailing_newline;
/**
* @param int $start
* @param int $end
* @param string $insertion_text
*/
public function __construct(
int $start,
int $end,
string $insertion_text,
bool $preserve_indentation = false,
bool $remove_trailing_newline = false
) {
$this->start = $start;
$this->end = $end;
$this->insertion_text = $insertion_text;
$this->preserve_indentation = $preserve_indentation;
$this->remove_trailing_newline = $remove_trailing_newline;
}
public function getKey() : string
{
return $this->start === $this->end
? ($this->start . ':' . sha1($this->insertion_text))
: ($this->start . ':' . $this->end);
}
public function transform(string $existing_contents) : string
{
if ($this->preserve_indentation) {
$newline_pos = strrpos($existing_contents, "\n", $this->start - strlen($existing_contents));
$newline_pos = $newline_pos !== false ? $newline_pos + 1 : 0;
$indentation = substr($existing_contents, $newline_pos, $this->start - $newline_pos);
if (trim($indentation) === '') {
$this->insertion_text = $this->insertion_text . $indentation;
}
}
if ($this->remove_trailing_newline && $existing_contents[$this->end] === "\n") {
$newline_pos = strrpos($existing_contents, "\n", $this->start - strlen($existing_contents));
$newline_pos = $newline_pos !== false ? $newline_pos + 1 : 0;
$indentation = substr($existing_contents, $newline_pos, $this->start - $newline_pos);
if (trim($indentation) === '') {
$this->start -= strlen($indentation);
$this->end++;
}
}
return substr($existing_contents, 0, $this->start)
. $this->insertion_text
. substr($existing_contents, $this->end);
}
}