Passed
Push — master ( b220c2...54e290 )
by Alexander
14:58 queued 12:49
created

EnvEncoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 33
ccs 0
cts 26
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 11 2
A encode() 0 13 1
A getDefaultOptions() 0 3 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
    public function getDefaultOptions(): array
15
    {
16
        return [];
17
    }
18
19
    public function supports($value): bool
20
    {
21
        if (!$value instanceof Closure) {
22
            return false;
23
        }
24
        $reflection = new ReflectionClosure($value);
25
26
        $closureReflection = $reflection->getClosureScopeClass();
27
        $closureClassOwnerName = $closureReflection->getName();
28
29
        return is_a($closureClassOwnerName, Env::class, true);
30
    }
31
32
    public function encode($value, $depth, array $options, callable $encode)
33
    {
34
        $reflection = new ReflectionClosure($value);
35
        $variables = $reflection->getStaticVariables();
36
        $key = $variables['key'];
37
        $default = $variables['default'] ?? null;
38
39
        return str_replace(
40
            ['$key', '$default'],
41
            ["'$key'", Helper::exportVar($default)],
42
            substr(
43
                $reflection->getCode(),
44
                16
45
            ),
46
        );
47
    }
48
}
49