Completed
Push — master ( 6e487d...af4d7b )
by Angus
03:37
created

generic_helper.php ➔ http_parse_headers()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') or exit('No direct script access allowed');
2
3
/**
4
 * Checks if view file exists.
5
 * @param string $path Location of view file. Use without /views/ & .php
6
 * @return bool
7
 */
8
function view_exists(string $path) : bool {
9 23
	return file_exists(APPPATH . "/views/{$path}.php");
10
}
11
12
function get_time_class(string $time_string) : string {
13 4
	$time = strtotime($time_string);
14
15 4
	if(is_int($time)) {
16 3
		if($time < strtotime('-1 month')) {
17
			//More than a month old.
18 1
			$time_string = "sprite-month";
19 2
		} elseif($time < strtotime('-1 week')) {
20
			//More than a week old, but less than a month old.
21 1
			$time_string = "sprite-week";
22
		} else {
23
			//Less than a week old.
24 3
			$time_string = "sprite-day";
25
		}
26
	} else {
27 1
		$time_string = "sprite-error"; //TODO: Create the sprite for this
28
	}
29 4
	return $time_string;
30
}
31
32
if (!function_exists('http_parse_headers')) { #http://www.php.net/manual/en/function.http-parse-headers.php#112917
33
	function http_parse_headers ($raw_headers){
34 12
		$headers = array(); // $headers = [];
35 12
		foreach (explode("\n", $raw_headers) as $i => $h) {
36 12
			$h = explode(':', $h, 2);
37 12
			if (isset($h[1])){
38 12
				if(!isset($headers[$h[0]])){
39 12
					$headers[$h[0]] = trim($h[1]);
40 8
				}else if(is_array($headers[$h[0]])){
41 2
					$tmp = array_merge($headers[$h[0]],array(trim($h[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
42 2
					$headers[$h[0]] = $tmp;
43
				}else{
44 8
					$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
45 12
					$headers[$h[0]] = $tmp;
46
				}
47
			}
48
		}
49 12
		return $headers;
50
	}
51
}
52