Completed
Push — d64 ( 47f128...a6abe6 )
by Welling
02:17
created

ClientLocal::createPreferences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Directus – <http://getdirectus.com>
5
 *
6
 * @link      The canonical repository – <https://github.com/directus/directus>
7
 * @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com>
8
 * @license   GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
9
 */
10
11
namespace Directus\SDK;
12
13
use Directus\Database\Connection;
14
use Directus\Database\TableGateway\BaseTableGateway;
15
use Directus\Database\TableGateway\DirectusMessagesTableGateway;
16
use Directus\Database\TableGateway\RelationalTableGateway;
17
use Directus\Database\TableSchema;
18
use Directus\Util\ArrayUtils;
19
20
/**
21
 * Client Local
22
 *
23
 * Client to Interact with the database directly using Directus Database Component
24
 *
25
 * @author Welling Guzmán <[email protected]>
26
 */
27
class ClientLocal extends AbstractClient
28
{
29
    /**
30
     * @var BaseTableGateway[]
31
     */
32
    protected $tableGateways = [];
33
34
    /**
35
     * @var Connection
36
     */
37
    protected $connection = null;
38
39
    /**
40
     * ClientLocal constructor.
41
     *
42
     * @param $connection
43
     */
44
    public function __construct($connection)
45
    {
46
        $this->connection = $connection;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function getTables(array $params = [])
53
    {
54
        return $this->createResponseFromData(TableSchema::getTablesSchema($params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...TablesSchema($params)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 54 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getTable($tableName)
61
    {
62
        return $this->createResponseFromData(TableSchema::getSchemaArray($tableName));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...hemaArray($tableName)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 62 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function getColumns($tableName, array $params = [])
69
    {
70
        return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...($tableName, $params)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 70 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function getColumn($tableName, $columnName)
77
    {
78
        return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $columnName));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...bleName, $columnName)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 78 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function getEntries($tableName, array $params = [])
85
    {
86
        $tableGateway = $this->getTableGateway($tableName);
87
88
        return $this->createResponseFromData($tableGateway->getEntries($params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...->getEntries($params)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 88 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntries of type Directus\SDK\Response\EntryCollection.
Loading history...
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function getEntry($tableName, $id, array $params = [])
95
    {
96
        // @TODO: Dynamic ID
97
        return $this->getEntries($tableName, array_merge($params, [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntries..., array('id' => $id))); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntry of type Directus\SDK\Response\Entry.

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...
98
            'id' => $id
99
        ]));
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function getUsers(array $params = [])
106
    {
107
        // @TODO: store the directus tables somewhere (SchemaManager?)
108
        return $this->getEntries('directus_users', $params);
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function getUser($id, array $params = [])
115
    {
116
        return $this->getEntry('directus_users', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry('..._users', $id, $params); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getUser of type Directus\SDK\Response\Entry.

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...
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function getGroups(array $params = [])
123
    {
124
        return $this->getEntries('directus_groups', $params);
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getGroup($id, array $params = [])
131
    {
132
        return $this->getEntry('directus_groups', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry('...groups', $id, $params); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroup of type Directus\SDK\Response\Entry.

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...
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function getGroupPrivileges($groupID)
139
    {
140
        $this->getEntries('directus_privileges', [
141
            'filter' => [
142
                'group_id' => ['eq' => $groupID]
143
            ]
144
        ]);
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function getFiles(array $params = [])
151
    {
152
        return $this->getEntries('directus_files', $params);
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function getFile($id, array $params = [])
159
    {
160
        return $this->getEntry('directus_files', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry('..._files', $id, $params); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getFile of type Directus\SDK\Response\Entry.

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...
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166
    public function getSettings()
167
    {
168
        return $this->getEntries('directus_settings');
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function getSettingsByCollection($collectionName)
175
    {
176
        return $this->getEntries('directus_settings', [
177
            'filter' => [
178
                'collection' => ['eq' => $collectionName]
179
            ]
180
        ]);
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function getMessages($userId)
187
    {
188
        $messagesTableGateway = new DirectusMessagesTableGateway($this->connection, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Directus\Permissions\Acl>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
        $result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId);
190
191
        return $this->createResponseFromData($result);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($result); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 191 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197 View Code Duplication
    public function createEntry($tableName, array $data)
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...
198
    {
199
        $tableGateway = $this->getTableGateway($tableName);
200
        $data = $this->processData($tableName, $data);
201
202
        foreach($data as $key => $value) {
203
            if ($value instanceof File) {
204
                $data[$key] = $this->processFile($value);
205
            }
206
        }
207
208
        $newRecord = $tableGateway->manageRecordUpdate($tableName, $data);
209
210
        return $this->getEntry($tableName, $newRecord[$tableGateway->primaryKeyFieldName]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry($...>primaryKeyFieldName]); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createEntry of type Directus\SDK\Response\Entry.

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...
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216 View Code Duplication
    public function updateEntry($tableName, $id, array $data)
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...
217
    {
218
        $tableGateway = $this->getTableGateway($tableName);
219
        $data = $this->processData($tableName, $data);
220
221
        foreach($data as $key => $value) {
222
            if ($value instanceof File) {
223
                $data[$key] = $this->processFile($value);
224
            }
225
        }
226
227
        $updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id]));
228
229
        return $this->getEntry($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]);
230
    }
231
232
    /**
233
     * @inheritDoc
234
     */
235
    public function deleteEntry($tableName, $ids)
236
    {
237
        // @TODO: Accept EntryCollection and Entry
238
        $tableGateway = $this->getTableGateway($tableName);
239
240
        if (!is_array($ids)) {
241
            $ids = [$ids];
242
        }
243
244
        return $tableGateway->delete(function($delete) use ($ids) {
245
            return $delete->where->in('id', $ids);
246
        });
247
    }
248
249
    /**
250
     * @inheritDoc
251
     */
252
    public function createUser(array $data)
253
    {
254
        return $this->createEntry('directus_users', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...irectus_users', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createUser of type Directus\SDK\Response\Entry.

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...
255
    }
256
257
    /**
258
     * @inheritDoc
259
     */
260
    public function updateUser($id, array $data)
261
    {
262
        return $this->updateEntry('directus_users', $id, $data);
263
    }
264
265
    /**
266
     * @inheritDoc
267
     */
268
    public function deleteUser($ids)
269
    {
270
        return $this->deleteEntry('directus_users', $ids);
271
    }
272
273
    /**
274
     * @inheritDoc
275
     */
276
    public function createFile(File $file)
277
    {
278
        $data = $this->processFile($file);
279
280
        return $this->createEntry('directus_files', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...irectus_files', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createFile of type Directus\SDK\Response\Entry.

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...
281
    }
282
283
    /**
284
     * @inheritDoc
285
     */
286
    public function updateFile($id, array $data)
287
    {
288
        return $this->updateEntry('directus_files', $id, $data);
289
    }
290
291
    /**
292
     * @inheritDoc
293
     */
294
    public function deleteFile($ids)
295
    {
296
        return $this->deleteEntry('directus_files', $ids);
297
    }
298
299
    public function createPreferences($data)
300
    {
301
        if (!ArrayUtils::contains($data, ['title', 'table_name'])) {
302
            throw new \Exception('title and table_name are required');
303
        }
304
305
        $acl = $this->container->get('acl');
306
        $data['user'] = $acl->getUserId();
307
308
        return $this->createEntry('directus_preferences', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...s_preferences', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPreferences of type Directus\SDK\Response\Entry.

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...
309
    }
310
311
    /**
312
     * @inheritdoc
313
     */
314
    public function createBookmark($data)
315
    {
316
        $acl = $this->container->get('acl');
317
        $data['user'] = $acl->getUserId();
318
319
        $preferences = $this->createPreferences(ArrayUtils::pick($data, [
320
            'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user'
321
        ]));
322
323
        $title = $preferences->title;
0 ignored issues
show
Bug introduced by
The property title does not seem to exist in Directus\SDK\Response\EntryCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
324
        $tableName = $preferences->table_name;
0 ignored issues
show
Bug introduced by
The property table_name does not seem to exist in Directus\SDK\Response\EntryCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
325
        $bookmarkData = [
326
            'section' => 'search',
327
            'title' => $title,
328
            'url' => 'tables/' . $tableName . '/pref/' . $title,
329
            'user' => $data['user']
330
        ];
331
332
        return $this->createEntry('directus_bookmarks', $bookmarkData);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...marks', $bookmarkData); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createBookmark of type Directus\SDK\Response\Entry.

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...
333
    }
334
335
    /**
336
     * Get a table gateway for the given table name
337
     *
338
     * @param $tableName
339
     *
340
     * @return RelationalTableGateway
341
     */
342
    protected function getTableGateway($tableName)
343
    {
344
        if (!array_key_exists($tableName, $this->tableGateways)) {
345
            $acl = TableSchema::getAclInstance();
346
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl);
347
        }
348
349
        return $this->tableGateways[$tableName];
350
    }
351
}
352