Completed
Push — master ( 3b5f38...c5ceed )
by Tomáš
04:59
created

FileToTokensParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 52
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseFromFilePath() 0 7 1
A parseLegacyWithFileContentAndEolChar() 0 6 1
A getLegacyConfig() 0 12 2
A setupLegacyVerbosityConstant() 0 6 2
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