Passed
Push — master ( 9a3526...e85357 )
by Alexander
03:52
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
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config\Util;
6
7
use Riimu\Kit\PHPEncoder\PHPEncoder;
8
9
/**
10
 * Helper class.
11
 */
12
class Helper
13
{
14
    /**
15
     * Returns PHP-executable string representation of given value.
16
     * Uses Riimu/Kit-PHPEncoder based `var_export` alternative.
17
     * And Opis/Closure to dump closures as PHP code.
18
     * @param mixed $value
19
     * @return string
20
     * @throws \ReflectionException
21
     */
22 11
    public static function exportVar($value): string
23
    {
24 11
        return static::getEncoder()->encode($value);
25
    }
26
27
    private static ?PHPEncoder $encoder = null;
28
29 11
    private static function getEncoder(): PHPEncoder
30
    {
31 11
        if (self::$encoder === null) {
32
            self::$encoder = static::createEncoder();
33
        }
34
35 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...
36
    }
37
38
    private static function createEncoder(): PHPEncoder
39
    {
40
        $encoder = new PHPEncoder([
41
            'object.format' => 'serialize',
42
        ]);
43
        $encoder->addEncoder(new ClosureEncoder(), true);
44
        $encoder->addEncoder(new EnvEncoder(), true);
45
        $encoder->addEncoder(new ObjectEncoder(), true);
46
47
        return $encoder;
48
    }
49
}
50