Failed Conditions
Push — experimental/3.1 ( d52b28...afea38 )
by chihiro
28s
created

OutputParser::parseArrayInfoOutput()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 2
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
namespace Eccube\Service\Composer;
24
25
/**
26
 * Class OutputParser
27
 * @package Eccube\Service\Composer
28
 */
29
class OutputParser
30
{
31
    /**
32
     * Parse to array
33
     *
34
     * @param string $output
35
     * @return array
36
     */
37
    public static function parseRequire($output)
38
    {
39
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
40
        $installedLogs = array_filter(
41
            array_map(
42
                function ($line) {
43
                    $matches = array();
44
                    preg_match('/^  - Installing (.*?) \((.*?)\) .*/', $line, $matches);
45
46
                    return $matches;
47
                },
48
                $rowArray
49
            )
50
        );
51
52
        // 'package name' => 'version'
53
        return array('installed' => array_column($installedLogs, 2, 1));
54
    }
55
56
    /**
57
     * Parse to array
58
     *
59
     * @param string $output
60
     * @return array
61
     */
62
    public static function parseInfo($output)
63
    {
64
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
65
        $infoLogs = array_filter(array_map(function ($line) {
66
            $matches = array();
67
            preg_match('/^(name|descrip.|keywords|versions|type|license|source|dist|names)\s*:\s*(.*)$/', $line, $matches);
68
69
            return $matches;
70
        }, $rowArray));
71
72
        // 'name' => 'value'
73
        $result = array_column($infoLogs, 2, 1);
74
        $result['requires'] = static::parseArrayInfoOutput($rowArray, 'requires');
0 ignored issues
show
Bug introduced by
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...
75
        $result['requires (dev)'] = static::parseArrayInfoOutput($rowArray, 'requires (dev)');
0 ignored issues
show
Bug introduced by
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...
76
77
        return $result;
78
    }
79
80
    /**
81
     * Parse to array
82
     *
83
     * @param string $output
84
     * @return array|mixed
85
     */
86
    public static function parseConfig($output)
87
    {
88
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
89
        $rowArray = array_filter($rowArray, function ($line) {
90
            return !preg_match('/^<warning>.*/', $line);
91
        });
92
93
        return $rowArray ? json_decode(array_shift($rowArray), true) : array();
94
    }
95
96
    /**
97
     * Parse to array
98
     *
99
     * @param string $output
100
     * @return array
101
     */
102
    public static function parseList($output)
103
    {
104
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $output));
105
        $rawConfig = array_map(function ($line) {
106
            $matches = array();
107
            preg_match('/^\[(.*?)\]\s?(.*)$/', $line, $matches);
108
109
            return $matches;
110
        }, $rowArray);
111
112
        $rawConfig = array_column($rawConfig, 2, 1);
113
114
        $result = array();
115
116
        foreach ($rawConfig as $path => $value) {
117
            $arr = &$result;
118
            $keys = explode('.', $path);
119
            foreach ($keys as $key) {
120
                $arr = &$arr[$key];
121
            }
122
            $arr = $value;
123
        }
124
125
        return $result;
126
    }
127
128
    /**
129
     * @param $rowArray
130
     * @param $key
131
     * @return array
132
     */
133
    private static function parseArrayInfoOutput($rowArray, $key)
134
    {
135
        $result = array();
136
        $start = false;
137
        foreach ($rowArray as $line) {
138
            if ($line === $key) {
139
                $start = true;
140
                continue;
141
            }
142
            if ($start) {
143
                if (empty($line)) {
144
                    break;
145
                }
146
                $parts = explode(' ', $line);
147
                $result[$parts[0]] = $parts[1];
148
            }
149
        }
150
151
        return $result;
152
    }
153
}
154