Completed
Push — master ( 2fd8e1...1442eb )
by Tomáš
03:40
created

FileToTokensParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 45
ccs 16
cts 16
cp 1
rs 10

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