Completed
Push — d64 ( 4484ee...a462d8 )
by Welling
04:55
created

ClientRemote   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 460
Duplicated Lines 3.7 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 25.81%

Importance

Changes 0
Metric Value
dl 17
loc 460
ccs 40
cts 155
cp 0.2581
rs 8.3999
c 0
b 0
f 0
wmc 46
lcom 1
cbo 4

43 Methods

Rating   Name   Duplication   Size   Complexity  
A getTables() 0 4 1
A getTable() 0 6 1
A getColumns() 0 6 1
A getColumn() 0 6 1
A getItems() 0 6 1
A getItem() 0 6 1
A getUsers() 0 4 1
A getUser() 0 4 1
A getGroups() 0 4 1
A getGroup() 0 6 1
A getGroupPrivileges() 0 6 1
A getFiles() 0 4 1
A getFile() 0 6 1
A getSettings() 0 4 1
A getSettingsByCollection() 0 6 1
A updateSettings() 0 8 1
A getMessages() 0 6 1
A createItem() 7 7 1
A updateItem() 7 7 1
A deleteItem() 0 6 1
A createUser() 0 4 1
A updateUser() 0 4 1
A deleteUser() 0 4 1
A createFile() 0 6 1
A updateFile() 0 12 2
A deleteFile() 0 4 1
A createPreferences() 0 10 1
A createBookmark() 0 19 1
A getBookmark() 0 6 1
A getBookmarks() 0 10 2
A createColumn() 0 8 1
A createGroup() 0 6 1
A createMessage() 0 12 1
A sendMessage() 0 4 1
A createPrivileges() 0 8 1
A createTable() 0 11 1
A createColumnUIOptions() 0 16 1
A getPreferences() 0 4 1
A deleteBookmark() 0 4 1
A deleteColumn() 0 6 1
A deleteGroup() 0 4 1
A deleteTable() 0 6 1
A getActivity() 3 12 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ClientRemote often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ClientRemote, and based on these observations, apply Extract Interface, too.

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
use Directus\Util\ArrayUtils;
13
use Directus\Util\DateUtils;
14
use Directus\Util\StringUtils;
15
16
/**
17
 * Client Remote
18
 *
19
 * @author Welling Guzmán <[email protected]>
20
 */
21
class ClientRemote extends BaseClientRemote
22
{
23
    /**
24
     * @inheritdoc
25
     */
26 2
    public function getTables(array $params = [])
27
    {
28 2
        return $this->performRequest('GET', static::TABLE_LIST_ENDPOINT);
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->performRequest('G...::TABLE_LIST_ENDPOINT); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 28 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 4
    public function getTable($tableName)
35
    {
36 4
        $path = $this->buildPath(static::TABLE_INFORMATION_ENDPOINT, $tableName);
37
38 4
        return $this->performRequest('GET', $path);
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 38 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 2
    public function getColumns($tableName, array $params = [])
45
    {
46 2
        $path = $this->buildPath(static::COLUMN_LIST_ENDPOINT, $tableName);
47
48 2
        return $this->performRequest('GET', $path);
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 48 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 2
    public function getColumn($tableName, $columnName)
55
    {
56 2
        $path = $this->buildPath(static::COLUMN_INFORMATION_ENDPOINT, [$tableName, $columnName]);
57
58 2
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 58 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 2
    public function getItems($tableName, array $options = [])
65
    {
66 2
        $path = $this->buildPath(static::TABLE_ENTRIES_ENDPOINT, $tableName);
0 ignored issues
show
Documentation introduced by
$tableName is of type string, but the function expects a array.

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...
67
68 2
        return $this->performRequest('GET', $path, ['query' => $options]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...('query' => $options)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 68 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getItems of type Directus\SDK\Response\EntryCollection.
Loading history...
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 2
    public function getItem($tableName, $id, array $options = [])
75
    {
76 2
        $path = $this->buildPath(static::TABLE_ENTRY_ENDPOINT, [$tableName, $id]);
77
78 2
        return $this->performRequest('GET', $path, ['query' => $options]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...('query' => $options)); 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::getItem of type Directus\SDK\Response\Entry.
Loading history...
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 2
    public function getUsers(array $params = [])
85
    {
86 2
        return $this->getItems('directus_users', $params);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 2
    public function getUser($id, array $params = [])
93
    {
94 2
        return $this->getItem($id, 'directus_users', $params);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 2
    public function getGroups()
101
    {
102 2
        return $this->performRequest('GET', static::GROUP_LIST_ENDPOINT);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...::GROUP_LIST_ENDPOINT); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 102 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroups of type Directus\SDK\Response\EntryCollection.
Loading history...
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 2
    public function getGroup($groupID)
109
    {
110 2
        $path = $this->buildPath(static::GROUP_INFORMATION_ENDPOINT, $groupID);
111
112 2
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 112 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroup of type Directus\SDK\Response\Entry.
Loading history...
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118 2
    public function getGroupPrivileges($groupID)
119
    {
120 2
        $path = $this->buildPath(static::GROUP_PRIVILEGES_ENDPOINT, $groupID);
121
122 2
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 122 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroupPrivileges of type Directus\SDK\Response\EntryCollection.
Loading history...
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 2
    public function getFiles()
129
    {
130 2
        return $this->performRequest('GET', static::FILE_LIST_ENDPOINT);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...c::FILE_LIST_ENDPOINT); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 130 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getFiles of type Directus\SDK\Response\EntryCollection.
Loading history...
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136 2
    public function getFile($fileID)
137
    {
138 2
        $path = $this->buildPath(static::FILE_INFORMATION_ENDPOINT, $fileID);
139
140 2
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 140 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getFile of type Directus\SDK\Response\Entry.
Loading history...
141
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146 2
    public function getSettings()
147
    {
148 2
        return $this->performRequest('GET', static::SETTING_LIST_ENDPOINT);
149
    }
150
151
    /**
152
     * @inheritdoc
153
     */
154 2
    public function getSettingsByCollection($collectionName)
155
    {
156 2
        $path = $this->buildPath(static::SETTING_COLLECTION_GET_ENDPOINT, $collectionName);
157
158 2
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 158 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInt...getSettingsByCollection of type Directus\SDK\Response\EntryCollection.
Loading history...
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function updateSettings($collection, array $data)
165
    {
166
        $path = $this->buildPath(static::SETTING_COLLECTION_UPDATE_ENDPOINT, $collection);
167
168
        return $this->performRequest('PUT', $path, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 168 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::updateSettings of type Directus\SDK\Response\Entry.
Loading history...
169
            'body' => $data
170
        ]);
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function getMessages($userId)
177
    {
178
        $path = $this->buildPath(static::MESSAGES_USER_ENDPOINT, $userId);
179
180
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 180 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
181
    }
182
183
    /**
184
     * @inheritdoc
185
     */
186 View Code Duplication
    public function createItem($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...
187
    {
188
        $path = $this->buildPath(static::TABLE_ENTRY_CREATE_ENDPOINT, $tableName);
189
        $data = $this->processData($tableName, $data);
190
191
        return $this->performRequest('POST', $path, ['body' => $data]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 191 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createItem of type Directus\SDK\Response\Entry.
Loading history...
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197 View Code Duplication
    public function updateItem($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...
198
    {
199
        $path = $this->buildPath(static::TABLE_ENTRY_UPDATE_ENDPOINT, [$tableName, $id]);
200
        $data = $this->processData($tableName, $data);
201
202
        return $this->performRequest('PUT', $path, ['body' => $data]);
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    public function deleteItem($tableName, $id)
209
    {
210
        $path = $this->buildPath(static::TABLE_ENTRY_DELETE_ENDPOINT, [$tableName, $id]);
211
212
        return $this->performRequest('DELETE', $path);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->performRequest('DELETE', $path); (Directus\SDK\Response\En...ctus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteItem of type integer.

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...
213
    }
214
215
    /**
216
     * @inheritdoc
217
     */
218
    public function createUser(array $data)
219
    {
220
        return $this->createItem('directus_users', $data);
221
    }
222
223
    /**
224
     * @inheritdoc
225
     */
226
    public function updateUser($id, array $data)
227
    {
228
        return $this->updateItem('directus_users', $id, $data);
229
    }
230
231
    /**
232
     * @inheritdoc
233
     */
234
    public function deleteUser($ids)
235
    {
236
        return $this->deleteItem('directus_users', $ids);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteItem('directus_users', $ids); (Directus\SDK\Response\En...ctus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteUser of type integer.

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...
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242
    public function createFile(File $file)
243
    {
244
        $data = $this->processFile($file);
245
246
        return $this->performRequest('POST', static::FILE_CREATE_ENDPOINT, ['body' => $data]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 246 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createFile of type Directus\SDK\Response\Entry.
Loading history...
247
    }
248
249
    /**
250
     * @inheritdoc
251
     */
252
    public function updateFile($id, $data)
253
    {
254
        if ($data instanceof File) {
255
            $data = $data->toArray();
256
        }
257
258
        $data['id'] = $id;
259
        $path = $this->buildPath(static::FILE_UPDATE_ENDPOINT, $id);
260
        $data = $this->processData('directus_files', $data);
261
262
        return $this->performRequest('POST', $path, ['body' => $data]);
263
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268
    public function deleteFile($id)
269
    {
270
        return $this->deleteItem('directus_files', $id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteItem('directus_files', $id); (Directus\SDK\Response\En...ctus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteFile of type integer.

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...
271
    }
272
273
    public function createPreferences($data)
274
    {
275
        $this->requiredAttributes(['title', 'table_name'], $data);
276
277
        $tableName = ArrayUtils::get($data, 'table_name');
278
        $path = $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $tableName);
279
        $data = $this->processData($tableName, $data);
280
281
        return $this->performRequest('POST', $path, ['body' => $data]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 281 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPreferences of type Directus\SDK\Response\Entry.
Loading history...
282
    }
283
284
    /**
285
     * @inheritdoc
286
     */
287
    public function createBookmark($data)
288
    {
289
        $preferences = $this->createPreferences(ArrayUtils::pick($data, [
290
            'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible'
291
        ]));
292
293
        $title = $preferences->title;
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<Directus\SDK\Response\Entry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
294
        $tableName = $preferences->table_name;
0 ignored issues
show
Documentation introduced by
The property table_name does not exist on object<Directus\SDK\Response\Entry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
295
        $bookmarkData = [
296
            'section' => 'search',
297
            'title' => $title,
298
            'url' => 'tables/' . $tableName . '/pref/' . $title
299
        ];
300
301
        $path = $this->buildPath(static::BOOKMARKS_CREATE_ENDPOINT);
302
        $bookmarkData = $this->processData($tableName, $bookmarkData);
303
304
        return $this->performRequest('POST', $path, ['body' => $bookmarkData]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...dy' => $bookmarkData)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 304 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createBookmark of type Directus\SDK\Response\Entry.
Loading history...
305
    }
306
307
    /**
308
     * @inheritdoc
309
     */
310
    public function getBookmark($id)
311
    {
312
        $path = $this->buildPath(static::BOOKMARKS_READ_ENDPOINT, $id);
0 ignored issues
show
Documentation introduced by
$id is of type integer, but the function expects a array.

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...
313
314
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 314 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getBookmark of type Directus\SDK\Response\Entry.
Loading history...
315
    }
316
317
    /**
318
     * @inheritdoc
319
     */
320
    public function getBookmarks($userId = null)
321
    {
322
        if ($userId !== null) {
323
            $path = $this->buildPath(static::BOOKMARKS_USER_ENDPOINT, $userId);
0 ignored issues
show
Documentation introduced by
$userId is of type integer, but the function expects a array.

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...
324
        } else {
325
            $path = $this->buildPath(static::BOOKMARKS_ALL_ENDPOINT);
326
        }
327
328
        return $this->performRequest('GET', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('GET', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 328 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getBookmarks of type Directus\SDK\Response\Entry.
Loading history...
329
    }
330
331
    /**
332
     * @inheritdoc
333
     */
334
    public function createColumn($data)
335
    {
336
        $data = $this->parseColumnData($data);
337
338
        return $this->performRequest('POST', $this->buildPath(static::COLUMN_CREATE_ENDPOINT, $data['table_name']), [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 338 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createColumn of type Directus\SDK\Response\Entry.
Loading history...
339
            'body' => $data
340
        ]);
341
    }
342
343
    /**
344
     * @inheritdoc
345
     */
346
    public function createGroup(array $data)
347
    {
348
        return $this->performRequest('POST', static::GROUP_CREATE_ENDPOINT, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 348 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createGroup of type Directus\SDK\Response\Entry.
Loading history...
349
            'body' => $data
350
        ]);
351
    }
352
353
    /**
354
     * @inheritdoc
355
     */
356
    public function createMessage(array $data)
357
    {
358
        $this->requiredAttributes(['from', 'message', 'subject'], $data);
359
        $this->requiredOneAttribute(['to', 'toGroup'], $data);
360
361
        $data['recipients'] = $this->getMessagesTo($data);
362
        ArrayUtils::remove($data, ['to', 'toGroup']);
363
364
        return $this->performRequest('POST', static::MESSAGES_CREATE_ENDPOINT, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 364 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createMessage of type Directus\SDK\Response\Entry.
Loading history...
365
            'body' => $data
366
        ]);
367
    }
368
369
    /**
370
     * @inheritdoc
371
     */
372
    public function sendMessage(array $data)
373
    {
374
        return $this->createMessage($data);
375
    }
376
377
    /**
378
     * @inheritdoc
379
     */
380
    public function createPrivileges(array $data)
381
    {
382
        $this->requiredAttributes(['group_id', 'table_name'], $data);
383
384
        return $this->performRequest('POST', static::GROUP_PRIVILEGES_CREATE_ENDPOINT, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 384 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPrivileges of type Directus\SDK\Response\Entry.
Loading history...
385
            'body' => $data
386
        ]);
387
    }
388
389
    public function createTable($name, array $params = [])
390
    {
391
        $data = [
392
            'addTable' => true,
393
            'table_name' => $name
394
        ];
395
396
        return $this->performRequest('POST', static::TABLE_CREATE_ENDPOINT, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 396 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createTable of type Directus\SDK\Response\Entry.
Loading history...
397
            'body' => $data
398
        ]);
399
    }
400
401
    /**
402
     * @inheritdoc
403
     */
404
    public function createColumnUIOptions(array $data)
405
    {
406
        $this->requiredAttributes(['table', 'column', 'ui', 'options'], $data);
407
408
        $path = $this->buildPath(static::COLUMN_OPTIONS_CREATE_ENDPOINT, [
409
            $data['table'],
410
            $data['column'],
411
            $data['ui']
412
        ]);
413
414
        $data = ArrayUtils::get($data, 'options');
415
416
        return $this->performRequest('POST', $path, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...rray('body' => $data)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 416 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInt...::createColumnUIOptions of type Directus\SDK\Response\Entry.
Loading history...
417
            'body' => $data
418
        ]);
419
    }
420
421
    /**
422
     * @inheritdoc
423
     */
424
    public function getPreferences($table, $user = null)
425
    {
426
        return $this->performRequest('POST', $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $table));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...CES_ENDPOINT, $table)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 426 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getPreferences of type Directus\SDK\Response\Entry.
Loading history...
427
    }
428
429
    /**
430
     * @inheritdoc
431
     */
432
    public function deleteBookmark($id)
433
    {
434
        return $this->deleteItem('directus_bookmarks', $id);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->deleteItem('directus_bookmarks', $id); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 434 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteBookmark of type Directus\SDK\Response\Entry.
Loading history...
435
    }
436
437
    /**
438
     * @inheritdoc
439
     */
440
    public function deleteColumn($name, $table)
441
    {
442
        $path = $this->buildPath(static::COLUMN_DELETE_ENDPOINT, [$name, $table]);
443
444
        return $this->performRequest('DELETE', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('DELETE', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 444 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteColumn of type Directus\SDK\Response\Entry.
Loading history...
445
    }
446
447
    /**
448
     * @inheritdoc
449
     */
450
    public function deleteGroup($id)
451
    {
452
        return $this->deleteItem('directus_groups', $id);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->deleteItem('directus_groups', $id); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 452 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteGroup of type Directus\SDK\Response\Entry.
Loading history...
453
    }
454
455
    /**
456
     * @inheritdoc
457
     */
458
    public function deleteTable($name)
459
    {
460
        $path = $this->buildPath(static::TABLE_DELETE_ENDPOINT, $name);
461
462
        return $this->performRequest('DELETE', $path);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('DELETE', $path); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 462 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteTable of type Directus\SDK\Response\Entry.
Loading history...
463
    }
464
465
    /**
466
     * @inheritdoc
467
     */
468
    public function getActivity(array $params = [])
469 2
    {
470 View Code Duplication
        if (!ArrayUtils::has($params, 'filters.datetime')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
471
            $params['filters']['datetime'] = ['>=' => DateUtils::daysAgo(30)];
472
        }
473
474
        $path = $this->buildPath(static::ACTIVITY_GET_ENDPOINT);
475
476
        return $this->performRequest('GET', $path, [
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...y('query' => $params)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 476 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getActivity of type Directus\SDK\Response\Entry.
Loading history...
477
            'query' => $params
478
        ]);
479
    }
480
}
481