Completed
Pull Request — master (#6)
by Rafael
09:32
created

Image   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 50
wmc 4
lcom 0
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assign() 0 13 2
A map() 0 12 2
1
<?php
2
3
namespace Iris\Mapping;
4
5
class Image extends Base
6
{
7
    /**
8
     * @var \Iris\Mapping\OrderCustomer
9
     */
10
    private $customerMapping;
0 ignored issues
show
Unused Code introduced by
The property $customerMapping is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    /**
13
     * @var \Iris\Mapping\OrderAddress
14
     */
15
    private $addressMapping;
0 ignored issues
show
Unused Code introduced by
The property $addressMapping is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
17
    /**
18
     * @var \Iris\Mapping\OrderItem
19
     */
20
    private $itemMapping;
0 ignored issues
show
Unused Code introduced by
The property $itemMapping is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function assign(array $externalData)
26
    {
27
        $images = [];
28
29
        foreach ($externalData as $image) {
30
            $images[] = new \Iris\Transfer\Catalog\Image([
31
                'url'      => $image['url'],
32
                'position' => $image['position']
33
            ]);
34
        }
35
36
         return $imageCollection = new \Iris\Transfer\Catalog\ImageCollection($images);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $imageCollection ...ageCollection($images); (Iris\Transfer\Catalog\ImageCollection) is incompatible with the return type declared by the abstract method Iris\Mapping\Base::assign of type Iris\Transfer\Catalog\ConfigCollection.

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...
Unused Code introduced by
$imageCollection 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...
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function map($internalData)
43
    {
44
        $externalData = [];
45
        foreach ($internalData as $image) {
46
            $externalData[] = [
47
                'url'      => $image->getUrl(),
48
                'position' => $image->getPosition()
49
            ];
50
        }
51
52
        return $externalData;
53
    }
54
}
55