Passed
Pull Request — master (#53)
by Dmitriy
19:08 queued 04:06
created

EnvEncoder::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 13
rs 9.9332
cc 1
nc 1
nop 4
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Composer\Config\Env was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
        if (!is_a($closureClassOwnerName, Env::class, true)) {
30
            return false;
31
        }
32
33
        return strpos($reflection->getCode(), 'static fn() => $_ENV') !== false;
34
    }
35
36
    public function encode($value, $depth, array $options, callable $encode)
37
    {
38
        $reflection = new ReflectionClosure($value);
39
        $variables = $reflection->getStaticVariables();
40
        $key = $variables['key'];
41
        $default = $variables['default'] ?? null;
42
43
        return str_replace(
44
            ['$key', '$default'],
45
            ["'$key'", Helper::exportVar($default)],
46
            substr(
47
                $reflection->getCode(),
48
                15
49
            ),
50
        );
51
    }
52
}
53