Passed
Push — master ( 9a3526...e85357 )
by Alexander
03:52
created

Helper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 38.46%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
c 2
b 0
f 0
dl 0
loc 36
ccs 5
cts 13
cp 0.3846
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncoder() 0 7 2
A exportVar() 0 3 1
A createEncoder() 0 10 1
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