1 | <?php |
||
17 | { |
||
18 | /** |
||
19 | * The used file. |
||
20 | * |
||
21 | * @var File |
||
22 | */ |
||
23 | private File $file; |
||
|
|||
24 | |||
25 | /** |
||
26 | * Dou you want to reuse a fixer? |
||
27 | * |
||
28 | * @var Fixer |
||
29 | */ |
||
30 | private Fixer $fixer; |
||
31 | |||
32 | /** |
||
33 | * LineHelper constructor. |
||
34 | * |
||
35 | * @param File $file The used file. |
||
36 | * @param Fixer|null $fixer Dou you want to reuse a fixer? |
||
37 | */ |
||
38 | public function __construct(File $file, ?Fixer $fixer = null) |
||
39 | { |
||
40 | $this->file = $file; |
||
41 | |||
42 | if (!$fixer) { |
||
43 | $fixer = $file->fixer; |
||
44 | } |
||
45 | |||
46 | $this->fixer = $fixer; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Removes the given line. |
||
51 | * |
||
52 | * @param int $line The line which is to be removed |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | public function removeLine(int $line): void |
||
57 | { |
||
58 | foreach ($this->file->getTokens() as $tagPtr => $tagToken) { |
||
59 | if ($tagToken['line'] !== $line) { |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | $this->fixer->replaceToken($tagPtr, ''); |
||
64 | |||
65 | if ($tagToken['line'] > $line) { |
||
66 | break; |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Removes lines by given start and end. |
||
73 | * |
||
74 | * @param int $startLine The first line which is to be removed |
||
75 | * @param int $endLine The last line which is to be removed |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function removeLines(int $startLine, int $endLine): void |
||
80 | { |
||
81 | for ($line = $startLine; $line <= $endLine; $line++) { |
||
82 | $this->removeLine($line); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 |