Passed
Push — master ( bd0ac7...2480b7 )
by Alexander
02:24
created

Helper::getEncoder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
namespace Yiisoft\Composer\Config\Util;
4
5
use Riimu\Kit\PHPEncoder\PHPEncoder;
6
7
/**
8
 * Helper class.
9
 */
10
class Helper
11
{
12
    /**
13
     * Returns PHP-executable string representation of given value.
14
     * Uses Riimu/Kit-PHPEncoder based `var_export` alternative.
15
     * And Opis/Closure to dump closures as PHP code.
16
     * @param mixed $value
17
     * @return string
18
     * @throws \ReflectionException
19
     */
20 11
    public static function exportVar($value): string
21
    {
22 11
        return static::getEncoder()->encode($value);
23
    }
24
25
    private static ?PHPEncoder $encoder = null;
26
27 11
    private static function getEncoder(): PHPEncoder
28
    {
29 11
        if (self::$encoder === null) {
30
            self::$encoder = static::createEncoder();
31
        }
32
33 11
        return self::$encoder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::encoder could return the type null which is incompatible with the type-hinted return Riimu\Kit\PHPEncoder\PHPEncoder. Consider adding an additional type-check to rule them out.
Loading history...
34
    }
35
36
    private static function createEncoder(): PHPEncoder
37
    {
38
        $encoder = new PHPEncoder([
39
            'object.format' => 'serialize',
40
        ]);
41
        $encoder->addEncoder(new ClosureEncoder(), true);
42
        $encoder->addEncoder(new EnvEncoder(), true);
43
44
        return $encoder;
45
    }
46
}
47