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

FilesProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 53
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFilesForSource() 0 12 2
A wrapFilesToValueObjects() 0 8 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
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