Passed
Push — master ( fb5a5d...3e18cb )
by Andrii
11:39
created

Helper::createEncoder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Composer plugin for config assembling
4
 *
5
 * @link      https://github.com/hiqdev/composer-config-plugin
6
 * @package   composer-config-plugin
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\composer\config;
12
13
use Closure;
14
use Riimu\Kit\PHPEncoder\PHPEncoder;
15
16
/**
17
 * Helper class.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Helper
22
{
23
    /**
24
     * Merges two or more arrays into one recursively.
25
     * Based on Yii2 yii\helpers\BaseArrayHelper::merge.
26
     * @return array the merged array
27
     */
28
    public static function mergeConfig(): array
29
    {
30
        $args = \func_get_args();
31
        $res = array_shift($args) ?: [];
32
        foreach ($args as $items) {
33
            if (!\is_array($items)) {
34
                continue;
35
            }
36
            foreach ($items as $k => $v) {
37
                if ($v instanceof \yii\helpers\UnsetArrayValue || $v instanceof \Yiisoft\Arrays\UnsetArrayValue) {
0 ignored issues
show
Bug introduced by
The type Yiisoft\Arrays\UnsetArrayValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type yii\helpers\UnsetArrayValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
                    unset($res[$k]);
39
                } elseif ($v instanceof \yii\helpers\ReplaceArrayValue || $v instanceof \Yiisoft\Arrays\ReplaceArrayValue) {
0 ignored issues
show
Bug introduced by
The type yii\helpers\ReplaceArrayValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Yiisoft\Arrays\ReplaceArrayValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
                    $res[$k] = $v->value;
41
                } elseif (\is_int($k)) {
42
                    /// XXX skip repeated values
43
                    if (\in_array($v, $res, true))  {
44
                        continue;
45
                    }
46
                    if (isset($res[$k])) {
47
                        $res[] = $v;
48
                    } else {
49
                        $res[$k] = $v;
50
                    }
51
                } elseif (\is_array($v) && isset($res[$k]) && \is_array($res[$k])) {
52
                    $res[$k] = self::mergeConfig($res[$k], $v);
53
                } else {
54
                    $res[$k] = $v;
55
                }
56
            }
57
        }
58
59
        return $res;
60
    }
61
62
    public static function exportDefines(array $defines): string
63
    {
64
        $res = '';
65
        foreach ($defines as $key => $value) {
66
            $var = static::exportVar($value);
67
            $res .= "defined('$key') or define('$key', $var);\n";
68
        }
69
70
        return $res;
71
    }
72
73
    /**
74
     * Returns PHP-executable string representation of given value.
75
     * Uses Riimu/Kit-PHPEncoder based `var_export` alternative.
76
     * And Opis/Closure to dump closures as PHP code.
77
     * @param mixed $value
78
     * @return string
79
     * @throws \ReflectionException
80
     */
81
    public static function exportVar($value): string
82
    {
83
        return static::getEncoder()->encode($value);
84
    }
85
86
    private static $encoder;
87
88
    private static function getEncoder()
89
    {
90
        if (static::$encoder === null) {
0 ignored issues
show
Bug introduced by
Since $encoder is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $encoder to at least protected.
Loading history...
91
            static::$encoder = static::createEncoder();
92
        }
93
94
        return static::$encoder;
95
    }
96
97
    private static function createEncoder()
98
    {
99
        $encoder = new PHPEncoder([
100
            'object.format' => 'serialize',
101
        ]);
102
        $encoder->addEncoder(new ClosureEncoder(), true);
103
104
        return $encoder;
105
    }
106
}
107