Completed
Branch master (c5ceed)
by Tomáš
33:54 queued 13:17
created

FileToTokensParser::parseFromFilePath()   A

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
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