FilesProvider::wrapFilesToValueObjects()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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