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

Image::assign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4286
cc 2
eloc 11
nc 2
nop 1
crap 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