Completed
Pull Request — master (#42)
by Greg
01:59
created

PropertyList::instantiateTableTransformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\Options\FormatterOptions;
5
use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
6
use Consolidation\OutputFormatters\Transformations\PropertyParser;
7
use Consolidation\OutputFormatters\Transformations\ReorderFields;
8
use Consolidation\OutputFormatters\Transformations\TableTransformation;
9
use Consolidation\OutputFormatters\Transformations\PropertyListTableTransformation;
10
11
/**
12
 * Holds an array where each element of the array is one
13
 * key : value pair.  The keys must be unique, as is typically
14
 * the case for associative arrays.
15
 */
16
class PropertyList extends AbstractStructuredList
17
{
18
    /**
19
     * Restructure this data for output by converting it into a table
20
     * transformation object.
21
     *
22
     * @param FormatterOptions $options Options that affect output formatting.
23
     * @return Consolidation\OutputFormatters\Transformations\TableTransformation
24
     */
25
    public function restructure(FormatterOptions $options)
26
    {
27
        $data = [$this->getArrayCopy()];
28
        $options->setConfigurationDefault('list-orientation', true);
29
        $tableTransformer = $this->createTableTransformation($data, $options);
30
        return $tableTransformer;
31
    }
32
33
    public function getListData(FormatterOptions $options)
34
    {
35
        $data = $this->getArrayCopy();
36
37
        $defaults = $this->defaultOptions();
38
        $fieldLabels = $this->getReorderedFieldLabels([$data], $options, $defaults);
39
40
        $result = [];
41
        foreach ($fieldLabels as $id => $label) {
42
            $result[$id] = $data[$id];
43
        }
44
        return $result;
45
    }
46
47
    protected function defaultOptions()
48
    {
49
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array(\Consolidat...rent::defaultOptions(); seems to be an array, but some of its elements' types (boolean) are incompatible with the return type of the parent method Consolidation\OutputForm...redList::defaultOptions of type array<*,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...
50
            FormatterOptions::LIST_ORIENTATION => true,
51
        ] + parent::defaultOptions();
52
    }
53
54
    protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
55
    {
56
        return new PropertyListTableTransformation($data, $fieldLabels, $rowLabels);
57
    }
58
}
59