SimpleClassToFile   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A classToFileCandidates() 0 22 3
1
<?php
2
3
namespace Phpactor\ClassFileConverter\Adapter\Simple;
4
5
use Phpactor\ClassFileConverter\Domain\ClassToFile;
6
use Phpactor\ClassFileConverter\Domain\FilePathCandidates;
7
use Phpactor\ClassFileConverter\Domain\ClassName;
8
use Phpactor\ClassFileConverter\Domain\FilePath;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use RegexIterator;
12
13
class SimpleClassToFile implements ClassToFile
14
{
15
    /**
16
     * @var string
17
     */
18
    private $cwd;
19
20
    /**
21
     * @var ClassScanner
22
     */
23
    private $classScanner;
24
25
    public function __construct(string $cwd)
26
    {
27
        $this->cwd = $cwd;
28
        $this->classScanner = new ClassScanner();
29
    }
30
31
    public function classToFileCandidates(ClassName $className): FilePathCandidates
32
    {
33
        $candidates = [];
34
        $pattern = sprintf(
35
            '{^.*/%s.php$}',
36
            $className->name()
37
        );
38
39
        $iterator = new RecursiveDirectoryIterator($this->cwd);
40
        $iterator = new RecursiveIteratorIterator($iterator);
41
        $iterator = new RegexIterator($iterator, $pattern);
42
43
        foreach ($iterator as $phpFile) {
44
            if (ClassName::fromString(
45
                $this->classScanner->getClassNameFromFile($phpFile->getPathName())
46
            ) == $className) {
47
                $candidates[] = FilePath::fromString($phpFile->getPathName());
48
            }
49
        }
50
51
        return FilePathCandidates::fromFilePaths($candidates);
52
    }
53
}
54