Passed
Pull Request — master (#33)
by Andrii
10:33
created

Helper::mergeConfig()   C

Complexity

Conditions 15
Paths 3

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 22
nc 3
nop 0
dl 0
loc 32
rs 5.9166
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Helper::exportVar() 0 3 1
A Helper::getEncoder() 0 7 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Yiisoft\Composer\Config\Utils;
4
5
use Riimu\Kit\PHPEncoder\PHPEncoder;
6
7
/**
8
 * Helper class.
9
 */
10
class Helper
11
{
12
    public static function exportDefines(array $defines): string
13
    {
14
        $res = '';
15
        foreach ($defines as $key => $value) {
16
            $var = static::exportVar($value);
17
            $res .= "defined('$key') or define('$key', $var);\n";
18
        }
19
20
        return $res;
21
    }
22
23
    /**
24
     * Returns PHP-executable string representation of given value.
25
     * Uses Riimu/Kit-PHPEncoder based `var_export` alternative.
26
     * And Opis/Closure to dump closures as PHP code.
27
     * @param mixed $value
28
     * @return string
29
     * @throws \ReflectionException
30
     */
31
    public static function exportVar($value): string
32
    {
33
        return static::getEncoder()->encode($value);
34
    }
35
36
    private static $encoder;
37
38
    private static function getEncoder()
39
    {
40
        if (self::$encoder === null) {
41
            self::$encoder = static::createEncoder();
42
        }
43
44
        return self::$encoder;
45
    }
46
47
    private static function createEncoder()
48
    {
49
        $encoder = new PHPEncoder([
50
            'object.format' => 'serialize',
51
        ]);
52
        $encoder->addEncoder(new ClosureEncoder(), true);
53
54
        return $encoder;
55
    }
56
}
57