Completed
Push — master ( bdcf7d...eb1b80 )
by Tomáš
05:51
created

FilesProvider::getFilesForSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2.0185
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\Provider;
9
10
use SplFileInfo;
11
use Symplify\PHP7_CodeSniffer\File\File;
12
use Symplify\PHP7_CodeSniffer\File\FileFactory;
13
use Symplify\PHP7_CodeSniffer\File\Finder\SourceFinder;
14
use Symplify\PHP7_CodeSniffer\Ruleset;
15
16
final class FilesProvider
17
{
18
    /**
19
     * @var SourceFinder
20
     */
21
    private $sourceFinder;
22
23
    /**
24
     * @var FileFactory
25
     */
26
    private $fileFactory;
27
28
    /**
29
     * @var File[][]
30
     */
31
    private $filesBySource = [];
32
33 1
    public function __construct(SourceFinder $sourceFinder, FileFactory $fileFactory)
34
    {
35 1
        $this->sourceFinder = $sourceFinder;
36 1
        $this->fileFactory = $fileFactory;
37 1
    }
38
39
    /**
40
     * @return File[]
41
     */
42 1
    public function getFilesForSource(array $source, bool $isFixer) : array
43
    {
44 1
        $sourceHash = md5(json_encode($source));
45 1
        if (isset($this->filesBySource[$sourceHash])) {
46
            return $this->filesBySource[$sourceHash];
47
        }
48
49 1
        return $this->filesBySource[$sourceHash] = $this->wrapFilesToValueObjects(
50 1
            $this->sourceFinder->find($source),
51
            $isFixer
52
        );
53
    }
54
55
    /**
56
     * @param SplFileInfo[] $files
57
     * @param bool $isFixer
58
     * @return File[]
59
     */
60 1
    private function wrapFilesToValueObjects(array $files, bool $isFixer) : array
61
    {
62 1
        foreach ($files as $name => $fileInfo) {
63 1
            $files[$name] = $this->fileFactory->create($name, $isFixer);
64
        }
65
66 1
        return $files;
67
    }
68
}
69