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\File; |
9
|
|
|
|
10
|
|
|
use PHP_CodeSniffer\Files\File as BaseFile; |
11
|
|
|
use Symplify\PHP7_CodeSniffer\Contract\File\FileInterface; |
12
|
|
|
use Symplify\PHP7_CodeSniffer\Exception\File\NotImplementedException; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Fixer; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\ErrorDataCollector; |
15
|
|
|
|
16
|
|
|
final class File extends BaseFile implements FileInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $tokenizerType = 'PHP'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Fixer |
25
|
|
|
*/ |
26
|
|
|
public $fixer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ErrorDataCollector |
30
|
|
|
*/ |
31
|
|
|
private $reportCollector; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $isFixer; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
string $path, |
40
|
|
|
array $tokens, |
41
|
|
|
Fixer $fixer, |
42
|
|
|
ErrorDataCollector $reportCollector, |
43
|
|
|
bool $isFixer, |
44
|
|
|
string $eolChar |
45
|
|
|
) { |
46
|
|
|
$this->path = $path; |
47
|
|
|
$this->tokens = $tokens; |
48
|
|
|
$this->fixer = $fixer; |
49
|
|
|
$this->reportCollector = $reportCollector; |
50
|
|
|
$this->eolChar = $eolChar; |
51
|
|
|
|
52
|
|
|
$this->numTokens = count($this->tokens); |
53
|
|
|
$this->content = file_get_contents($path); |
54
|
|
|
$this->isFixer = $isFixer; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function parse() |
61
|
|
|
{ |
62
|
|
|
throw new NotImplementedException( |
63
|
|
|
'This is not needed to be public. File is already parsed on __construct.' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function process() |
71
|
|
|
{ |
72
|
|
|
throw new NotImplementedException( |
73
|
|
|
'This is not needed to be public. Use external processing.' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function addFixableError($error, $stackPtr, $code, $data=[], $severity=0) |
81
|
|
|
{ |
82
|
|
|
$this->addError($error, $stackPtr, $code, $data, $severity, true); |
83
|
|
|
return $this->isFixer; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
protected function addMessage( |
90
|
|
|
$error, |
91
|
|
|
$message, |
92
|
|
|
$line, |
93
|
|
|
$column, |
94
|
|
|
$code, |
95
|
|
|
$data, |
96
|
|
|
$severity, |
97
|
|
|
$isFixable=false |
98
|
|
|
) : bool { |
99
|
|
|
if (!$error) { // skip warnings |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->reportCollector->addErrorMessage( |
104
|
|
|
$this->path, |
105
|
|
|
$message, |
106
|
|
|
$line, |
107
|
|
|
$code, |
108
|
|
|
$data, |
109
|
|
|
$isFixable |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getContent() : string |
119
|
|
|
{ |
120
|
|
|
return $this->getTokensAsString(0, count($this->tokens)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|