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
Push — dev ( 239737...f0805b )
by w3l
02:12
created

Misc   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 166
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B get_client_ip_address() 0 17 7
A auto_correct_parse_url() 0 16 4
C url_parser() 0 50 8
B generate_password() 0 24 5
A textarea_encode() 0 3 1
A textarea_decode() 0 3 1
1
<?php
2
namespace w3l\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
	 * Tries to auto-correct parse_url()-output.
29
	 *
30
	 * @param string $url
31
	 * @return array
32
	 */
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>|false?

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...
33
	
34
	private static function auto_correct_parse_url($url) {
35
		// multiple /// messes up parse_url, replace 3 or more with 2
36
		$url = preg_replace('/(\/{2,})/','//',$url);
37
		
38
		$parse_url = parse_url($url);
39
		
40
		if(empty($parse_url["scheme"])) {
41
			$parse_url["scheme"] = "http";
42
		}
43
		if(empty($parse_url["host"]) && !empty($parse_url["path"])) {
44
			// Strip slash from the beginning of path
45
			$parse_url["host"] = ltrim($parse_url["path"], '\/');
46
			$parse_url["path"] = "";
47
		}
48
		return $parse_url;
49
	}
50
51
	/**
52
	 * parse url, try to correct errors and return valid url + display-url.
53
	 *
54
	 * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html
55
	 * @example gopher:/ww.example.com => gopher://www.example.com
56
	 * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd
57
	 * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/
58
	 * @example .example.com/ => http://example.com/
59
	 * @example example.com =>http://example.com
60
	 * @example subdomain.example.com => http://subdomain.example.com
61
	 *
62
	 * @param string $url Any somewhat valid url.
63
	 * @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...
64
	 */
65
	public static function url_parser($url) {
66
		
67
		$parse_url = self::auto_correct_parse_url($url);
68
69
		$url_array = array("url" => "", "url_display" => "");
70
		
71
		// Check if scheme is correct
72
		if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) {
73
			$url_array["url"] .= 'http'.'://';
74
		} else {
75
			$url_array["url"] .= $parse_url["scheme"].'://';
76
		}
77
		
78
		// Check if the right amount of "www" is set.
79
		$explode_host = explode(".", $parse_url["host"]);
80
		
81
		// Remove empty entries
82
		$explode_host = array_filter($explode_host);
83
		// And reassign indexes
84
		$explode_host = array_values($explode_host);
85
		
86
		// Contains subdomain
87
		if(count($explode_host) > 2) {
88
			// Check if subdomain only contains the letter w(then not any other subdomain).
89
			if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) {
90
				// Replace with "www" to avoid "ww" or "wwww", etc.
91
				$explode_host[0] = "www";
92
				
93
			}
94
		}
95
96
		$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...
97
		$url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash
98
		
99
		if(!empty($parse_url["port"])) {
100
			$url_array["url"] .= ":".$parse_url["port"];
101
		}
102
		if(!empty($parse_url["path"])) {
103
			$url_array["url"] .= $parse_url["path"];
104
		}
105
		if(!empty($parse_url["query"])) {
106
			$url_array["url"] .= '?'.$parse_url["query"];
107
		}
108
		if(!empty($parse_url["fragment"])) {
109
			$url_array["url"] .= '#'.$parse_url["fragment"];
110
		}
111
112
		
113
		return $url_array;
114
	}
115
	
116
	/**
117
	 * Generate a password-suggestion.
118
	 *
119
	 * @param int $length Length of password
120
	 * @param bool $simple Limit character-set to first 33 characters.
121
	 * @return string
122
	 */
123
	public static function generate_password($length = 8, $simple = false) {
124
		$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...
125
		$character_set_lenght = (($simple) ? 33 : 64);
126
		
127
		$counter = 0;
128
		
129
		while($counter < 10) {
130
		
131
			$suggested_password = "";
132
			
133
			for($i = 0; $i < $length; $i++) {
134
				$suggested_password .= $character_set[rand(0,($character_set_lenght-1))];
135
			}
136
137
			if(strlen(count_chars($suggested_password, 3)) > ($length-2)) {
138
				break;
139
			}
140
			
141
			$counter++;
142
		}
143
		
144
		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...
145
		
146
	}
147
	
148
	/**
149
	 * Convert <textarea> to [textarea].
150
	 *
151
	 * @param string $html
152
	 * @return string
153
	 */
154
	public static function textarea_encode($html) {
155
		return preg_replace("/<textarea(.*?)>(.*?)<\/textarea>/is", "[textarea$1]$2[/textarea]", $html);
156
	}
157
	
158
	/**
159
	 * Convert [textarea] to <textarea>.
160
	 *
161
	 * @param string $html
162
	 * @return string
163
	 */
164
	public static function textarea_decode($html) {
165
		return preg_replace("/\[textarea(.*?)\](.*?)\[\/textarea\]/is", "<textarea$1>$2</textarea>", $html);
166
	}
167
	
168
}