Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/Service/Composer/OutputParser.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\Composer;
15
16
/**
17
 * Class OutputParser
18
 */
19
class OutputParser
20
{
21
    /**
22
     * Parse to array
23
     *
24
     * @param string $output
25
     *
26
     * @return array
27
     */
28
    public static function parseRequire($output)
29
    {
30
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
31
        $installedLogs = array_filter(
32
            array_map(
33
                function ($line) {
34
                    $matches = [];
35
                    preg_match('/^  - Installing (.*?) \((.*?)\) .*/', $line, $matches);
36
37
                    return $matches;
38
                },
39
                $rowArray
40
            )
41
        );
42
43
        // 'package name' => 'version'
44
        return ['installed' => array_column($installedLogs, 2, 1)];
45
    }
46
47
    /**
48
     * Parse to array
49
     *
50
     * @param string $output
51
     *
52
     * @return array
53
     */
54
    public static function parseInfo($output)
55
    {
56
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
57
        $infoLogs = array_filter(array_map(function ($line) {
58
            $matches = [];
59
            preg_match('/^(name|descrip.|keywords|versions|type|license|source|dist|names)\s*:\s*(.*)$/', $line, $matches);
60
61
            return $matches;
62
        }, $rowArray));
63
64
        // 'name' => 'value'
65
        $result = array_column($infoLogs, 2, 1);
66
        $result['requires'] = static::parseArrayInfoOutput($rowArray, 'requires');
0 ignored issues
show
Since parseArrayInfoOutput() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of parseArrayInfoOutput() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
67
        $result['requires (dev)'] = static::parseArrayInfoOutput($rowArray, 'requires (dev)');
0 ignored issues
show
Since parseArrayInfoOutput() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of parseArrayInfoOutput() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
68
69
        return $result;
70
    }
71
72
    /**
73
     * Parse to array
74
     *
75
     * @param string $output
76
     *
77
     * @return array|mixed
78
     */
79
    public static function parseConfig($output)
80
    {
81
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
82
        $rowArray = array_filter($rowArray, function ($line) {
83
            return !preg_match('/^<warning>.*/', $line);
84
        });
85
86
        return $rowArray ? json_decode(array_shift($rowArray), true) : [];
87
    }
88
89
    /**
90
     * Parse to array
91
     *
92
     * @param string $output
93
     *
94
     * @return array
95
     */
96
    public static function parseList($output)
97
    {
98
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
99
        $rawConfig = array_map(function ($line) {
100
            $matches = [];
101
            preg_match('/^\[(.*?)\]\s?(.*)$/', $line, $matches);
102
103
            return $matches;
104
        }, $rowArray);
105
106
        $rawConfig = array_column($rawConfig, 2, 1);
107
108
        $result = [];
109
110
        foreach ($rawConfig as $path => $value) {
111
            $arr = &$result;
112
            $keys = explode('.', $path);
113
            foreach ($keys as $key) {
114
                $arr = &$arr[$key];
115
            }
116
            $arr = $value;
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * @param $rowArray
124
     * @param string $key
125
     *
126
     * @return array
127
     */
128
    private static function parseArrayInfoOutput($rowArray, $key)
129
    {
130
        $result = [];
131
        $start = false;
132
        foreach ($rowArray as $line) {
133
            if ($line === $key) {
134
                $start = true;
135
                continue;
136
            }
137
            if ($start) {
138
                if (empty($line)) {
139
                    break;
140
                }
141
                $parts = explode(' ', $line);
142
                $result[$parts[0]] = $parts[1];
143
            }
144
        }
145
146
        return $result;
147
    }
148
149
    /**
150
     * Parse to composer version
151
     *
152
     * @param string $output
153
     *
154
     * @return array|mixed|string
155
     */
156
    public static function parseComposerVersion($output)
157
    {
158
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
159
        $rowArray = array_filter($rowArray, function ($line) {
160
            return preg_match('/^Composer */', $line);
161
        });
162
163
        return array_shift($rowArray);
164
    }
165
}
166