Passed
Pull Request — develop (#269)
by
unknown
07:49
created

Utils::getContentFromUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 8
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10

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
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Generator;
6
7
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
8
9
final class Utils
10
{
11
    /**
12
     * Gets upper case word among a string from the end or from the beginning part.
13
     */
14 170
    public static function getPart(string $optionValue, string $string): string
15
    {
16 170
        $string = str_replace('_', '', $string);
17 170
        $string = preg_replace('/([0-9])/', '', $string);
18
19 170
        if (empty($string)) {
20 2
            return '';
21
        }
22
23 170
        $elementType = '';
24
25
        switch ($optionValue) {
26 170
            case GeneratorOptions::VALUE_END:
27 8
                $parts = preg_split('/[A-Z]/', ucfirst($string));
28 8
                $partsCount = count($parts);
29 8
                if (!empty($parts[$partsCount - 1])) {
30 6
                    $elementType = mb_substr($string, mb_strrpos($string, implode('', array_slice($parts, -1))) - 1);
31
                } else {
32 2
                    for ($i = $partsCount - 1; $i >= 0; --$i) {
33 2
                        $part = trim($parts[$i]);
34 2
                        if (!empty($part)) {
35
                            break;
36
                        }
37
                    }
38 2
                    $elementType = mb_substr($string, ((count($parts) - 2 - $i) + 1) * -1);
39
                }
40
41 8
                break;
42
43 162
            case GeneratorOptions::VALUE_START:
44 154
                $parts = preg_split('/[A-Z]/', ucfirst($string));
45 154
                $partsCount = count($parts);
46 154
                if (empty($parts[0]) && !empty($parts[1])) {
47 154
                    $elementType = mb_substr($string, 0, mb_strlen($parts[1]) + 1);
48
                } else {
49 6
                    for ($i = 0; $i < $partsCount; ++$i) {
50 6
                        $part = trim($parts[$i]);
51 6
                        if (!empty($part)) {
52 6
                            break;
53
                        }
54
                    }
55 6
                    $elementType = mb_substr($string, 0, $i);
56
                }
57
58 154
                break;
59
60 8
            case GeneratorOptions::VALUE_NONE:
61 8
                $elementType = $string;
62
63 8
                break;
64
        }
65
66 170
        return $elementType;
67
    }
68
69 4
    public static function getContentFromUrl(string $url, ?string $basicAuthLogin = null, ?string $basicAuthPassword = null, ?string $proxyHost = null, $proxyPort = null, ?string $proxyLogin = null, ?string $proxyPassword = null, array $contextOptions = []): string
70
    {
71 4
        $context = null;
72 4
        $options = self::getStreamContextOptions($basicAuthLogin, $basicAuthPassword, $proxyHost, $proxyPort, $proxyLogin, $proxyPassword, $contextOptions);
73 4
        if (!empty($options)) {
74 4
            $context = stream_context_create($options);
75
        }
76
77 4
        return file_get_contents($url, false, $context);
78
    }
79
80 12
    public static function getStreamContextOptions(?string $basicAuthLogin = null, ?string $basicAuthPassword = null, ?string $proxyHost = null, $proxyPort = null, ?string $proxyLogin = null, ?string $proxyPassword = null, array $contextOptions = []): array
81
    {
82 12
        $applyHttpHeader = function (array $contextOptions, string $header): array {
83 12
            if (!isset($contextOptions['http']['header']) || !in_array($header, $contextOptions['http']['header'])) {
84 3
                $contextOptions['http']['header'][] = $header;
85
            }
86
87 6
            return $contextOptions;
88
        };
89
90
        if (!empty($basicAuthLogin) && !empty($basicAuthPassword)) {
91
            $authorizationHeader = sprintf('Authorization: Basic %s', base64_encode(sprintf('%s:%s', $basicAuthLogin, $basicAuthPassword)));
92
            $contextOptions = $applyHttpHeader($contextOptions, $authorizationHeader);
93 12
        }
94 3
95
        if (!empty($proxyHost)) {
96 6
            $contextOptions['http']['proxy'] = sprintf('tcp://%s%s', $proxyHost, empty($proxyPort) ? '' : sprintf(':%s', $proxyPort));
97
            $proxyAuthorizationHeader = sprintf('Proxy-Authorization: Basic %s', base64_encode(sprintf('%s:%s', $proxyLogin, $proxyPassword)));
98 6
            $contextOptions = $applyHttpHeader($contextOptions, $proxyAuthorizationHeader);
99
        }
100
101
        return $contextOptions;
102
    }
103
104 12
    public static function getValueWithinItsType($value, ?string $knownType = null)
105
    {
106
        if (is_int($value) || (!is_null($value) && in_array($knownType, [
107 106
            'time',
108
            'positiveInteger',
109 106
            'unsignedLong',
110
            'unsignedInt',
111
            'short',
112
            'long',
113
            'int',
114
            'integer',
115
        ], true))) {
116
            return intval($value);
117
        }
118
        if (is_float($value) || (!is_null($value) && in_array($knownType, [
119 6
            'float',
120
            'double',
121 106
            'decimal',
122
        ], true))) {
123
            return floatval($value);
124
        }
125
        if (is_bool($value) || (!is_null($value) && in_array($knownType, [
126 2
            'bool',
127
            'boolean',
128 106
        ], true))) {
129
            return 'true' === $value || true === $value || 1 === $value || '1' === $value;
130
        }
131
132 6
        return $value;
133
    }
134
135 102
    public static function resolveCompletePath(string $origin, string $destination): string
136
    {
137
        $resolvedPath = $destination;
138 70
        if (!empty($destination) && false === mb_strpos($destination, 'http://') && false === mb_strpos($destination, 'https://') && !empty($origin)) {
139
            if ('./' === mb_substr($destination, 0, 2)) {
140 70
                $destination = mb_substr($destination, 2);
141 70
            }
142 70
            $destinationParts = explode('/', $destination);
143 4
            $fileParts = pathinfo($origin);
144
            $fileBasename = (is_array($fileParts) && array_key_exists('basename', $fileParts)) ? $fileParts['basename'] : '';
145 70
            $parts = parse_url(str_replace('/'.$fileBasename, '', $origin));
146 70
            $scheme = (is_array($parts) && array_key_exists('scheme', $parts)) ? $parts['scheme'] : '';
147 70
            $host = (is_array($parts) && array_key_exists('host', $parts)) ? $parts['host'] : '';
148 70
            $path = (is_array($parts) && array_key_exists('path', $parts)) ? $parts['path'] : '';
149 70
            $path = str_replace('/'.$fileBasename, '', $path);
150 70
            $pathParts = explode('/', $path);
151 70
            $finalPath = implode('/', $pathParts);
152 70
            foreach ($destinationParts as $locationPart) {
153 70
                if ('..' === $locationPart) {
154 70
                    $finalPath = mb_substr($finalPath, 0, mb_strrpos($finalPath, '/', 0));
155 70
                } else {
156 70
                    $finalPath .= '/'.$locationPart;
157 4
                }
158
            }
159 70
            $port = (is_array($parts) && array_key_exists('port', $parts)) ? $parts['port'] : '';
160
            // Remote file
161
            if (!empty($scheme) && !empty($host)) {
162 70
                $resolvedPath = str_replace('urn', 'http', $scheme).'://'.$host.(!empty($port) ? ':'.$port : '').str_replace('//', '/', $finalPath);
163
            } elseif (empty($scheme) && empty($host) && count($pathParts)) {
164 70
                // Local file
165 2
                if (is_file($finalPath)) {
166 68
                    $resolvedPath = $finalPath;
167
                }
168 68
            }
169 68
        }
170
171
        return $resolvedPath;
172
    }
173
174 70
    public static function cleanComment($comment, string $glueSeparator = ',', bool $uniqueValues = true): string
175
    {
176
        if (!is_scalar($comment) && !is_array($comment)) {
177 120
            return '';
178
        }
179 120
180 2
        return trim(str_replace('*/', '*[:slash:]', is_scalar($comment) ? (string) $comment : implode($glueSeparator, $uniqueValues ? array_unique($comment) : $comment)));
181
    }
182
183 118
    /**
184
     * Clean a string to make it valid as PHP variable
185
     * See more about the used regular expression at {@link http://www.regular-expressions.info/unicode.html}:
186
     * - \p{L} for any valid letter
187
     * - \p{N} for any valid number
188
     * - /u for supporting unicode.
189
     *
190
     * @param string $string                  the string to clean
191
     * @param bool   $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores
192
     */
193
    public static function cleanString(string $string, bool $keepMultipleUnderscores = true): string
194
    {
195
        $cleanedString = preg_replace('/[^\p{L}\p{N}_]/u', '_', $string);
196 420
        if (!$keepMultipleUnderscores) {
197
            $cleanedString = preg_replace('/[_]+/', '_', $cleanedString);
198 420
        }
199 420
200 118
        return $cleanedString;
201
    }
202
203 420
    public static function removeNamespace(string $namespacedClassName): string
204
    {
205
        $elements = explode('\\', $namespacedClassName);
206 146
207
        return (string) array_pop($elements);
208 146
    }
209
210 146
    public static function createDirectory(string $directory, $permissions = 0775): bool
211
    {
212
        if (!is_dir($directory)) {
213 170
            mkdir($directory, $permissions, true);
214
        }
215 170
216 10
        return true;
217
    }
218
219 170
    /**
220
     * Save schemas to schemasFolder
221
     * Filename will be extracted from schemasUrl or default schema.wsdl will be used.
222
     */
223
    public static function saveSchemas(string $destinationFolder, string $schemasFolder, string $schemasUrl, string $content): string
224
    {
225
        if ((is_null($schemasFolder)) || empty($schemasFolder)) {
226 4
            // if null or empty schemas folder was provided
227
            // default schemas folder will be wsdl
228 4
            $schemasFolder = 'wsdl';
229
        }
230
        $schemasPath = rtrim($destinationFolder, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.rtrim($schemasFolder, DIRECTORY_SEPARATOR);
231 2
232
        // Here we must cover all possible variants
233 4
        if ((false !== mb_strpos(mb_strtolower($schemasUrl), '.wsdl')) || (false !== mb_strpos(mb_strtolower($schemasUrl), '.xsd')) || (false !== mb_strpos(mb_strtolower($schemasUrl), '.xml'))) {
234
            $filename = basename($schemasUrl);
235
        } else {
236 4
            // if $url is like http://example.com/index.php?WSDL default filename will be schema.wsdl
237 4
            $filename = 'schema.wsdl';
238
        }
239
240 2
        self::createDirectory($schemasPath);
241
242
        file_put_contents($schemasPath.DIRECTORY_SEPARATOR.$filename, $content);
243 4
244
        return $schemasPath.DIRECTORY_SEPARATOR.$filename;
245 4
    }
246
}
247