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

FloydSteinberg::process()   C

Complexity

Conditions 13
Paths 230

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
ccs 33
cts 33
cp 1
rs 5.4083
cc 13
nc 230
nop 1
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Thermal\Graphics\Filter;
4
5
use Thermal\Graphics\Filter;
6
7
class FloydSteinberg 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 3
    public function process($image)
0 ignored issues
show
Complexity introduced by
This operation has 8642 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
17
    {
18 3
        $width = imagesx($image);
19 3
        $height = imagesy($image);
20 3
        $new_image = imagecreatetruecolor($width, $height);
21
        // sets background to black
22 3
        $black = imagecolorallocate($new_image, 0, 0, 0);
23 3
        $white = imagecolorallocate($new_image, 255, 255, 255);
24 3
        $pixel = [];
25 3
        for ($y = 0; $y < $height; $y++) {
26 3
            for ($x = 0; $x < $width; $x++) {
27 3
                $color = imagecolorat($image, $x, $y);
28 3
                $red = ($color >> 16) & 0xFF;
29 3
                $green = ($color >> 8) & 0xFF;
30 3
                $blue = $color & 0xFF;
31 3
                $gray = (int)($red * 0.3 + $green * 0.59 + $blue * 0.11);
32
                // Add errors to color if there are
33 3
                if (isset($pixel[$x][$y])) {
34 3
                    $gray += $pixel[$x][$y];
35
                }
36 3
                $new_color = $black;
37 3
                if ($gray > 127) {
38 3
                    $new_color = $white;
39
                }
40 3
                imagesetpixel($new_image, $x, $y, $new_color);
41 3
                $error = $gray - ($new_color & 0xFF);
42 3
                if ($x + 1 < $width) {
43 3
                    $pixel[$x + 1][$y] = (isset($pixel[$x + 1][$y]) ? $pixel[$x + 1][$y] : 0) + ($error * 7 >> 4);
44
                }
45
                // if we are in the last line
46 3
                if ($y == $height - 1) {
47 3
                    continue;
48
                }
49 3
                if ($x > 0) {
50 3
                    $prev = isset($pixel[$x - 1][$y + 1]) ? $pixel[$x - 1][$y + 1] : 0;
51 3
                    $pixel[$x - 1][$y + 1] = $prev + ($error * 3 >> 4);
52
                }
53 3
                $pixel[$x][$y + 1] = (isset($pixel[$x][$y + 1]) ? $pixel[$x][$y + 1] : 0) + ($error * 5 >> 4);
54 3
                if ($x < $width - 1) {
55 3
                    $prev = isset($pixel[$x + 1][$y + 1]) ? $pixel[$x + 1][$y + 1] : 0;
56 3
                    $pixel[$x + 1][$y + 1] = $prev + ($error * 1 >> 4);
57
                }
58
            }
59
        }
60 3
        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...
61
    }
62
}
63