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