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

FileToTokensParser::getLegacyConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2.0116
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