LinearHorizontalPlacer::getFirstPlaceToTry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud\Placer;
4
5
use Imagine\Image\Point;
6
use Imagine\Image\PointInterface;
7
8
class LinearHorizontalPlacer extends AbstractPlacer
9
{
10
    /** @var int */
11
    protected $increment;
12
13
    /**
14
     * @param int $imgWidth
15
     * @param int $imgHeight
16
     * @param int $increment
17
     */
18 1
    public function __construct($imgWidth, $imgHeight, $increment = 10)
19
    {
20 1
        parent::__construct($imgWidth, $imgHeight);
21 1
        $this->increment = $increment;
22 1
    }
23
24
    /**
25
     * @param PointInterface $current
26
     * @return bool|PointInterface
27
     */
28 1
    public function getNextPlaceToTry(PointInterface $current)
29
    {
30 1
        if ($current->getX() < $this->imgWidth) {
31 1
            return new Point($current->getX() + $this->increment, $current->getY());
32
        }
33
34 1
        if ($current->getY() < $this->imgHeight) {
35 1
            return new Point(0, $current->getY() + $this->increment);
36
        }
37
38
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface SixtyNine\Cloud\Placer\P...face::getNextPlaceToTry of type Imagine\Image\PointInterface.

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...
39
    }
40
41
    /**
42
     * @return PointInterface
43
     */
44 1
    function getFirstPlaceToTry()
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...
45
    {
46 1
        return new Point(0, 0);
47
    }
48
49
}
50