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\tableschema; |
||
4 | |||
5 | use frictionlessdata\tableschema\Fields\FieldsFactory; |
||
6 | |||
7 | class EditableSchema extends Schema |
||
8 | { |
||
9 | public function __construct($descriptor = null) |
||
10 | { |
||
11 | $this->descriptor = empty($descriptor) ? (object) ['fields' => []] : $descriptor; |
||
12 | } |
||
13 | |||
14 | public function fields($newFields = null) |
||
15 | { |
||
16 | if (!is_null($newFields)) { |
||
17 | $this->descriptor()->fields = []; |
||
18 | $this->fieldsCache = []; |
||
19 | foreach ($newFields as $name => $field) { |
||
20 | if (!is_a($field, 'frictionlessdata\\tableschema\\Fields\\BaseField')) { |
||
21 | if (!isset($field->name)) { |
||
22 | $field->name = $name; |
||
23 | } |
||
24 | $field = FieldsFactory::field($field); |
||
25 | } |
||
26 | $this->fieldsCache[$name] = $field; |
||
27 | $this->descriptor()->fields[] = $field->descriptor(); |
||
28 | } |
||
29 | |||
30 | return $this->revalidate(); |
||
0 ignored issues
–
show
|
|||
31 | } else { |
||
32 | return is_null($this->fieldsCache) ? [] : $this->fieldsCache; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public function field($name, $field = null) |
||
37 | { |
||
38 | $fields = $this->fields(); |
||
39 | if (!is_null($field)) { |
||
40 | $fields[$name] = $field; |
||
41 | |||
42 | return $this->fields($fields); |
||
43 | } elseif (array_key_exists($name, $fields)) { |
||
44 | return $fields[$name]; |
||
45 | } else { |
||
46 | throw new \Exception("unknown field name: {$name}"); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | public function removeField($name) |
||
51 | { |
||
52 | $fields = $this->fields(); |
||
53 | unset($fields[$name]); |
||
54 | |||
55 | return $this->fields($fields); |
||
56 | } |
||
57 | |||
58 | View Code Duplication | public function primaryKey($primaryKey = null) |
|
0 ignored issues
–
show
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...
|
|||
59 | { |
||
60 | if (is_null($primaryKey)) { |
||
61 | return parent::primaryKey(); |
||
62 | } else { |
||
63 | $this->descriptor()->primaryKey = $primaryKey; |
||
64 | |||
65 | return $this->revalidate(); |
||
0 ignored issues
–
show
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
Loading history...
|
|||
66 | } |
||
67 | } |
||
68 | |||
69 | View Code Duplication | public function foreignKeys($foreignKeys = null) |
|
0 ignored issues
–
show
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...
|
|||
70 | { |
||
71 | if (is_null($foreignKeys)) { |
||
72 | return parent::foreignKeys(); |
||
73 | } else { |
||
74 | $this->descriptor()->foreignKeys = $foreignKeys; |
||
75 | |||
76 | return $this->revalidate(); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | View Code Duplication | public function missingValues($missingValues = null) |
|
0 ignored issues
–
show
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...
|
|||
81 | { |
||
82 | if (is_null($missingValues)) { |
||
83 | return parent::missingValues(); |
||
84 | } else { |
||
85 | $this->descriptor()->missingValues = $missingValues; |
||
86 | |||
87 | return $this->revalidate(); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function revalidate() |
||
92 | { |
||
93 | $validationErrors = SchemaValidator::validate($this->descriptor()); |
||
94 | if (count($validationErrors) > 0) { |
||
95 | throw new Exceptions\SchemaValidationFailedException($validationErrors); |
||
96 | } else { |
||
97 | return $this; |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.