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

EnvEncoder::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
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\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