Completed
Push — feature/issue-40 ( 3d69a6...1b01be )
by Mikaël
24:23
created

Utils::getContentFromUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Generator;
4
5
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
6
7
class Utils
8
{
9
    /**
10
     * Gets upper case word admong a string from the end or from the beginning part
11
     * @param string $optionValue
12
     * @param string $string the string from which we can extract the part
13
     * @return string
14
     */
15 384
    public static function getPart($optionValue, $string)
16
    {
17 384
        $elementType = '';
18 384
        $string = str_replace('_', '', $string);
19 384
        $string = preg_replace('/([0-9])/', '', $string);
20 384
        if (!empty($string)) {
21
            switch ($optionValue) {
22 384
                case GeneratorOptions::VALUE_END:
23 12
                    $parts = preg_split('/[A-Z]/', ucfirst($string));
24 12
                    $partsCount = count($parts);
25 12
                    if ($partsCount == 0) {
26
                        $elementType = $string;
27 12
                    } elseif (!empty($parts[$partsCount - 1])) {
28 12
                        $elementType = substr($string, strrpos($string, implode('', array_slice($parts, -1))) - 1);
29 9
                    } else {
30
                        for ($i = $partsCount - 1; $i >= 0; $i--) {
31
                            $part = trim($parts[$i]);
32
                            if (!empty($part)) {
33
                                break;
34
                            }
35
                        }
36
                        $elementType = substr($string, ((count($parts) - 2 - $i) + 1) * -1);
37
                    }
38 12
                    break;
39 279
                case GeneratorOptions::VALUE_START:
40 372
                    $parts = preg_split('/[A-Z]/', ucfirst($string));
41 372
                    $partsCount = count($parts);
42 372
                    if ($partsCount == 0) {
43
                        $elementType = $string;
44 372
                    } elseif (empty($parts[0]) && !empty($parts[1])) {
45 372
                        $elementType = substr($string, 0, strlen($parts[1]) + 1);
46 279
                    } else {
47 12
                        for ($i = 0; $i < $partsCount; $i++) {
48 12
                            $part = trim($parts[$i]);
49 12
                            if (!empty($part)) {
50 12
                                break;
51
                            }
52 9
                        }
53 12
                        $elementType = substr($string, 0, $i);
54
                    }
55 372
                    break;
56
                default:
57
                    break;
58
            }
59 288
        }
60 384
        return $elementType;
61
    }
62
    /**
63
     * Get content from url using a proxy or not
64
     * @param string $url
65
     * @param string $basicAuthLogin
66
     * @param string $basicAuthPassword
67
     * @param string $proxyHost
68
     * @param string $proxyPort
69
     * @param string $proxyLogin
70
     * @param string $proxyPassword
71
     * @param array $contextOptions
72
     * @return string
73
     */
74 8
    public static function getContentFromUrl($url, $basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array())
75
    {
76 8
        $context = null;
77 8
        $options = self::getContentFromUrlContextOptions($url, $basicAuthLogin, $basicAuthPassword, $proxyHost, $proxyPort, $proxyLogin, $proxyPassword, $contextOptions);
78 8
        if (!empty($options)) {
79 4
            $context = stream_context_create($options);
80 3
        }
81 8
        return file_get_contents($url, false, $context);
82
    }
83
    /**
84
     * @param string $url
85
     * @param string $basicAuthLogin
86
     * @param string $basicAuthPassword
87
     * @param string $proxyHost
88
     * @param string $proxyPort
89
     * @param string $proxyLogin
90
     * @param string $proxyPassword
91
     * @param array $contextOptions
92
     * @return string[]
93
     */
94 24
    public static function getContentFromUrlContextOptions($url, $basicAuthLogin = null, $basicAuthPassword = null, $proxyHost = null, $proxyPort = null, $proxyLogin = null, $proxyPassword = null, array $contextOptions = array())
95
    {
96 24
        $protocol = strpos($url, 'https://') !== false ? 'https' : 'http';
97 24
        $proxyOptions = $basicAuthOptions = array();
98 24
        if (!empty($basicAuthLogin) && !empty($basicAuthPassword)) {
99
            $basicAuthOptions = array(
100
                $protocol => array(
101
                    'header' => array(
102 12
                        sprintf('Authorization: Basic %s', base64_encode(sprintf('%s:%s', $basicAuthLogin, $basicAuthPassword))),
103 9
                    ),
104 9
                ),
105 9
            );
106 9
        }
107 24
        if (!empty($proxyHost)) {
108
            $proxyOptions = array(
109
                $protocol => array(
110 12
                    'proxy' => sprintf('tcp://%s%s',
111 9
                        $proxyHost,
112 12
                        empty($proxyPort) ? '' : sprintf(':%s', $proxyPort)
113 9
                    ),
114
                    'header' => array(
115 12
                        sprintf('Proxy-Authorization: Basic %s', base64_encode(sprintf('%s:%s', $proxyLogin, $proxyPassword))),
116 9
                    ),
117 9
                ),
118 9
            );
119 9
        }
120 24
        return array_merge_recursive($contextOptions, $proxyOptions, $basicAuthOptions);
121
    }
122
    /**
123
     * Returns the value with good type
124
     * @param mixed $value the value
125
     * @return mixed
126
     */
127 436
    public static function getValueWithinItsType($value, $knownType = null)
128
    {
129 436
        if (is_int($value) || (!is_null($value) && in_array($knownType, array('time', 'positiveInteger', 'unsignedLong', 'unsignedInt', 'short', 'long', 'int', 'integer'), true))) {
130 8
            return intval($value);
131 436
        } elseif (is_float($value) || (!is_null($value) && in_array($knownType, array('float', 'double', 'decimal'), true))) {
132
            return floatval($value);
133 436
        } elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array('bool', 'boolean'), true))) {
134 40
            return ($value === 'true' || $value === true || $value === 1 || $value === '1');
135
        }
136 432
        return $value;
137
    }
138
    /**
139
     * @param string $origin
140
     * @param string $destination
141
     * @return string
142
     */
143 128
    public static function resolveCompletePath($origin, $destination)
144
    {
145 128
        $resolvedPath = $destination;
146 128
        if (!empty($destination) && strpos($destination, 'http://') === false && strpos($destination, 'https://') === false && !empty($origin)) {
147 128
            if (substr($destination, 0, 2) === './') {
148 8
                $destination = substr($destination, 2);
149 6
            }
150 128
            $destinationParts = explode('/', $destination);
151
152 128
            $fileParts = pathinfo($origin);
153 128
            $fileBasename = (is_array($fileParts) && array_key_exists('basename', $fileParts)) ? $fileParts['basename'] : '';
154 128
            $parts = parse_url(str_replace('/' . $fileBasename, '', $origin));
155 128
            $scheme = (is_array($parts) && array_key_exists('scheme', $parts)) ? $parts['scheme'] : '';
156 128
            $host = (is_array($parts) && array_key_exists('host', $parts)) ? $parts['host'] : '';
157 128
            $path = (is_array($parts) && array_key_exists('path', $parts)) ? $parts['path'] : '';
158 128
            $path = str_replace('/' . $fileBasename, '', $path);
159 128
            $pathParts = explode('/', $path);
160 128
            $finalPath = implode('/', $pathParts);
161
162 128
            foreach ($destinationParts as $locationPart) {
163 128
                if ($locationPart == '..') {
164 8
                    $finalPath = substr($finalPath, 0, strrpos($finalPath, '/', 0));
165 6
                } else {
166 128
                    $finalPath .= '/' . $locationPart;
167
                }
168 96
            }
169
170 128
            $port = (is_array($parts) && array_key_exists('port', $parts)) ? $parts['port'] : '';
171
            /**
172
             * Remote file
173
             */
174 128
            if (!empty($scheme) && !empty($host)) {
175 4
                $resolvedPath = str_replace('urn', 'http', $scheme) . '://' . $host . (!empty($port) ? ':' . $port : '') . str_replace('//', '/', $finalPath);
176 127
            } elseif (empty($scheme) && empty($host) && count($pathParts)) {
177
                /**
178
                 * Local file
179
                 */
180 124
                if (is_file($finalPath)) {
181 124
                    $resolvedPath = $finalPath;
182 93
                }
183 93
            }
184 96
        }
185 128
        return $resolvedPath;
186
    }
187
    /**
188
     * Clean comment
189
     * @param string $comment the comment to clean
190
     * @param string $glueSeparator ths string to use when gathering values
191
     * @param bool $uniqueValues indicates if comment values must be unique or not
192
     * @return string
193
     */
194 96
    public static function cleanComment($comment, $glueSeparator = ',', $uniqueValues = true)
195
    {
196 96
        if (!is_scalar($comment) && !is_array($comment)) {
197 4
            return '';
198
        }
199 92
        return trim(str_replace('*/', '*[:slash:]', is_scalar($comment) ? $comment : implode($glueSeparator, $uniqueValues ? array_unique($comment) : $comment)));
200
    }
201
    /**
202
     * Clean a string to make it valid as PHP variable
203
     * @param string $string the string to clean
204
     * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores
205
     * @return string
206
     */
207 432
    public static function cleanString($string, $keepMultipleUnderscores = true)
208
    {
209 432
        $cleanedString = preg_replace('/[^a-zA-Z0-9_]/', '_', $string);
210 432
        if (!$keepMultipleUnderscores) {
211 48
            $cleanedString = preg_replace('/[_]+/', '_', $cleanedString);
212 36
        }
213 432
        return $cleanedString;
214
    }
215
    /**
216
     * @param string $namespacedClassName
217
     * @return string
218
     */
219 148
    public static function removeNamespace($namespacedClassName)
220
    {
221 148
        $elements = explode('\\', $namespacedClassName);
222 148
        return (string)array_pop($elements);
223
    }
224
    /**
225
     * @param string $directory
226
     * @param int $permissions
227
     * @return bool
228
     */
229 164
    public static function createDirectory($directory, $permissions = 0775)
230
    {
231 164
        if (!is_dir($directory)) {
232 44
            mkdir($directory, $permissions, true);
233 33
        }
234 164
        return true;
235
    }
236
}
237