ReturnTypeTemplate::doGenerate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.7
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator\Template;
5
6
/**
7
 * Class ReturnTypeTemplate
8
 * @package Moka\Generator\Template
9
 */
10
class ReturnTypeTemplate implements TemplateInterface
11
{
12
    private const TEMPLATE = ': %s%s';
13
14
    /**
15
     * @param \Reflector|\ReflectionMethod $method
16
     * @return string
17
     */
18 7
    public static function generate(\Reflector $method): string
19
    {
20 7
        return static::doGenerate($method);
21
    }
22
23
    /**
24
     * @param \ReflectionMethod $method
25
     * @return string
26
     */
27 7
    protected static function doGenerate(\ReflectionMethod $method): string
28
    {
29 7
        $originalReturnType = $method->getReturnType();
30 7
        $allowsNull = $originalReturnType->allowsNull()
31 1
            ? '?'
32 7
            : '';
33
34 7
        $returnType = 'self' === $originalReturnType->getName()
35 1
            ? $method->getDeclaringClass()->name
36 7
            : $originalReturnType->getName();
37
38 7
        return sprintf(
39 7
            self::TEMPLATE,
40
            $allowsNull,
41
            $returnType
42
        );
43
    }
44
}
45