FileFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2
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\File;
9
10
use Nette\FileNotFoundException;
11
use Symplify\PHP7_CodeSniffer\Application\Fixer;
12
use Symplify\PHP7_CodeSniffer\Parser\EolCharDetector;
13
use Symplify\PHP7_CodeSniffer\Parser\FileToTokensParser;
14
use Symplify\PHP7_CodeSniffer\Report\ErrorDataCollector;
15
16
final class FileFactory
17
{
18
    /**
19
     * @var Fixer
20
     */
21
    private $fixer;
22
23
    /**
24
     * @var ErrorDataCollector
25
     */
26
    private $reportCollector;
27
28
    /**
29
     * @var FileToTokensParser
30
     */
31
    private $fileToTokenParser;
32
33
    /**
34
     * @var EolCharDetector
35
     */
36
    private $eolCharDetector;
37
38 16
    public function __construct(
39
        Fixer $fixer,
40
        ErrorDataCollector $reportCollector,
41
        FileToTokensParser $fileToTokenParser,
42
        EolCharDetector $eolCharDetector
43
    ) {
44 16
        $this->fixer = $fixer;
45 16
        $this->reportCollector = $reportCollector;
46 16
        $this->fileToTokenParser = $fileToTokenParser;
47 16
        $this->eolCharDetector = $eolCharDetector;
48 16
    }
49
50 15
    public function create(string $filePath, bool $isFixer) : File
51
    {
52 15
        $this->ensureFileExists($filePath);
53
54 14
        $tokens = $this->fileToTokenParser->parseFromFilePath($filePath);
55 14
        $eolChar = $this->eolCharDetector->detectForFilePath($filePath);
56
57 14
        return new File(
58
            $filePath,
59
            $tokens,
60 14
            $this->fixer,
61 14
            $this->reportCollector,
62
            $isFixer,
63
            $eolChar
64
        );
65
    }
66
67 15
    private function ensureFileExists(string $filePath)
68
    {
69 15
        if (!is_file($filePath) || !file_exists($filePath)) {
70 1
            throw new FileNotFoundException(sprintf(
71 1
                'File "%s" was not found.',
72
                $filePath
73
            ));
74
        }
75 14
    }
76
}
77