Passed
Pull Request — master (#64)
by Alexander
02:29
created

EnvEncoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 33
ccs 20
cts 20
cp 1
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 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