FileToTokensParser::parseFromFilePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\Parser;
9
10
use Nette\Utils\FileSystem;
11
use PHP_CodeSniffer\Tokenizers\PHP;
12
use stdClass;
13
14
final class FileToTokensParser
15
{
16
    /**
17
     * @var EolCharDetector
18
     */
19
    private $eolCharDetector;
20
21 17
    public function __construct(EolCharDetector $eolCharDetector)
22
    {
23 17
        $this->eolCharDetector = $eolCharDetector;
24 17
    }
25
26
    /**
27
     * @var stdClass
28
     */
29
    private $legacyConfig;
30
31 15
    public function parseFromFilePath(string $filePath) : array
32
    {
33 15
        $fileContent = FileSystem::read($filePath);
34 15
        $eolChar = $this->eolCharDetector->detectForContent($fileContent);
35
36 15
        return $this->parseLegacyWithFileContentAndEolChar($fileContent, $eolChar);
37
    }
38
39 15
    private function parseLegacyWithFileContentAndEolChar(
40
        string $fileContent,
41
        string $eolChar
42
    ) : array {
43 15
        return (new PHP($fileContent, $this->getLegacyConfig(), $eolChar))->getTokens();
44
    }
45
46 15
    private function getLegacyConfig() : stdClass
47
    {
48 15
        if ($this->legacyConfig) {
49 1
            return $this->legacyConfig;
50
        }
51
52 15
        $config = new stdClass();
53 15
        $config->tabWidth = 4;
54 15
        $this->legacyConfig = $config;
55
56 15
        return $this->legacyConfig;
57
    }
58
}
59