Completed
Push — master ( 77c4c6...fbe049 )
by Ori
04:20
created

EditableSchema::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\tableschema;
4
5
class EditableSchema extends Schema
6
{
7
    public function __construct($descriptor = null)
8
    {
9
        $this->descriptor = empty($descriptor) ? (object) ['fields' => []] : $descriptor;
10
    }
11
12
    public function fields($newFields = null)
13
    {
14
        if (!is_null($newFields)) {
15
            $this->fieldsCache = $newFields;
16
            $this->descriptor()->fields = [];
17
            foreach ($newFields as $field) {
18
                $this->descriptor()->fields[] = $field->descriptor();
19
            }
20
21
            return $this->revalidate();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->revalidate(); (frictionlessdata\tableschema\EditableSchema) is incompatible with the return type of the parent method frictionlessdata\tableschema\Schema::fields of type frictionlessdata\tableschema\Fields\BaseField[].

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...
22
        } else {
23
            return is_null($this->fieldsCache) ? [] : $this->fieldsCache;
24
        }
25
    }
26
27
    public function field($name, $field = null)
28
    {
29
        $fields = $this->fields();
30
        if (!is_null($field)) {
31
            $fields[$name] = $field;
32
33
            return $this->fields($fields);
34
        } elseif (array_key_exists($name, $fields)) {
35
            return $fields[$name];
36
        } else {
37
            throw new \Exception("unknown field name: {$name}");
38
        }
39
    }
40
41
    public function removeField($name)
42
    {
43
        $fields = $this->fields();
44
        unset($fields[$name]);
45
46
        return $this->fields($fields);
47
    }
48
49 View Code Duplication
    public function primaryKey($primaryKey = null)
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...
50
    {
51
        if (is_null($primaryKey)) {
52
            return parent::primaryKey();
53
        } else {
54
            $this->descriptor()->primaryKey = $primaryKey;
55
56
            return $this->revalidate();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->revalidate(); (frictionlessdata\tableschema\EditableSchema) is incompatible with the return type of the parent method frictionlessdata\tableschema\Schema::primaryKey of type 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 '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...
57
        }
58
    }
59
60 View Code Duplication
    public function foreignKeys($foreignKeys = null)
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...
61
    {
62
        if (is_null($foreignKeys)) {
63
            return parent::foreignKeys();
64
        } else {
65
            $this->descriptor()->foreignKeys = $foreignKeys;
66
67
            return $this->revalidate();
68
        }
69
    }
70
71 View Code Duplication
    public function missingValues($missingValues = null)
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...
72
    {
73
        if (is_null($missingValues)) {
74
            return parent::missingValues();
75
        } else {
76
            $this->descriptor()->missingValues = $missingValues;
77
78
            return $this->revalidate();
79
        }
80
    }
81
82
    public function revalidate()
83
    {
84
        $validationErrors = SchemaValidator::validate($this->descriptor());
85
        if (count($validationErrors) > 0) {
86
            throw new Exceptions\SchemaValidationFailedException($validationErrors);
87
        } else {
88
            return $this;
89
        }
90
    }
91
}
92