Passed
Push — master ( 5aa4b6...5758c7 )
by Francimar
02:45
created

BayerOrdered::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
ccs 19
cts 19
cp 1
rs 9.44
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Thermal\Graphics\Filter;
4
5
use Thermal\Graphics\Filter;
6
7
class BayerOrdered implements Filter
8
{
9
    /**
10
     * Convert an image resource to black and white aplying dither.
11
     * The original image resource will not be changed, a new image resource will be created.
12
     *
13
     * @param ImageResource $image The source image resource
14
     * @return ImageResource The black and white image resource
15
     */
16 1
    public function process($image)
17
    {
18 1
        $width = imagesx($image);
19 1
        $height = imagesy($image);
20 1
        $new_image = imagecreatetruecolor($width, $height);
21
        // sets background to black
22 1
        $black = imagecolorallocate($new_image, 0, 0, 0);
0 ignored issues
show
Unused Code introduced by
$black is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23 1
        $white = imagecolorallocate($new_image, 255, 255, 255);
24
        $pattern = [
25 1
            [ 15, 195,  60, 240],
26
            [135,  75, 180, 120],
27
            [ 45, 225,  30, 210],
28
            [165, 105, 150,  90]
29
        ];
30 1
        for ($y = 0; $y < $height; $y++) {
31 1
            for ($x = 0; $x < $width; $x++) {
32 1
                $color = imagecolorat($image, $x, $y);
33 1
                $red = ($color >> 16) & 0xFF;
34 1
                $green = ($color >> 8) & 0xFF;
35 1
                $blue = $color & 0xFF;
36 1
                $gray = (int)($red * 0.3 + $green * 0.59 + $blue * 0.11);
37 1
                $threshold = $pattern[$y % 4][$x % 4];
38 1
                $gray = ($gray + $threshold) >> 1;
39 1
                if ($gray > 127) {
40 1
                    imagesetpixel($new_image, $x, $y, $white);
41
                }
42
            }
43
        }
44 1
        return $new_image;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $new_image; (resource) is incompatible with the return type declared by the interface Thermal\Graphics\Filter::process of type Thermal\Graphics\ImageResource.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
    }
46
}
47