Completed
Push — master ( 1d37d4...bf8214 )
by Ori
01:34
created

TabularResource::handlesDescriptor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\datapackage\Resources;
4
5
use frictionlessdata\datapackage\DataStreams\TabularDataStream;
6
use frictionlessdata\datapackage\DataStreams\TabularInlineDataStream;
7
use frictionlessdata\datapackage\Registry;
8
9
class TabularResource extends DefaultResource
10
{
11
    public function schema()
12
    {
13
        // TODO: change to table schema object
14
        return $this->descriptor()->schema;
15
    }
16
17
    public function getFileExtension()
18
    {
19
        return '.csv';
20
    }
21
22
    /**
23
     * @param string $dataSource
24
     *
25
     * @return TabularDataStream
26
     */
27
    protected function getDataStream($dataSource, $dataSourceOptions = null)
28
    {
29
        $dataSourceOptions = array_merge([
30
            'schema' => $this->schema(),
31
            'dialect' => isset($this->descriptor()->dialect) ? $this->descriptor()->dialect : null,
32
        ], (array) $dataSourceOptions);
33
34
        return new TabularDataStream($this->normalizeDataSource($dataSource, $this->basePath), $dataSourceOptions);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \frictionless...), $dataSourceOptions); (frictionlessdata\datapac...reams\TabularDataStream) is incompatible with the return type of the parent method frictionlessdata\datapac...Resource::getDataStream of type frictionlessdata\datapac...reams\DefaultDataStream.

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...
35
    }
36
37
    protected function getInlineDataStream($data)
38
    {
39
        return new TabularInlineDataStream($data, [
40
            'schema' => $this->schema(),
41
            'dialect' => isset($this->descriptor()->dialect) ? $this->descriptor()->dialect : null,
42
        ]);
43
    }
44
45
    public static function handlesDescriptor($descriptor)
46
    {
47
        return (
48
            Registry::getResourceValidationProfile($descriptor) == 'tabular-data-resource'
49
            || (isset($descriptor->format) && in_array($descriptor->format, ['csv', 'tsv']))
50
        );
51
    }
52
}
53