Completed
Push — master ( 14c836...906b18 )
by Björn
06:09 queued 03:52
created

LineHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer\Helper;
6
7
use PHP_CodeSniffer\Files\File;
8
use PHP_CodeSniffer\Fixer;
9
10
/**
11
 * Helps you handling lines.
12
 *
13
 * @author blange <[email protected]>
14
 * @package BestIt\CodeSniffer\Helper
15
 */
16
class LineHelper
17
{
18
    /**
19
     * The used file.
20
     *
21
     * @var File
22
     */
23
    private File $file;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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