Completed
Push — master ( d55531...cfe428 )
by Amine
02:42
created

object.php ➔ check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Tarsana\Functional;
2
/**
3
 * This file contains some useful functions to handle objects (associative arrays are considered objects).
4
 */
5
6
/**
7
 * Returns a deep copy of the given value. `Callable`s are not copied but returned by reference.
8
 * ```php
9
 * $data = (object) [
10
 *     'content' => (object) ['name' => 'foo'],
11
 *     'other' => 'value'
12
 * ];
13
 *
14
 * $clonedData = clone_($data);
15
 * $clonedData->content->name = 'bar';
16
 *
17
 * $clonedData; // stdClass({content: {name: 'bar'}, other: 'value'})
18
 * $data; // stdClass({content: {name: 'foo'}, other: 'value'})
19
 * ```
20
 *
21
 * @signature a -> a
22
 * @param  mixed $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
23
 * @return mixed
24
 */
25
function clone_() {
26
    $clone = function($value) {
27
        switch (type($value)) {
28
            case 'Null':
29
            case 'Boolean':
30
            case 'String':
31
            case 'Function':
32
            case 'Resource':
33
            case 'Number':
34
                return $value;
35
            case 'ArrayObject':
36
            case 'Array':
37
            case 'List':
38
                return map(clone_(), $value);
39
            case 'Error':
40
            case 'Stream':
41
            case 'Object':
42
                $result = clone $value;
43
                foreach (keys($value) as $key) {
44
                    $result->{$key} = clone_($result->{$key});
45
                }
46
                return $result;
47
        }
48
        return $value;
49
    };
50
    return apply(curry($clone), func_get_args());
51
}
52
53
/**
54
 * Converts an object to an associative array containing accessible
55
 * non-static attributes. A curried version of `get_object_vars`.
56
 * If `$object` is not an object, it returns it.
57
 * ```php
58
 * class foo {
59
 *     private $a;
60
 *     public $b = 1;
61
 *     public $c;
62
 *     private $d;
63
 *     static $e;
64
 *
65
 *     public function getAttrs() {
66
 *         return attributes($this));
67
 *     }
68
 * }
69
 *
70
 * $test = new foo;
71
 * attributes($test); // ['b' => 1, 'c' => null]
72
 *
73
 * $test->getAttrs(); // ['a' => null, 'b' => 1, 'c' => null, 'd' => null]
74
 * ```
75
 *
76
 * @signature {k: v} -> {k: v}
77
 * @param  object $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
 * @return array
79
 */
80
function attributes() {
81
    $attrs = function($object) {
82
        if (is_object($object))
83
            return get_object_vars($object);
84
        return $object;
85
    };
86
    return apply(curry($attrs), func_get_args());
87
}
88
89
/**
90
 * Returns a list of array's keys or object's accessible attributes names.
91
 * ```php
92
 * keys([1, 2, 3]); // [0, 1, 2]
93
 * keys(['name' => 'foo', 'age' => 11]); // ['name', 'age']
94
 * keys((object)['name' => 'foo', 'age' => 11]); // ['name', 'age']
95
 * ```
96
 *
97
 * @signature [*] -> [Number]
98
 * @signature {k: v} -> [k]
99
 */
100
function keys() {
101
    $keys = function($object) {
102
        return array_keys(attributes($object));
103
    };
104
    return apply(curry($keys), func_get_args());
105
}
106
107
/**
108
 * Returns a list of array's values or object's accessible attributes values.
109
 * ```php
110
 * keys([1, 2, 3]); // [1, 2, 3]
111
 * keys(['name' => 'foo', 'age' => 11]); // ['foo', 11]
112
 * keys((object)['name' => 'foo', 'age' => 11]); // ['foo', 11]
113
 * ```
114
 *
115
 * @signature [a] -> [a]
116
 * @signature {k: v} -> [v]
117
 */
118
function values() {
119
    $values = function($object) {
120
        return array_values(attributes($object));
121
    };
122
    return apply(curry($values), func_get_args());
123
}
124
125
/**
126
 * Checks if the given array or object has a specific key or accessible attribute.
127
 * ```php
128
 * class Foo {
129
 *     public $a = 1;
130
 *     private $b = 2;
131
 *     protected $c = 3;
132
 *     public $d;
133
 * }
134
 * $array = [
135
 *     'type' => 'Array',
136
 *     'length' => 78
137
 * ];
138
 * $array[3] = 'three';
139
 * $object = (object) ['name' => 'ok'];
140
 *
141
 * $hasName = has('name');
142
 *
143
 * has('type', $array); // true
144
 * has(3, $array); // true
145
 * $hasName($array); // false
146
 * $hasName($object); // true
147
 * has('length', $object); // false
148
 * has('a', new Foo); // true
149
 * has('b', new Foo); // false (not a public attribute)
150
 * ```
151
 *
152
 * @signature k -> {k: v} -> Boolean
153
 * @param  string|int $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
154
 * @param  mixed $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
155
 * @return bool
156
 */
157
function has() {
158
    $has = function($name, $object){
159
        return contains($name, keys($object));
160
    };
161
    return apply(curry($has), func_get_args());
162
}
163
164
/**
165
 * Gets the value of a key from an array or the
166
 * value of an accessible attribute from an object.
167
 * If the key/attribute is missing, `null` is returned.
168
 * ```php
169
 * $data = [
170
 *     ['name' => 'foo', 'type' => 'test'],
171
 *     ['name' => 'bar', 'type' => 'test'],
172
 *     (object) ['name' => 'baz']
173
 *     [1, 2, 3]
174
 * ];
175
 * $nameOf = get('name');
176
 * get(0, $data) // ['name' => 'foo', 'type' => 'test']
177
 * $nameOf($data[1]) // 'bar'
178
 * $nameOf($data[2]) // 'baz'
179
 * $nameOf($data[4]) // null
180
 * ```
181
 *
182
 * @signature k -> {k: v} -> Maybe(v)
183
 * @param  string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
184
 * @param  array $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
185
 * @return mixed
186
 */
187
function get() {
188
    $get = function($name, $object){
189
        $object = attributes($object);
190
        return has($name, $object)
191
            ? $object[$name]
192
            : null;
193
    };
194
    return apply(curry($get), func_get_args());
195
}
196
197
/**
198
 * Gets a value from an array/object using a path of keys/attributes.
199
 * ```php
200
 * $data = [
201
 *     ['name' => 'foo', 'type' => 'test'],
202
 *     ['name' => 'bar', 'type' => 'test'],
203
 *     (object) ['name' => 'baz', 'scores' => [1, 2, 3]]
204
 * ];
205
 * $nameOfFirst = getPath([0, 'name']);
206
 * $nameOfFirst($data) // 'foo'
207
 * getPath([2, 'scores', 1]) // 2
208
 * ```
209
 *
210
 * @signature [k] -> {k: v} -> v
211
 * @param  array $path
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
212
 * @param  mixed $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
213
 * @return mixed
214
 */
215
function getPath() {
216
    $getPath = function($path, $object){
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
217
218
    };
219
    return apply(curry($getPath), func_get_args());
220
}
221
222
/**
223
 * Returns a new array or object with the value of a key or a public attribute set
224
 * to a specific value. if the key/attribute is missing and `$object` is an `array`
225
 * or `stdClass`; the key/attribute is added. Otherwise `null` is returned.
226
 * ```php
227
 * $task = ['name' => 'test', 'complete' => false];
228
 * $done = set('complete', true);
229
 * $done($task); // ['name' => 'test', 'complete' => true]
230
 * $done((object) $task); // stdClass({name: 'test', complete: true})
231
 * set('description', 'Some text here', $task); // ['name' => 'test', 'complete' => false, 'description' => 'Some text here']
232
 * ```
233
 *
234
 * @signature k -> v -> {k: v} -> {k: v}
235
 * @param  string|int $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
236
 * @param  mixed $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
237
 * @param  mixed $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
238
 * @return mixed
239
 */
240 View Code Duplication
function set() {
0 ignored issues
show
Duplication introduced by
This function 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...
241
    $set = function($name, $value, $object) {
242
        return is_object($object)
243
            ? $object->{$name}
244
            : $object[$name];
245
    };
246
    return apply(curry($set), func_get_args());
247
}
248
249
/**
250
 * Checks if an attribute/value of an object/array passes the given predicate.
251
 * ```php
252
 * $foo = ['name' => 'foo', 'age' => 11];
253
 * $isAdult = check(gt(__(), 18), 'age');
254
 * check(startsWith('f'), 'name', $foo); // true
255
 * check(startsWith('g'), 'name', $foo); // false
256
 * $isAdult($foo); // false
257
 * ```
258
 *
259
 * @signature (a -> Boolean) -> k -> {k : a} -> Boolean
260
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
261
 * @param  string|int $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
262
 * @param  mixed $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
263
 * @return bool
264
 */
265
function check() {
266
    $check = function($predicate, $key, $object) {
267
        return $predicate(get($key, $object));
268
    };
269
    return apply(curry($check), func_get_args());
270
}
271
272
/**
273
 * Converts an object or associative array to an array of [key, value] pairs.
274
 * ```php
275
 * $list = ['key' => 'value', 'number' => 53, 'foo', 'bar'];
276
 * toPairs($list); // [['key', 'value'], ['number', 53], [0, 'foo'], [1, 'bar']]
277
 * ```
278
 *
279
 * @signature {k: v} -> [(k,v)]
280
 * @param  array $object
281
 * @return array
282
 */
283
function toPairs($object) {
284
    $object = attributes($object);
285
    return map(function($key) use($object) {
286
        return [$key, $object[$key]];
287
    }, keys($object));
288
}
289