Passed
Pull Request — master (#36)
by Dmitriy
34:35 queued 19:37
created

Helper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 34
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncoder() 0 7 2
A createEncoder() 0 8 1
A exportVar() 0 3 1
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
    public static function exportVar($value): string
21
    {
22
        return static::getEncoder()->encode($value);
23
    }
24
25
    private static $encoder;
26
27
    private static function getEncoder()
28
    {
29
        if (self::$encoder === null) {
30
            self::$encoder = static::createEncoder();
31
        }
32
33
        return self::$encoder;
34
    }
35
36
    private static function createEncoder()
37
    {
38
        $encoder = new PHPEncoder([
39
            'object.format' => 'serialize',
40
        ]);
41
        $encoder->addEncoder(new ClosureEncoder(), true);
42
43
        return $encoder;
44
    }
45
}
46