Passed
Push — master ( cd14ff...da2bd7 )
by Alexander
03:37 queued 25s
created

EnvEncoder::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 10
cc 3
nc 3
nop 1
crap 3.0123
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 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, Env::class, true);
35
    }
36
37 10
    public function encode($value, $depth, array $options, callable $encode)
38
    {
39 10
        $reflection = new ReflectionClosure($value);
40 10
        $variables = $reflection->getStaticVariables();
41 10
        $key = $variables['key'];
42 10
        $default = $variables['default'] ?? null;
43
44 10
        return str_replace(
45 10
            ['$key', '$default'],
46 10
            ["'$key'", Helper::exportVar($default)],
47
            substr(
48 10
                $reflection->getCode(),
49 10
                16
50
            ),
51
        );
52
    }
53
}
54