ProxiedMethodReturnExpression::generate()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 3
cts 3
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 8
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\Generator\Util;
6
7
use ReflectionMethod;
8
9
/**
10
 * Utility class to generate return expressions in method, given a method signature.
11
 *
12
 * This is required since return expressions may be forbidden by the method signature (void).
13
 */
14
final class ProxiedMethodReturnExpression
15 6
{
16
    public static function generate(string $returnedValueExpression, ?ReflectionMethod $originalMethod) : string
17 6
    {
18 2
        $originalReturnType = $originalMethod === null
19
            ? null
20
            : $originalMethod->getReturnType();
21 4
22
        $originalReturnTypeName = $originalReturnType === null
23
            ? null
24
            : $originalReturnType->getName();
25
26
        if ($originalReturnTypeName === 'void') {
27
            return $returnedValueExpression . ";\nreturn;";
28
        }
29
30
        return 'return ' . $returnedValueExpression . ';';
31
    }
32
}
33