Completed
Push — master ( f81cf6...7fbbb0 )
by Oleg
03:40
created

DefaultCodeFeederPart::getCalleeCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Config\Code\Parts;
5
6
use Nette\PhpGenerator\ClassType;
7
use Nette\PhpGenerator\PhpLiteral;
8
use Nette\PhpGenerator\PhpNamespace;
9
use SlayerBirden\DFCodeGeneration\Code\Printer\NsArrayPrinter;
10
use SlayerBirden\DFCodeGeneration\Generator\Config\Code\ArrayPartInterface;
11
use Zend\Filter\Word\UnderscoreToCamelCase;
12
13
final class DefaultCodeFeederPart implements ArrayPartInterface
14
{
15 2
    public function feed(string $key, array $data, ClassType $class, PhpNamespace $namespace): void
16
    {
17 2
        $class->addMethod($this->getMethodName($key))
18 2
            ->setVisibility(ClassType::VISIBILITY_PUBLIC)
19 2
            ->setReturnType('array')
20 2
            ->setBody(
21 2
                sprintf('return %s;', (new NsArrayPrinter($namespace))->printArray($data, 1, ''))
22
            );
23 2
    }
24
25 2
    private function getMethodName(string $key): string
26
    {
27 2
        if (strpos($key, '\\')) {
28
            //get the last part
29 2
            $parts = explode('\\', $key);
30 2
            $key = end($parts);
31
        }
32 2
        return 'get' . (new UnderscoreToCamelCase())->filter($key) . 'Config';
0 ignored issues
show
Bug introduced by
Are you sure new Zend\Filter\Word\Und...melCase()->filter($key) of type string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        return 'get' . /** @scrutinizer ignore-type */ (new UnderscoreToCamelCase())->filter($key) . 'Config';
Loading history...
33
    }
34
35 2
    public function matches(string $key): bool
36
    {
37 2
        return true;
38
    }
39
40 2
    public function getCalleeCode(string $key, array $data, PhpNamespace $namespace): PhpLiteral
41
    {
42 2
        return new PhpLiteral(sprintf('$this->%s()', $this->getMethodName($key)));
43
    }
44
}
45