Completed
Push — master ( 482bb6...bff53a )
by Amine
03:17
created

object.php ➔ has()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 8
loc 8
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
 * Gets the value of a key from an array or the
8
 * value of a public attribute from an object.
9
 * ```php
10
 * $data = [
11
 *     ['name' => 'foo', 'type' => 'test'],
12
 *     ['name' => 'bar', 'type' => 'test'],
13
 *     (object) ['name' => 'baz']
14
 * ];
15
 * $nameOf = value('name');
16
 * value(0, $data) // ['name' => 'foo', 'type' => 'test']
17
 * $nameOf($data[1]) // 'bar'
18
 * $nameOf($data[2]) // 'baz'
19
 * ```
20
 *
21
 * @signature String|Number -> [key => *] -> *
22
 * @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...
23
 * @param  array $array
0 ignored issues
show
Bug introduced by
There is no parameter named $array. 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...
24
 * @return mixed
25
 */
26 View Code Duplication
function value() {
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...
27
    $value = function($name, $array){
28
        return ('Object' == type($array))
29
            ? $array->{$name}
30
            : $array[$name];
31
    };
32
    return apply(curry($value), func_get_args());
33
}
34
35
/**
36
 * Checks if the given array or object has a specific key or public attribute.
37
 * ```php
38
 * $array = [
39
 *     'type' => 'Array',
40
 *     'length' => 78
41
 * ];
42
 * $array[3] = 'three';
43
 * $object = (object) ['name' => 'ok'];
44
 *
45
 * $hasName = F\has('name');
46
 *
47
 * F\has('type', $array); // true
48
 * F\has(3, $array); // true
49
 * $hasName($array); // false
50
 * $hasName($object); // true
51
 * F\has('length', $object); // false
52
 * ```
53
 *
54
 * @signature String|Number -> [key => *] -> Boolean
55
 * @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...
56
 * @param  array $array
0 ignored issues
show
Bug introduced by
There is no parameter named $array. 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...
57
 * @return mixed
58
 */
59 View Code Duplication
function has() {
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...
60
    $has = function($name, $array){
61
        return ('Object' == type($array))
62
            ? isset($array->{$name})
63
            : isset($array[$name]);
64
    };
65
    return apply(curry($has), func_get_args());
66
}
67
68
/**
69
 * Checks if an attribute or value of an object or
70
 * associative array passes the given predicate.
71
 * ```php
72
 * $data = [
73
 *     'name' => ''
74
 * ]
75
 * ```
76
 */
77
78
/**
79
 * Converts an associative array to an array of [key, value] pairs.
80
 * ```php
81
 * $list = ['key' => 'value', 'number' => 53, 'foo', 'bar'];
82
 * toPairs($list); // [['key', 'value'], ['number', 53], [0, 'foo'], [1, 'bar']]
83
 * ```
84
 *
85
 * @signature [a => b] -> [(a,b)]
86
 * @param  array $list
87
 * @return array
88
 */
89
function toPairs($list) {
90
    return map(function($key) use($list) {
91
        return [$key, $list[$key]];
92
    }, array_keys($list));
93
}
94