Completed
Push — master ( 49c7a9...f71cbe )
by Dan
02:30
created

LinearHorizontalPlacer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 30
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNextPlaceToTry() 0 12 3
A getFirstPlaceToTry() 0 4 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 1
    public function __construct($imgWidth, $imgHeight, $increment = 10)
14
    {
15 1
        parent::__construct($imgWidth, $imgHeight);
16 1
        $this->increment = $increment;
17 1
    }
18
19 1
    public function getNextPlaceToTry(PointInterface $current)
20
    {
21 1
        if ($current->getX() < $this->imgWidth) {
22 1
            return new Point($current->getX() + $this->increment, $current->getY());
23
        }
24
25
        if ($current->getY() < $this->imgHeight) {
26
            return new Point(0, $current->getY() + $this->$increment);
0 ignored issues
show
Bug introduced by
The variable $increment does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
27
        }
28
29
        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...
30
    }
31
32 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...
33
    {
34 1
        return new Point(0, 0);
35
    }
36
37
}
38