|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\CodeSniffer; |
|
6
|
|
|
|
|
7
|
|
|
use PHP_CodeSniffer_Fixer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Fixer |
|
11
|
|
|
* |
|
12
|
|
|
* @package BestIt\CodeSniffer |
|
13
|
|
|
* @author Nick Lubisch <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Fixer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The PHP_CodeSniffer_Fixer |
|
19
|
|
|
* |
|
20
|
|
|
* @var PHP_CodeSniffer_Fixer |
|
21
|
|
|
*/ |
|
22
|
|
|
private $baseFixer; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The wrapped PHP_CodeSniffer_File |
|
26
|
|
|
* |
|
27
|
|
|
* @var File |
|
28
|
|
|
*/ |
|
29
|
|
|
private $file; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Fixer constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param File $file The wrapped PHP_CodeSniffer_File |
|
35
|
|
|
* @param PHP_CodeSniffer_Fixer $baseFixer The PHP_CodeSniffer_Fixer |
|
36
|
|
|
*/ |
|
37
|
121 |
|
public function __construct(File $file, PHP_CodeSniffer_Fixer $baseFixer) |
|
38
|
|
|
{ |
|
39
|
121 |
|
$this->file = $file; |
|
40
|
121 |
|
$this->baseFixer = $baseFixer; |
|
41
|
121 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Start recording actions for a changeset. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
33 |
|
public function beginChangeset(): void |
|
49
|
|
|
{ |
|
50
|
33 |
|
$this->baseFixer->beginChangeset(); |
|
51
|
33 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Replace the entire contents of a token. |
|
55
|
|
|
* |
|
56
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
|
57
|
|
|
* @param string $content The new content of the token. |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool If the change was accepted. |
|
60
|
|
|
*/ |
|
61
|
8 |
|
public function replaceToken(int $stackPtr, string $content): bool |
|
62
|
|
|
{ |
|
63
|
8 |
|
return $this->baseFixer->replaceToken($stackPtr, $content); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Adds content to the start of a token's current content. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
|
70
|
|
|
* @param string $content The content to add. |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool If the change was accepted. |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function addContentBefore(int $stackPtr, string $content): bool |
|
75
|
|
|
{ |
|
76
|
2 |
|
return $this->baseFixer->addContentBefore($stackPtr, $content); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Adds content to the end of a token's current content. |
|
81
|
|
|
* |
|
82
|
|
|
* @param int $stackPtr The position of the token in the token stack. |
|
83
|
|
|
* @param string $content The content to add. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool If the change was accepted. |
|
86
|
|
|
*/ |
|
87
|
12 |
|
public function addContent(int $stackPtr, string $content): bool |
|
88
|
|
|
{ |
|
89
|
12 |
|
return $this->baseFixer->addContent($stackPtr, $content); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Stop recording actions for a changeset, and apply logged changes. |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool Returns false if the changeset is in conflict |
|
96
|
|
|
*/ |
|
97
|
33 |
|
public function endChangeset(): bool |
|
98
|
|
|
{ |
|
99
|
33 |
|
return $this->baseFixer->endChangeset() ?? true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Removes the given line. |
|
104
|
|
|
* |
|
105
|
|
|
* @param int $line The line which is to be removed |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
16 |
|
public function removeLine(int $line): void |
|
110
|
|
|
{ |
|
111
|
16 |
|
foreach ($this->file->getTokens() as $tagPtr => $tagToken) { |
|
112
|
16 |
|
if ($tagToken['line'] !== $line) { |
|
113
|
16 |
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
16 |
|
$this->baseFixer->replaceToken($tagPtr, ''); |
|
117
|
|
|
} |
|
118
|
16 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Removes lines by given start and end. |
|
122
|
|
|
* |
|
123
|
|
|
* @param int $startLine The first line which is to be removed |
|
124
|
|
|
* @param int $endLine The last line which is to be removed |
|
125
|
|
|
* |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
16 |
|
public function removeLines(int $startLine, int $endLine): void |
|
129
|
|
|
{ |
|
130
|
16 |
|
for ($line = $startLine; $line <= $endLine; $line++) { |
|
131
|
16 |
|
$this->removeLine($line); |
|
132
|
|
|
} |
|
133
|
16 |
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|