Passed
Pull Request — master (#22)
by Dmitriy
28:42 queued 13:51
created

Helper::createEncoder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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
     * Merges two or more arrays into one recursively.
14
     * Based on Yii2 yii\helpers\BaseArrayHelper::merge.
15
     * @return array the merged array
16
     */
17
    public static function mergeConfig(): array
18
    {
19
        $args = \func_get_args();
20
        $res = array_shift($args) ?: [];
21
        foreach ($args as $items) {
22
            if (!\is_array($items)) {
23
                continue;
24
            }
25
            foreach ($items as $k => $v) {
26
                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...
27
                    unset($res[$k]);
28
                } 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...
29
                    $res[$k] = $v->value;
30
                } elseif (\is_int($k)) {
31
                    /// XXX skip repeated values
32
                    if (\in_array($v, $res, true)) {
33
                        continue;
34
                    }
35
                    if (isset($res[$k])) {
36
                        $res[] = $v;
37
                    } else {
38
                        $res[$k] = $v;
39
                    }
40
                } elseif (\is_array($v) && isset($res[$k]) && \is_array($res[$k])) {
41
                    $res[$k] = self::mergeConfig($res[$k], $v);
42
                } else {
43
                    $res[$k] = $v;
44
                }
45
            }
46
        }
47
48
        return $res;
49
    }
50
51
    public static function fixConfig(array $config): array
52
    {
53
        $remove = false;
54
        foreach ($config as $key => &$value) {
55
            if (is_array($value)) {
56
                $value = static::fixConfig($value);
57
            } elseif ($value instanceof RemoveArrayKeys) {
58
                $remove = true;
59
                unset($config[$key]);
60
            }
61
        }
62
        if ($remove) {
63
            return array_values($config);
64
        }
65
66
        return $config;
67
    }
68
69
    public static function exportDefines(array $defines): string
70
    {
71
        $res = '';
72
        foreach ($defines as $key => $value) {
73
            $var = static::exportVar($value);
74
            $res .= "defined('$key') or define('$key', $var);\n";
75
        }
76
77
        return $res;
78
    }
79
80
    /**
81
     * Returns PHP-executable string representation of given value.
82
     * Uses Riimu/Kit-PHPEncoder based `var_export` alternative.
83
     * And Opis/Closure to dump closures as PHP code.
84
     * @param mixed $value
85
     * @return string
86
     * @throws \ReflectionException
87
     */
88
    public static function exportVar($value): string
89
    {
90
        return static::getEncoder()->encode($value);
91
    }
92
93
    private static $encoder;
94
95
    private static function getEncoder()
96
    {
97
        if (self::$encoder === null) {
98
            self::$encoder = static::createEncoder();
99
        }
100
101
        return self::$encoder;
102
    }
103
104
    private static function createEncoder()
105
    {
106
        $encoder = new PHPEncoder([
107
            'object.format' => 'serialize',
108
        ]);
109
        $encoder->addEncoder(new ClosureEncoder(), true);
110
111
        return $encoder;
112
    }
113
}
114