Completed
Push — master ( e3ed33...61d114 )
by
unknown
04:23
created

Image   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 42
ccs 23
cts 23
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assign() 0 19 2
A map() 0 13 2
1
<?php
2
3
namespace Iris\Mapping;
4
5
class Image extends Base
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10 1
    public function assign(array $externalData)
11
    {
12 1
        $imageCollection  = new \Iris\Transfer\Catalog\ImageCollection;
13
14 1
        foreach ($externalData['items'] as $data) {
15 1
            $imageCollection[] = new \Iris\Transfer\Catalog\Image([
16 1
                'url'           => $data['url'],
17 1
                'position'      => $data['position'],
18 1
                'processStatus' => $data['process_status']
19 1
            ]);
20 1
        }
21
22 1
        $configCollection = new \Iris\Transfer\Catalog\Config([
0 ignored issues
show
Unused Code introduced by
$configCollection 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...
23 1
            'sku'             => $externalData['sku'],
24
            'imageCollection' => $imageCollection
25 1
            ]);
26
27 1
        return $imageCollection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $imageCollection; (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...
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function map($internalData)
34
    {
35 1
        $externalData = ['items' => []];
36 1
        foreach ($internalData as $item) {
37 1
            $externalData['items'][] = [
38 1
                'url'           => $item->getUrl(),
39 1
                'position'      => $item->getPosition(),
40 1
                'processStatus' => $item->getProcessStatus()
41 1
            ];
42 1
        }
43
44 1
        return $externalData;
45
    }
46
}
47