Completed
Push — master ( 8f64a3...641323 )
by Jonathan
12s
created

FindClassMethodUsages   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 63
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 48 3
A cleanupFindClassMethodUsagesPlugin() 0 3 1
A copyFindClassMethodUsagesPlugin() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UsageFinder;
6
7
use Symfony\Component\Process\Process;
8
use function array_map;
9
use function copy;
10
use function is_array;
11
use function json_decode;
12
use function realpath;
13
use function sprintf;
14
use function unlink;
15
16
final class FindClassMethodUsages
17
{
18
    /**
19
     * @return array<int, ClassMethodUsage>
20
     */
21 1
    public function __invoke(
22
        string $path,
23
        ClassMethodReference $classMethodReference,
24
        int $threads = 1
25
    ) : array {
26 1
        $this->copyFindClassMethodUsagesPlugin($path);
27
28 1
        $configFile = (new CreateTemporaryPsalmXmlFile())->__invoke($path);
29
30 1
        $rootDir = realpath(__DIR__ . '/..');
31
32 1
        $process = new Process([
33 1
            'vendor/bin/psalm',
34 1
            sprintf('--config=%s', $configFile),
35 1
            sprintf('--root=%s', $path),
36 1
            sprintf('--threads=%d', $threads),
37 1
            '--output-format=json',
38 1
        ], $rootDir, [
39 1
            'USAGE_FINDER_NAME'        => $classMethodReference->getName(),
40 1
            'USAGE_FINDER_CLASS_NAME'  => $classMethodReference->getClassName(),
41 1
            'USAGE_FINDER_METHOD_NAME' => $classMethodReference->getMethodName(),
42
        ]);
43 1
        $process->setTimeout(0);
44
45 1
        $process->mustRun();
46
47 1
        $this->cleanupFindClassMethodUsagesPlugin($path);
48
49 1
        $output = $process->getOutput();
50
51 1
        unlink($configFile);
52
53 1
        if ($output === '') {
54
            return [];
55
        }
56
57 1
        $results = json_decode($output, true);
58
59 1
        if (! is_array($results)) {
60
            return [];
61
        }
62
63
        return array_map(static function (array $result) : ClassMethodUsage {
64 1
            return new ClassMethodUsage(
65 1
                $result['file_name'],
66 1
                $result['line_from']
67
            );
68 1
        }, $results);
69
    }
70
71 1
    private function copyFindClassMethodUsagesPlugin(string $path) : void
72
    {
73 1
        copy(__DIR__ . '/FindClassMethodUsagesPlugin.php', $path . '/FindClassMethodUsagesPlugin.php');
74 1
    }
75
76 1
    private function cleanupFindClassMethodUsagesPlugin(string $path) : void
77
    {
78 1
        unlink($path . '/FindClassMethodUsagesPlugin.php');
79 1
    }
80
}
81