Completed
Branch master (06cb84)
by Tomáš
06:00
created

FilesProvider::wrapFilesToValueObjects()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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