Passed
Push — feature/custom-services-v2 ( 35de26...30061f )
by Chema
03:34
created

UseBlockParser::lookInCurrentNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver\DocBlockService;
6
7
final class UseBlockParser
8
{
9 5
    public function getUseStatement(string $className, string $phpCode): string
10
    {
11 5
        if ($phpCode === '') {
12 1
            return '';
13
        }
14
15 4
        $fullyQualifiedClassName = $this->searchInUsesStatements($className, $phpCode);
16 4
        if ($fullyQualifiedClassName !== '') {
17 3
            return $fullyQualifiedClassName;
18
        }
19
20 2
        $namespace = $this->lookInCurrentNamespace($phpCode);
21
22 2
        return sprintf('%s\\%s', $namespace, $className);
23
    }
24
25 4
    private function searchInUsesStatements(string $className, string $phpCode): string
26
    {
27 4
        $needle = "{$className};";
28 4
        $lines = array_filter(
29 4
            explode(PHP_EOL, $phpCode),
30 4
            static fn (string $l) => str_contains($l, $needle)
31
        );
32
        /** @psalm-suppress RedundantCast */
33 4
        $lineSplit = (array)explode(' ', (string)reset($lines));
34
35 4
        return rtrim($lineSplit[1] ?? '', ';');
36
    }
37
38 2
    private function lookInCurrentNamespace(string $phpCode): string
39
    {
40 2
        $lines = array_filter(
41 2
            explode(PHP_EOL, $phpCode),
42 2
            static fn (string $l) => str_contains($l, 'namespace')
43
        );
44
        /** @psalm-suppress RedundantCast */
45 2
        $lineSplit = (array)explode(' ', (string)reset($lines));
46
47 2
        return rtrim($lineSplit[1] ?? '', ';');
48
    }
49
}
50