BuilderRequireEncoder::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.9666
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config\Util;
6
7
use Closure;
8
use Opis\Closure\ReflectionClosure;
9
use Riimu\Kit\PHPEncoder\Encoder\Encoder;
10
use Yiisoft\Composer\Config\Builder;
11
12
class BuilderRequireEncoder implements Encoder
13
{
14 24
    public function getDefaultOptions(): array
15
    {
16 24
        return [];
17
    }
18
19 24
    public function supports($value): bool
20
    {
21 24
        if (!$value instanceof Closure) {
22 18
            return false;
23
        }
24 20
        $reflection = new ReflectionClosure($value);
25
26 20
        $closureReflection = $reflection->getClosureScopeClass();
27
28 20
        if (null === $closureReflection) {
29
            return false;
30
        }
31
32 20
        $closureClassOwnerName = $closureReflection->getName();
33
34 20
        return is_a($closureClassOwnerName, Builder::class, true);
35
    }
36
37
    public function encode($value, $depth, array $options, callable $encode)
38
    {
39
        $reflection = new ReflectionClosure($value);
40
        $variables = $reflection->getStaticVariables();
41
        $config = $variables['config'];
42
43
        return str_replace(
44
            ['$config'],
45
            ["'$config.php'"],
46
            substr(
47
                $reflection->getCode(),
48
                16
49
            ),
50
        );
51
    }
52
}
53