Passed
Push — master ( bd0ac7...2480b7 )
by Alexander
02:24
created

EnvEncoder::getDefaultOptions()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\Env;
11
12
class EnvEncoder implements Encoder
13
{
14 11
    public function getDefaultOptions(): array
15
    {
16 11
        return [];
17
    }
18
19 11
    public function supports($value): bool
20
    {
21 11
        if (!$value instanceof Closure) {
22 8
            return false;
23
        }
24 10
        $reflection = new ReflectionClosure($value);
25
26 10
        $closureReflection = $reflection->getClosureScopeClass();
27 10
        $closureClassOwnerName = $closureReflection->getName();
28
29 10
        return is_a($closureClassOwnerName, Env::class, true);
30
    }
31
32 5
    public function encode($value, $depth, array $options, callable $encode)
33
    {
34 5
        $reflection = new ReflectionClosure($value);
35 5
        $variables = $reflection->getStaticVariables();
36 5
        $key = $variables['key'];
37 5
        $default = $variables['default'] ?? null;
38
39 5
        return str_replace(
40 5
            ['$key', '$default'],
41 5
            ["'$key'", Helper::exportVar($default)],
42 5
            substr(
43 5
                $reflection->getCode(),
44 5
                16
45
            ),
46
        );
47
    }
48
}
49