Completed
Pull Request — master (#41)
by Björn
02:45
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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A removeLine() 0 14 4
A removeLines() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\CodeSniffer\Helper;
6
7
use BestIt\CodeSniffer\File as FileDecorator;
8
use PHP_CodeSniffer\Files\File;
9
use PHP_CodeSniffer\Fixer;
10
11
/**
12
 * Helps you handling lines.
13
 *
14
 * @author blange <[email protected]>
15
 * @package BestIt\CodeSniffer\Helper
16
 */
17
class LineHelper
18
{
19
    /**
20
     * The used file.
21
     *
22
     * @var File
23
     */
24
    private $file;
25
26
    /**
27
     * Dou you want to reuse a fixer?
28
     *
29
     * @var Fixer
30
     */
31
    private $fixer;
32
33
    /**
34
     * LineHelper constructor.
35
     *
36
     * @param File $file The used file.
37
     * @param Fixer|null $fixer Dou you want to reuse a fixer?
38
     */
39
    public function __construct(File $file, ?Fixer $fixer = null)
40
    {
41
        $this->file = $file;
42
43
        if (!$fixer) {
44
            $fixer = $file instanceof FileDecorator ? $file->getFixer() : $file->fixer;
45
        }
46
47
        $this->fixer = $fixer;
48
    }
49
50
    /**
51
     * Removes the given line.
52
     *
53
     * @param int $line The line which is to be removed
54
     *
55
     * @return void
56
     */
57
    public function removeLine(int $line): void
58
    {
59
        foreach ($this->file->getTokens() as $tagPtr => $tagToken) {
60
            if ($tagToken['line'] !== $line) {
61
                continue;
62
            }
63
64
            $this->fixer->replaceToken($tagPtr, '');
65
66
            if ($tagToken['line'] > $line) {
67
                break;
68
            }
69
        }
70
    }
71
72
    /**
73
     * Removes lines by given start and end.
74
     *
75
     * @param int $startLine The first line which is to be removed
76
     * @param int $endLine The last line which is to be removed
77
     *
78
     * @return void
79
     */
80
    public function removeLines(int $startLine, int $endLine): void
81
    {
82
        for ($line = $startLine; $line <= $endLine; $line++) {
83
            $this->removeLine($line);
84
        }
85
    }
86
}
87