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