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 ( 2aea1d...51a001 )
by w3l
03:08
created

Convert::colorBlend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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 array 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
	function hexrgb($hex) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 array Blended RGB color
50
	 */
51
	function colorBlend($arrayRGB,$arrayRGB2) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
53
		$arrayBlend = array();
54
55
		for($i = 0; $i < count($arrayRGB); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
56
			$arrayBlend[] = round(($arrayRGB[$i]+$arrayRGB2[$i])/2);
57
		}
58
59
		return $arrayBlend;
60
	}
61
62
}
63