Completed
Pull Request — master (#69)
by Greg
01:51
created

PropertyList::convert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 implements ConversionInterface
17
{
18
    /**
19
     * @inheritdoc
20
     */
21 View Code Duplication
    public function convert(FormatterOptions $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        $defaults = $this->defaultOptions();
24
        $fields = $this->getFields($options, $defaults);
25
        if (FieldProcessor::hasUnstructuredFieldAccess($fields)) {
26
            return new UnstructuredData($this->getArrayCopy());
27
        }
28
        return $this;
29
    }
30
31
    /**
32
     * Restructure this data for output by converting it into a table
33
     * transformation object.
34
     *
35
     * @param FormatterOptions $options Options that affect output formatting.
36
     * @return Consolidation\OutputFormatters\Transformations\TableTransformation
37
     */
38
    public function restructure(FormatterOptions $options)
39
    {
40
        $data = [$this->getArrayCopy()];
41
        $options->setConfigurationDefault('list-orientation', true);
42
        $tableTransformer = $this->createTableTransformation($data, $options);
43
        return $tableTransformer;
44
    }
45
46
    public function getListData(FormatterOptions $options)
47
    {
48
        $data = $this->getArrayCopy();
49
50
        $defaults = $this->defaultOptions();
51
        $fieldLabels = $this->getReorderedFieldLabels([$data], $options, $defaults);
52
53
        $result = [];
54
        foreach ($fieldLabels as $id => $label) {
55
            $result[$id] = $data[$id];
56
        }
57
        return $result;
58
    }
59
60
    protected function defaultOptions()
61
    {
62
        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...
63
            FormatterOptions::LIST_ORIENTATION => true,
64
        ] + parent::defaultOptions();
65
    }
66
67
    protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
68
    {
69
        return new PropertyListTableTransformation($data, $fieldLabels, $rowLabels);
70
    }
71
}
72