Completed
Push — master ( 0c3e92...3c3b41 )
by Ori
01:47
created

src/DataStreams/TabularInlineDataStream.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace frictionlessdata\datapackage\DataStreams;
4
5
use frictionlessdata\tableschema\DataSources\NativeDataSource;
6
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
7
8
class TabularInlineDataStream extends TabularDataStream
9
{
10
    protected function getDataSourceObject()
11
    {
12
        $data = json_decode(json_encode($this->dataSource), true);
13
        if (is_array($data)) {
14
            $numFields = count($this->schema->fields());
15
            $objRows = [];
16
            if (array_sum(array_keys($data[0])) == array_sum(range(0, $numFields - 1))) {
17
                // Row Arrays - convert to Row Objects
18
                $header = array_shift($data);
19
                foreach ($data as $row) {
20
                    $objRow = [];
21
                    foreach ($header as $fieldOrder => $fieldName) {
22
                        $objRow[$fieldName] = $row[$fieldOrder];
23
                    }
24
                    $objRows[] = $objRow;
25
                }
26
            } else {
27
                // Row Objects - no processing needed
28
                $objRows = $data;
29
            }
30
31
            return new NativeDataSource($objRows);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \frictionless...veDataSource($objRows); (frictionlessdata\tablesc...ources\NativeDataSource) is incompatible with the return type of the parent method frictionlessdata\datapac...am::getDataSourceObject of type frictionlessdata\tablesc...taSources\CsvDataSource.

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...
32
        } else {
33
            throw new DataStreamOpenException('inline tabular data must be an array');
34
        }
35
    }
36
}
37