FilesProvider   A
last analyzed

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
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