Completed
Push — master ( c915d2...2ad4b2 )
by Alexander
04:32
created

ApiSerializer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 86
rs 10
c 3
b 0
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A collection() 0 4 1
A item() 0 6 1
A null() 0 7 1
A meta() 0 4 1
A sideloadIncludes() 0 4 1
A mergeIncludes() 0 10 2
A includedData() 0 4 1
1
<?php
2
3
namespace Flugg\Responder\Serializers;
4
5
use League\Fractal\Resource\ResourceInterface;
6
use League\Fractal\Serializer\ArraySerializer;
7
8
class ApiSerializer extends ArraySerializer
9
{
10
    /**
11
     * Serialize a collection.
12
     *
13
     * @param  string $resourceKey
14
     * @param  array  $data
15
     * @return array
16
     */
17
    public function collection( $resourceKey, array $data )
18
    {
19
        return $this->item( $resourceKey, $data );
0 ignored issues
show
Best Practice introduced by
The expression return $this->item($resourceKey, $data); seems to be an array, but some of its elements' types (boolean|null) are incompatible with the return type of the parent method League\Fractal\Serialize...ySerializer::collection of type array<string,array>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
20
    }
21
22
    /**
23
     * Serialize an item.
24
     *
25
     * @param  string $resourceKey
26
     * @param  array  $data
27
     * @return array
28
     */
29
    public function item( $resourceKey, array $data )
30
    {
31
        return array_merge( $this->null(), [
32
            'data' => $data
33
        ] );
34
    }
35
36
    /**
37
     * Serialize a null resource.
38
     *
39
     * @return array
40
     */
41
    public function null()
42
    {
43
        return [
44
            'success' => true,
45
            'data' => null
46
        ];
47
    }
48
49
    /**
50
     * Serialize the meta.
51
     *
52
     * @param  array $meta
53
     * @return array
54
     */
55
    public function meta( array $meta )
56
    {
57
        return $meta;
58
    }
59
60
    /**
61
     * Indicates if includes should be side-loaded.
62
     *
63
     * @return bool
64
     */
65
    public function sideloadIncludes()
66
    {
67
        return true;
68
    }
69
70
    public function mergeIncludes( $transformedData, $includedData )
71
    {
72
        $resourceKey = key( $includedData );
73
74
        if ( $resourceKey ) {
75
            $includedData[ $resourceKey ] = $includedData[ $resourceKey ][ 'data' ];
76
        }
77
78
        return array_merge( $transformedData, $includedData );
79
    }
80
81
    /**
82
     * Serialize the included data.
83
     *
84
     * @param ResourceInterface $resource
85
     * @param array             $data
86
     *
87
     * @return array
88
     */
89
    public function includedData( ResourceInterface $resource, array $data )
90
    {
91
        return [ ];
92
    }
93
}