Completed
Push — master ( eb1b80...23bc19 )
by Tomáš
06:58
created

Fixer::addNewline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\PHP7_CodeSniffer;
9
10
use Symplify\PHP7_CodeSniffer\File\File;
11
12
final class Fixer
13
{
14
    /**
15
     * @var File
16
     */
17
    private $currentFile;
18
19
    /**
20
     * @var string[]|array<int, string>
21
     */
22
    private $tokens = [];
23
24 6
    public function startFile(File $file)
25
    {
26 6
        $this->currentFile = $file;
27
28 6
        $tokens = $file->getTokens();
29
30 6
        $this->tokens = [];
31 6
        foreach ($tokens as $index => $token) {
32 6
            if (isset($token['orig_content']) === true) {
33
                $this->tokens[$index] = $token['orig_content'];
34
            } else {
35 6
                $this->tokens[$index] = $token['content'];
36
            }
37
        }
38 6
    }
39
40 2
    public function getContents() : string
41
    {
42 2
        return implode($this->tokens);
43
    }
44
45 4
    public function getTokenContent(int $stackPtr) : string
46
    {
47 4
        return $this->tokens[$stackPtr];
48
    }
49
50 4
    public function replaceToken(int $stackPtr, string $content) : bool
51
    {
52 4
        $this->tokens[$stackPtr] = $content;
53 4
        return true;
54
    }
55
56 1
    public function substrToken(int $stackPtr, int $start, int $length=null) : bool
57
    {
58 1
        $current = $this->getTokenContent($stackPtr);
59
60 1
        if ($length) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $length of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
61 1
            $newContent = substr($current, $start, $length);
62
        } else {
63 1
            $newContent = substr($current, $start);
64
        }
65
66 1
        return $this->replaceToken($stackPtr, $newContent);
67
    }
68
69 2
    public function addContent(int $stackPtr, string $content) : bool
70
    {
71 2
        $current = $this->getTokenContent($stackPtr);
72 2
        return $this->replaceToken($stackPtr, $current.$content);
73
    }
74
75 2
    public function addContentBefore(int $stackPtr, string $content) : bool
76
    {
77 2
        $current = $this->getTokenContent($stackPtr);
78 2
        return $this->replaceToken($stackPtr, $content.$current);
79
    }
80
81 1
    public function addNewline(int $stackPtr) : bool
82
    {
83 1
        return $this->addContent($stackPtr, $this->currentFile->eolChar);
84
    }
85
86 1
    public function addNewlineBefore(int $stackPtr) : bool
87
    {
88 1
        return $this->addContentBefore($stackPtr, $this->currentFile->eolChar);
89
    }
90
91
    /**
92
     * Needed for legacy compatibility.
93
     */
94 1
    public function beginChangeset()
95
    {
96 1
    }
97
98
    /**
99
     * Needed for legacy compatibility.
100
     */
101 1
    public function endChangeset()
102
    {
103 1
    }
104
}
105