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) { |
|
|
|
|
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
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: