GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (9f36d7)
by w3l
02:08
created

Misc::url_parser()   D

Complexity

Conditions 11
Paths 384

Size

Total Lines 62
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 62
c 1
b 0
f 0
rs 4.5652
cc 11
eloc 30
nc 384
nop 1

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
namespace Holt45;
3
trait Misc {
4
	/**
5
	 * Get client ip-address
6
	 *
7
	 * @return string User ip-address
8
	 */
9
	public static function get_client_ip_address() {
10
11
		if (getenv('HTTP_CLIENT_IP'))
12
			return getenv('HTTP_CLIENT_IP');
13
		else if(getenv('HTTP_X_FORWARDED_FOR'))
14
			return getenv('HTTP_X_FORWARDED_FOR');
15
		else if(getenv('HTTP_X_FORWARDED'))
16
			return getenv('HTTP_X_FORWARDED');
17
		else if(getenv('HTTP_FORWARDED_FOR'))
18
			return getenv('HTTP_FORWARDED_FOR');
19
		else if(getenv('HTTP_FORWARDED'))
20
			return getenv('HTTP_FORWARDED');
21
		else if(getenv('REMOTE_ADDR'))
22
			return getenv('REMOTE_ADDR');
23
		else
24
			return '127.0.0.1'; // Unknown IP
25
	}
26
27
	/**
28
	 * parse url, try to correct errors and return valid url + display-url.
29
	 *
30
	 * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html
31
	 * @example gopher:/ww.example.com => gopher://www.example.com
32
	 * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd
33
	 * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/
34
	 * @example .example.com/ => http://example.com/
35
	 * @example example.com =>http://example.com
36
	 * @example subdomain.example.com => http://subdomain.example.com
37
	 *
38
	 * @param string $url Any somewhat valid url.
39
	 * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
	 */
41
	public static function url_parser($url) {
42
		
43
		// multiple /// messes up parse_url, replace 3 or more with 2
44
		$url = preg_replace('/(\/{2,})/','//',$url);
45
		
46
		$parse_url = parse_url($url);
47
		
48
		if(empty($parse_url["scheme"])) {
49
			$parse_url["scheme"] = "http";
50
		}
51
		if(empty($parse_url["host"]) && !empty($parse_url["path"])) {
52
			// Strip slash from the beginning of path
53
			$parse_url["host"] = ltrim($parse_url["path"], '\/');
54
			$parse_url["path"] = "";
55
		}
56
57
		$url_array = array("url" => "", "url_display" => "");
58
		
59
		// Check if scheme is correct
60
		if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) {
61
			$url_array["url"] .= 'http'.'://';
62
		} else {
63
			$url_array["url"] .= $parse_url["scheme"].'://';
64
		}
65
		
66
		// Check if the right amount of "www" is set.
67
		$explode_host = explode(".", $parse_url["host"]);
68
		
69
		// Remove empty entries
70
		$explode_host = array_filter($explode_host);
71
		// And reassign indexes
72
		$explode_host = array_values($explode_host);
73
		
74
		// Contains subdomain
75
		if(count($explode_host) > 2) {
76
			// Check if subdomain only contains the letter w(then not any other subdomain).
77
			if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) {
78
				// Replace with "www" to avoid "ww" or "wwww", etc.
79
				$explode_host[0] = "www";
80
				
81
			}
82
		}
83
84
		$url_array["url"] .= implode(".",$explode_host);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
85
		$url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash
86
		
87
		if(!empty($parse_url["port"])) {
88
			$url_array["url"] .= ":".$parse_url["port"];
89
		}
90
		if(!empty($parse_url["path"])) {
91
			$url_array["url"] .= $parse_url["path"];
92
		}
93
		if(!empty($parse_url["query"])) {
94
			$url_array["url"] .= '?'.$parse_url["query"];
95
		}
96
		if(!empty($parse_url["fragment"])) {
97
			$url_array["url"] .= '#'.$parse_url["fragment"];
98
		}
99
100
		
101
		return $url_array;
102
	}
103
	
104
	/**
105
	 * Generate a password-suggestion.
106
	 *
107
	 * @param int $length Length of password
108
	 * @param bool $simple Limit character-set to first 33 characters.
109
	 * @return string
110
	 */
111
	public static function generate_password($length = 8, $simple = false) {
112
		$character_set = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ!#%+:=?@";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
113
		$character_set_lenght = (($simple) ? 33 : 64);
114
		
115
		$i = 0;
116
		
117
		while($i < 10) {
118
		
119
			$suggested_password = "";
120
			
121
			for($i = 0; $i < $length; $i++) {
122
				$suggested_password .= $character_set[rand(0,($character_set_lenght-1))];
123
			}
124
125
			if(strlen(count_chars($suggested_password, 3)) > ($length-2)) {
126
				break;
127
			}
128
		}
129
		
130
		return $suggested_password;
0 ignored issues
show
Bug introduced by
The variable $suggested_password does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
131
		
132
	}
133
	
134
	/**
135
	 * Convert <textarea> to [textarea].
136
	 *
137
	 * @param string $html
138
	 * @return string
139
	 */
140
	public static function textarea_encode($html) {
141
		return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html);
142
	}
143
	
144
	/**
145
	 * Convert [textarea] to <textarea>.
146
	 *
147
	 * @param string $html
148
	 * @return string
149
	 */
150
	public static function textarea_decode($html) {
151
		return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html);
152
	}
153
	
154
}