|
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\Parser; |
|
9
|
|
|
|
|
10
|
|
|
use Nette\Utils\FileSystem; |
|
11
|
|
|
use PHP_CodeSniffer\Tokenizers\PHP; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
use Symplify\PHP7_CodeSniffer\File\File; |
|
14
|
|
|
|
|
15
|
|
|
final class FileToTokensParser |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EolCharDetector |
|
19
|
|
|
*/ |
|
20
|
|
|
private $eolCharDetector; |
|
21
|
|
|
|
|
22
|
1 |
|
public function __construct(EolCharDetector $eolCharDetector) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->eolCharDetector = $eolCharDetector; |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var stdClass |
|
29
|
|
|
*/ |
|
30
|
|
|
private $legacyConfig; |
|
31
|
|
|
|
|
32
|
1 |
|
public function parseFromFilePath(string $filePath) : array |
|
33
|
|
|
{ |
|
34
|
1 |
|
$fileContent = FileSystem::read($filePath); |
|
35
|
1 |
|
$eolChar = $this->eolCharDetector->detectForContent($fileContent); |
|
36
|
|
|
|
|
37
|
1 |
|
return $this->parseLegacyWithFileContentAndEolChar($fileContent, $eolChar); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
private function parseLegacyWithFileContentAndEolChar($fileContent, $eolChar) : array |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->setupLegacyVerbosityConstant(); |
|
43
|
|
|
|
|
44
|
1 |
|
return (new PHP($fileContent, $this->getLegacyConfig(), $eolChar))->getTokens(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
private function getLegacyConfig() : stdClass |
|
48
|
|
|
{ |
|
49
|
1 |
|
if ($this->legacyConfig) { |
|
50
|
|
|
return $this->legacyConfig; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$config = new stdClass(); |
|
54
|
1 |
|
$config->tabWidth = 4; |
|
55
|
1 |
|
$this->legacyConfig = $config; |
|
56
|
|
|
|
|
57
|
1 |
|
return $this->legacyConfig; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
private function setupLegacyVerbosityConstant() |
|
61
|
|
|
{ |
|
62
|
1 |
|
if (!defined('PHP_CODESNIFFER_VERBOSITY')) { |
|
63
|
1 |
|
define('PHP_CODESNIFFER_VERBOSITY', 0); |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|