Repository navigation
Expand file tree
/
Copy pathPhpPrinter.php
More file actions
68 lines (57 loc) · 1.63 KB
/
Copy pathPhpPrinter.php
File metadata and controls
68 lines (57 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php declare(strict_types = 1);
namespace PHPStan\Fixable;
use Override;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use function array_last;
use function count;
use function rtrim;
final class PhpPrinter extends Standard
{
public const TAB_WIDTH = 4;
public const FUNC_ARGS_TRAILING_COMMA_ATTRIBUTE = 'trailing_comma';
/**
* @param Node[] $nodes
*/
#[Override]
protected function pCommaSeparated(array $nodes): string
{
$result = parent::pCommaSeparated($nodes);
if (count($nodes) === 0) {
return $result;
}
$last = array_last($nodes);
$trailingComma = $last->getAttribute(self::FUNC_ARGS_TRAILING_COMMA_ATTRIBUTE);
if ($trailingComma === false) {
$result = rtrim($result, ',');
}
return $result;
}
/**
* A node that is its own origNode was not cloned, so a fix did not change it
* (see PathCloningVisitor). Its original code is copied as it is, without
* printing its children one by one.
*/
#[Override]
protected function p(
Node $node,
int $precedence = self::MAX_PRECEDENCE,
int $lhsPrecedence = self::MAX_PRECEDENCE,
bool $parentFormatPreserved = false,
): string
{
if (
$parentFormatPreserved
&& $this->origTokens !== null
&& $node->getAttribute('origNode') === $node
) {
$startPos = $node->getStartTokenPos();
$endPos = $node->getEndTokenPos();
if ($startPos >= 0 && $endPos >= 0) {
$indentAdjustment = $this->indentLevel - $this->origTokens->getIndentationBefore($startPos);
return $this->origTokens->getTokenCode($startPos, $endPos + 1, $indentAdjustment);
}
}
return parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);
}
}