Completed
Push — master ( 4a6b89...c84e82 )
by Andrii
17:16 queued 14s
created

Helper::fixConfig()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
cc 5
nc 8
nop 1
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\utils;
12
13
use Riimu\Kit\PHPEncoder\PHPEncoder;
14
use hiqdev\composer\config\utils\RemoveArrayKeys;
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 fixConfig(array $config): array
63
    {
64
        $remove = false;
65
        foreach ($config as $key => &$value) {
66
            if (is_array($value)) {
67
                $value = static::fixConfig($value);
68
            } elseif ($value instanceof RemoveArrayKeys) {
69
                $remove = true;
70
                unset($config[$key]);
71
            }
72
        }
73
        if ($remove) {
74
            return array_values($config);
75
        }
76
77
        return $config;
78
    }
79
80
    public static function exportDefines(array $defines): string
81
    {
82
        $res = '';
83
        foreach ($defines as $key => $value) {
84
            $var = static::exportVar($value);
85
            $res .= "defined('$key') or define('$key', $var);\n";
86
        }
87
88
        return $res;
89
    }
90
91
    /**
92
     * Returns PHP-executable string representation of given value.
93
     * Uses Riimu/Kit-PHPEncoder based `var_export` alternative.
94
     * And Opis/Closure to dump closures as PHP code.
95
     * @param mixed $value
96
     * @return string
97
     * @throws \ReflectionException
98
     */
99
    public static function exportVar($value): string
100
    {
101
        return static::getEncoder()->encode($value);
102
    }
103
104
    private static $encoder;
105
106
    private static function getEncoder()
107
    {
108
        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...
109
            static::$encoder = static::createEncoder();
110
        }
111
112
        return static::$encoder;
113
    }
114
115
    private static function createEncoder()
116
    {
117
        $encoder = new PHPEncoder([
118
            'object.format' => 'serialize',
119
        ]);
120
        $encoder->addEncoder(new ClosureEncoder(), true);
121
122
        return $encoder;
123
    }
124
}
125