Completed
Pull Request — master (#3)
by Amine
02:55
created

src/operators.php (8 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Tarsana\Functional;
2
3
/**
4
 * This file contains operators as functions.
5
 * @file
6
 */
7
8
/**
9
 * Returns `$a && $b`.
10
 *
11
 * ```php
12
 * $isTrue = F\and_(true);
13
 * $isTrue(false); //=> false
14
 * $isTrue(true); //=> true
15
 * ```
16
 *
17
 * @stream
18
 * @signature Boolean -> Boolean -> Boolean
19
 * @param  bool $a
20
 * @param  bool $b
21
 * @return bool
22
 */
23 View Code Duplication
function and_() {
24
    static $and = false;
25
    $and = $and ?: curry(function($a, $b){
26
        return $a && $b;
27
    });
28
    return _apply($and, func_get_args());
29
}
30
31
/**
32
 * Returns `$a || $b`.
33
 *
34
 * ```php
35
 * $isTrue = F\or_(false);
36
 * $isTrue(false); //=> false
37
 * $isTrue(true); //=> true
38
 * ```
39
 *
40
 * @stream
41
 * @signature Boolean -> Boolean -> Boolean
42
 * @param  bool $a
43
 * @param  bool $b
44
 * @return bool
45
 */
46 View Code Duplication
function or_() {
47
    static $or = false;
48
    $or = $or ?: curry(function($a, $b){
49
        return $a || $b;
50
    });
51
    return _apply($or, func_get_args());
52
}
53
54
/**
55
 * Returns `!$x`.
56
 *
57
 * ```php
58
 * F\map(F\not(), [true, false, true]); //=> [false, true, false]
59
 * ```
60
 *
61
 * @stream
62
 * @signature Boolean -> Boolean
63
 * @param  bool $x
64
 * @return bool
65
 */
66 View Code Duplication
function not() {
67
    static $not = false;
68
    $not = $not ?: curry(function($x) {
69
        return !$x;
70
    });
71
    return _apply($not, func_get_args());
72
}
73
74
/**
75
 * Returns `$x == $y`.
76
 *
77
 * ```php
78
 * F\eq('10', 10); //=> true
79
 * ```
80
 *
81
 * @stream
82
 * @signature * -> * -> Boolean
83
 * @param  mixed $a
84
 * @param  mixed $b
85
 * @return bool
86
 */
87
function eq() {
88
    $eq = curry(function($a, $b){
89
        return $a == $b;
90
    });
91
    return _apply($eq, func_get_args());
92
}
93
94
/**
95
 * Returns `$x != $y`.
96
 *
97
 * ```php
98
 * F\notEq('Hi', 'Hello'); //=> true
99
 * ```
100
 *
101
 * @stream
102
 * @signature * -> * -> Boolean
103
 * @param  mixed $a
104
 * @param  mixed $b
105
 * @return bool
106
 */
107 View Code Duplication
function notEq() {
108
    static $notEq = false;
109
    $notEq = $notEq ?: curry(function($a, $b){
110
        return $a != $b;
111
    });
112
    return _apply($notEq, func_get_args());
113
}
114
115
/**
116
 * Returns `$x === $y`.
117
 *
118
 * ```php
119
 * F\eqq(10, '10'); //=> false
120
 * ```
121
 * @stream
122
 * @signature * -> * -> Boolean
123
 * @param  mixed $a
124
 * @param  mixed $b
125
 * @return bool
126
 */
127 View Code Duplication
function eqq() {
128
    static $eqq = false;
129
    $eqq = $eqq ?: curry(function($a, $b){
130
        return $a === $b;
131
    });
132
    return _apply($eqq, func_get_args());
133
}
134
135
/**
136
 * Returns `$x !== $y`.
137
 *
138
 * ```php
139
 * F\notEqq(10, '10'); //=> true
140
 * ```
141
 *
142
 * @stream
143
 * @signature * -> * -> Boolean
144
 * @param  mixed $a
145
 * @param  mixed $b
146
 * @return bool
147
 */
148 View Code Duplication
function notEqq() {
149
    static $notEqq = false;
150
    $notEqq = $notEqq ?: curry(function($a, $b){
151
        return $a !== $b;
152
    });
153
    return _apply($notEqq, func_get_args());
154
}
155
156
/**
157
 * Returns `true` if the two elements have the same type and are deeply equivalent.
158
 *
159
 * ```php
160
 * $a = (object) ['a' => 1, 'b' => (object) ['c' => 'Hello'], 'd' => false];
161
 * $b = (object) ['a' => 1, 'b' => (object) ['c' => 'Hi'], 'd' => false];
162
 * $c = (object) ['a' => 1, 'b' => ['c' => 'Hello'], 'd' => false];
163
 * // should have the same type
164
 * F\equals(5, '5'); //=> false
165
 * F\equals([1, 2, 3], [1, 2, 3]); //=> true
166
 * // should have the same order
167
 * F\equals([1, 3, 2], [1, 2, 3]); //=> false
168
 * F\equals($a, $b); //=> false
169
 * F\equals($a, $c); //=> false
170
 * $b->b->c = 'Hello';
171
 * F\equals($a, $b); //=> true
172
 * ```
173
 *
174
 * @stream
175
 * @signature * -> * -> Boolean
176
 * @param  mixed $a
177
 * @param  mixed $b
178
 * @return bool
179
 */
180
function equals() {
181
    static $equals = false;
182
    $equals = $equals ?: curry(_f('_equals'));
183
    return _apply($equals, func_get_args());
184
}
185
function _equals($a, $b) {
186
    $type = type($a);
187
    if ($type != type($b))
188
        return false;
189
    switch ($type) {
190
        case 'List':
191
            $length = count($a);
192
            if (count($b) != $length)
193
                return false;
194
            $index = 0;
195
            while ($index < $length) {
196
                if (!_equals($a[$index], $b[$index]))
197
                    return false;
198
                $index ++;
199
            }
200
            return true;
201
        case 'Array':
202
        case 'ArrayObject':
203
        case 'Object':
204
            $keysA = keys($a);
205
            $keysB = keys($b);
206
            $length = count($keysA);
207
            if (count($keysB) != $length)
208
                return false;
209
            $index = 0;
210
            while ($index < $length) {
211
                if (!_equals($keysA[$index], $keysB[$index]))
212
                    return false;
213
                if (!_equals(get($keysA[$index], $a), get($keysB[$index], $b)))
214
                    return false;
215
                $index ++;
216
            }
217
            return true;
218
        default:
219
            return $a == $b;
220
    }
221
}
222
223
/**
224
 * Returns `true` if the results of applying `$fn` to `$a` and `$b` are deeply equal.
225
 *
226
 * ```php
227
 * $headEquals = F\equalBy(F\head());
228
 * $headEquals([1, 2], [1, 3]); //=> true
229
 * $headEquals([3, 2], [1, 3]); //=> false
230
 *
231
 * $sameAge = F\equalBy(F\get('age'));
232
 * $foo = ['name' => 'foo', 'age' => 11];
233
 * $bar = ['name' => 'bar', 'age' => 13];
234
 * $baz = ['name' => 'baz', 'age' => 11];
235
 * $sameAge($foo, $bar); //=> false
236
 * $sameAge($foo, $baz); //=> true
237
 * ```
238
 *
239
 * @stream
240
 * @signature (a -> b) -> a -> a -> Boolean
241
 * @return [type] [description]
242
 */
243 View Code Duplication
function equalBy() {
244
    static $equalBy = false;
245
    $equalBy = $equalBy ?: curry(function($fn, $a, $b) {
246
        return _equals($fn($a), $fn($b));
247
    });
248
    return _apply($equalBy, func_get_args());
249
}
250
251
/**
252
 * Returns `$a < $b`.
253
 *
254
 * ```php
255
 * F\lt(3, 5); //=> true
256
 * F\lt(5, 5); //=> false
257
 * ```
258
 *
259
 * @stream
260
 * @signature * -> * -> Boolean
261
 * @param  mixed $a
0 ignored issues
show
There is no parameter named $a. 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 $b
0 ignored issues
show
There is no parameter named $b. 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 View Code Duplication
function lt() {
266
    static $lt = false;
267
    $lt = $lt ?: curry(function($a, $b){
268
        return $a < $b;
269
    });
270
    return _apply($lt, func_get_args());
271
}
272
273
/**
274
 * Returns `$a <= $b`.
275
 *
276
 * ```php
277
 * F\lte(3, 5); //=> true
278
 * F\lte(5, 5); //=> true
279
 * ```
280
 *
281
 * @stream
282
 * @signature * -> * -> Boolean
283
 * @param  mixed $a
0 ignored issues
show
There is no parameter named $a. 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...
284
 * @param  mixed $b
0 ignored issues
show
There is no parameter named $b. 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...
285
 * @return bool
286
 */
287 View Code Duplication
function lte() {
288
    static $lte = false;
289
    $lte = $lte ?: curry(function($a, $b){
290
        return $a <= $b;
291
    });
292
    return _apply($lte, func_get_args());
293
}
294
295
/**
296
 * Returns `$a > $b`.
297
 *
298
 * ```php
299
 * F\gt(5, 3); //=> true
300
 * F\gt(5, 5); //=> false
301
 * ```
302
 *
303
 * @stream
304
 * @signature * -> * -> Boolean
305
 * @param  mixed $a
0 ignored issues
show
There is no parameter named $a. 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...
306
 * @param  mixed $b
0 ignored issues
show
There is no parameter named $b. 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...
307
 * @return bool
308
 */
309 View Code Duplication
function gt() {
310
    static $gt = false;
311
    $gt = $gt ?: curry(function($a, $b){
312
        return $a > $b;
313
    });
314
    return _apply($gt, func_get_args());
315
}
316
317
/**
318
 * Returns `$a >= $b`.
319
 *
320
 * ```php
321
 * F\gte(5, 3); //=> true
322
 * F\gte(5, 5); //=> true
323
 * ```
324
 *
325
 * @stream
326
 * @signature * -> * -> Boolean
327
 * @param  mixed $a
0 ignored issues
show
There is no parameter named $a. 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...
328
 * @param  mixed $b
0 ignored issues
show
There is no parameter named $b. 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...
329
 * @return bool
330
 */
331 View Code Duplication
function gte() {
332
    static $gte = false;
333
    $gte = $gte ?: curry(function($a, $b){
334
        return $a >= $b;
335
    });
336
    return _apply($gte, func_get_args());
337
}
338
339