CreateTemporaryPsalmXmlFile   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 29 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UsageFinder;
6
7
use function file_exists;
8
use function file_get_contents;
9
use function file_put_contents;
10
use function sprintf;
11
use function str_replace;
12
use function sys_get_temp_dir;
13
use function tempnam;
14
15
final class CreateTemporaryPsalmXmlFile
16
{
17
    private const PROJECT_FILES_XML = <<<'PROJECT_FILES'
18
    <projectFiles>
19
        {{ directoryXml }}
20
        {{ ignoreFilesXml }}
21
    </projectFiles>
22
PROJECT_FILES;
23
24
    private const DIRECTORY_XML = <<<'DIRECTORY'
25
        <directory name="%s" />
26
DIRECTORY;
27
28
    private const IGNORE_FILES_XML = <<<'IGNORE_FILES'
29
        <ignoreFiles>
30
            <directory name="%s" />
31
        </ignoreFiles>
32
IGNORE_FILES;
33
34 7
    public function __invoke(string $path) : string
35
    {
36 7
        $templatePath = __DIR__ . '/../usage-finder-psalm.xml.template';
37
38 7
        $tmpPath = tempnam(sys_get_temp_dir(), 'usage-finder-psalm') . '.xml';
39
40 7
        $vendorPath = null;
41
42 7
        if (file_exists($path . '/vendor')) {
43 5
            $vendorPath = 'vendor';
44
        }
45
46 7
        $codePath = (new GuessCodePath())->__invoke($path) ?? $path;
47
48 7
        $directoryXml   = sprintf(self::DIRECTORY_XML, $codePath);
49 7
        $ignoreFilesXml = $vendorPath !== null ? sprintf(self::IGNORE_FILES_XML, $vendorPath) : '';
50
51 7
        $projectFilesXml = str_replace(
52 7
            ['{{ directoryXml }}', '{{ ignoreFilesXml }}'],
53 7
            [$directoryXml, $ignoreFilesXml],
54 7
            self::PROJECT_FILES_XML
55
        );
56
57 7
        $configXml = file_get_contents($templatePath);
58 7
        $configXml = str_replace('{{ projectFilesXml }}', $projectFilesXml, $configXml);
59
60 7
        file_put_contents($tmpPath, $configXml);
61
62 7
        return $tmpPath;
63
    }
64
}
65