Issues (22)

src/utils/Helper.php (5 issues)

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