Completed
Pull Request — master (#37)
by Angelo
02:34
created

ProxyClassGenerator::generateCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Generator;
5
6
7
use Moka\Proxy\ProxyInterface;
8
9
class ProxyClassGenerator
10
{
11
    const UNSAFE_METHODS = ['__call', '__construct', '__destruct', '__clone'];
12
13
    private static $template = '
14
    return new class extends %s implements %s
15
    {
16
        use %s;
17
        %s
18
    };
19
    ';
20
21 8
    public static function generateCode(string $classWillBeEtended): string
22
    {
23 8
        $reflection = new \ReflectionClass($classWillBeEtended);
24 8
        $methods = $reflection->getMethods();
25 8
        $methodsArray = [];
26 8
        foreach ($methods as $method) {
27 8
            if (!in_array($method->getName(), self::UNSAFE_METHODS, true)) {
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28 8
                $methodsArray[] = ProxyMethodGenerator::generateMethodString($method);
29
            }
30
        }
31
32 8
        return sprintf(
33 8
            self::$template,
34 8
            $classWillBeEtended,
35 8
            ProxyInterface::class,
36 8
            ProxyTrait::class,
37 8
            implode(PHP_EOL, $methodsArray)
38
        );
39
    }
40
}
41