Passed
Pull Request — master (#53)
by Dmitriy
10:48
created

EnvEncoder::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 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;
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
        $value = current($reflection->getStaticVariables());
40
41
        return substr(
42
            str_replace('$key', "'$value'", $reflection->getCode()),
43
            15,
44
            -1
45
        );
46
    }
47
}
48