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 — master ( 73245c...f6f85c )
by w3l
01:33
created

Convert::hexrgb()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
namespace w3l\Holt45;
3
trait Convert {
4
5
	/**
6
	 * Converts red-green-blue(RGB) to hexadecimal
7
	 *
8
	 * @param array $rgb RGB color
9
	 * @return string Hexadecimal color
10
	 */
11
	public static function rgbhex($rgb) {
12
		$hex = "";
13
		foreach($rgb AS $color) {
14
			$hex .= str_pad(dechex($color), 2, "0", STR_PAD_LEFT);
15
		}
16
		return $hex;
17
	}
18
19
	/**
20
	 * Converts hexadecimal to red-green-blue(RGB)
21
	 *
22
	 * @param string $hex Hexadecimal color
23
	 * @return null|int[] RGB color
0 ignored issues
show
Documentation introduced by
Should the return type not be array<integer|double>|null?

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...
24
	 */
25
	public static function hexrgb($hex) {
26
		$hex = preg_replace("/[^0-9A-Fa-f]/", '', $hex);
27
28
		$strlenHex = strlen($hex);
29
30
		if($strlenHex >= 3) {
31
			if($strlenHex >= 6) {
32
				if($strlenHex > 6) {
33
					$hex = substr($hex,0,6);
34
				}
35
				$hexArray = str_split($hex,2);
36
			} elseif($strlenHex < 6) {
37
				$hexArray = array("$hex[0]$hex[0]", "$hex[1]$hex[1]", "$hex[2]$hex[2]");
38
			}
39
			return array(hexdec($hexArray[0]), hexdec($hexArray[1]), hexdec($hexArray[2]));
0 ignored issues
show
Bug introduced by
The variable $hexArray 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...
40
		}
41
42
		return NULL;
43
	}
44
	/**
45
	 * Converts hexadecimal to red-green-blue(RGB)
46
	 *
47
	 * @param array $arrayRGB RGB color
48
	 * @param array $arrayRGB2 RGB color
49
	 * @return int[] Blended RGB color
50
	 */
51
	public static function colorBlend($arrayRGB,$arrayRGB2) {
52
53
		$arrayBlend = array();
54
55
		for($i = 0, $size = count($arrayRGB); $i < $size; $i++) {
56
			$arrayBlend[] = round(($arrayRGB[$i]+$arrayRGB2[$i])/2);
57
		}
58
59
		return $arrayBlend;
60
	}
61
62
}
63