Completed
Pull Request — master (#7)
by Rafael
03:49
created

Image::assign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Iris\Mapping;
4
5
class Image extends Base
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10
    public function assign(array $externalData)
11
    {
12
        $imageCollection = new \Iris\Transfer\Catalog\ImageCollection();
13
14
        foreach ($externalData as $image) {
15
            $imagesCollection[] = new \Iris\Transfer\Catalog\Image([
0 ignored issues
show
Coding Style Comprehensibility introduced by
$imagesCollection was never initialized. Although not strictly required by PHP, it is generally a good practice to add $imagesCollection = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
16
                'url'           => $image['url'],
17
                'position'      => $image['position'],
18
                'processStatus' => $image['process_status']
19
            ]);
20
        }
21
22
        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...
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function map($internalData)
29
    {
30
        $externalData = [];
31
        foreach ($internalData as $image) {
32
            $externalData[] = [
33
                'url'           => $image->getUrl(),
34
                'position'      => $image->getPosition(),
35
                'processStatus' => $image->getProcessStatus()
36
            ];
37
        }
38
39
        return $externalData;
40
    }
41
}
42