Completed
Push — master ( 87b7a4...6ce28d )
by Gaetano
11:11 queued 06:38
created

argParser::getArgs()   F

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
nc 73728
nop 0
dl 0
loc 102
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Common parameter parsing for benchmark and tests scripts.
5
 *
6
 * @param integer DEBUG
7
 * @param string  LOCALSERVER
8
 * @param string  URI
9
 * @param string  HTTPSSERVER
10
 * @param string  HTTPSSURI
11
 * @param string  PROXY
12
 * @param string  NOPROXY
13
 * @param bool    HTTPSIGNOREPEER
14
 * @param int     HTTPSVERIFYHOST
15
 * @param int     SSLVERSION
16
 *
17
 * @copyright (C) 2007-2019 G. Giunta
18
 * @license code licensed under the BSD License: see file license.txt
19
 **/
20
class argParser
21
{
22
    public static function getArgs()
23
    {
24
        global $argv;
25
26
        $args = array(
27
            'DEBUG' => 0,
28
            'LOCALSERVER' => 'localhost',
29
            'HTTPSSERVER' => 'gggeek.ssl.altervista.org',
30
            'HTTPSURI' => '/sw/xmlrpc/demo/server/server.php',
31
            'HTTPSIGNOREPEER' => false,
32
            'HTTPSVERIFYHOST' => 2,
33
            'SSLVERSION' => 0,
34
            'PROXYSERVER' => null,
35
            'NOPROXY' => false,
36
            'LOCALPATH' => __DIR__,
37
        );
38
39
        // check for command line vs web page input params
40
        if (!isset($_SERVER['REQUEST_METHOD'])) {
41
            if (isset($argv)) {
42
                foreach ($argv as $param) {
43
                    $param = explode('=', $param);
44
                    if (count($param) > 1) {
45
                        $pname = strtoupper(ltrim($param[0], '-'));
46
                        $$pname = $param[1];
47
                    }
48
                }
49
            }
50
        } else {
51
            // NB: we might as well consider using $_GET stuff later on...
52
            extract($_GET);
53
            extract($_POST);
54
        }
55
56
        if (isset($DEBUG)) {
57
            $args['DEBUG'] = intval($DEBUG);
58
        }
59
        if (isset($LOCALSERVER)) {
60
            $args['LOCALSERVER'] = $LOCALSERVER;
61
        } else {
62
            if (isset($HTTP_HOST)) {
63
                $args['LOCALSERVER'] = $HTTP_HOST;
64
            } elseif (isset($_SERVER['HTTP_HOST'])) {
65
                $args['LOCALSERVER'] = $_SERVER['HTTP_HOST'];
66
            }
67
        }
68
        if (isset($HTTPSSERVER)) {
69
            $args['HTTPSSERVER'] = $HTTPSSERVER;
70
        }
71
        if (isset($HTTPSURI)) {
72
            $args['HTTPSURI'] = $HTTPSURI;
73
        }
74
        if (isset($HTTPSIGNOREPEER)) {
75
            $args['HTTPSIGNOREPEER'] = (bool)$HTTPSIGNOREPEER;
76
        }
77
        if (isset($HTTPSVERIFYHOST)) {
78
            $args['HTTPSVERIFYHOST'] = (int)$HTTPSVERIFYHOST;
79
        }
80
        if (isset($SSLVERSION)) {
81
            $args['SSLVERSION'] = (int)$SSLVERSION;
82
        }
83
        if (isset($PROXY)) {
84
            $arr = explode(':', $PROXY);
85
            $args['PROXYSERVER'] = $arr[0];
86
            if (count($arr) > 1) {
87
                $args['PROXYPORT'] = $arr[1];
88
            } else {
89
                $args['PROXYPORT'] = 8080;
90
            }
91
        }
92
        // used to silence testsuite warnings about proxy code not being tested
93
        if (isset($NOPROXY)) {
94
            $args['NOPROXY'] = true;
95
        }
96
        if (!isset($URI)) {
97
            // GUESTIMATE the url of local demo server
98
            // play nice to php 3 and 4-5 in retrieving URL of server.php
99
            /// @todo filter out query string from REQUEST_URI
100
            if (isset($REQUEST_URI)) {
101
                $URI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $REQUEST_URI);
102
                $URI = str_replace('/testsuite.php', '/server.php', $URI);
103
                $URI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $URI);
104
                $URI = str_replace('/benchmark.php', '/server.php', $URI);
105
            } elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['REQUEST_METHOD'])) {
106
                $URI = str_replace('/tests/testsuite.php', '/demo/server/server.php', $_SERVER['PHP_SELF']);
107
                $URI = str_replace('/testsuite.php', '/server.php', $URI);
108
                $URI = str_replace('/tests/benchmark.php', '/demo/server/server.php', $URI);
109
                $URI = str_replace('/benchmark.php', '/server.php', $URI);
110
            } else {
111
                $URI = '/demo/server/server.php';
112
            }
113
        }
114
        if ($URI[0] != '/') {
115
            $URI = '/' . $URI;
116
        }
117
        $args['URI'] = $URI;
118
        if (isset($LOCALPATH)) {
119
            $args['LOCALPATH'] = $LOCALPATH;
120
        }
121
122
        return $args;
123
    }
124
}
125