Test Setup Failed
Push — master ( b9c0be...6a3b1f )
by Francimar
06:58
created

Threshold::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.568
c 0
b 0
f 0
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 Threshold 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 \resource $image The source image resource
14
     * @return \resource The black and white image resource
15
     */
16 2
    public function process($image)
17
    {
18 2
        $width = imagesx($image);
19 2
        $height = imagesy($image);
20 2
        $new_image = imagecreatetruecolor($width, $height);
21
        // sets background to black
22 2
        imagecolorallocate($new_image, 0, 0, 0);
23 2
        $white = imagecolorallocate($new_image, 255, 255, 255);
24 2
        for ($y = 0; $y < $height; $y++) {
25 2
            for ($x = 0; $x < $width; $x++) {
26 2
                $color = imagecolorat($image, $x, $y);
27 2
                $red = ($color >> 16) & 0xFF;
28 2
                $green = ($color >> 8) & 0xFF;
29 2
                $blue = $color & 0xFF;
30 2
                $gray = (int)($red * 0.3 + $green * 0.59 + $blue * 0.11);
31 2
                if ($gray > 127) {
32 2
                    imagesetpixel($new_image, $x, $y, $white);
33
                }
34
            }
35
        }
36 2
        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 resource.

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...
37
    }
38
}
39