Completed
Push — master ( 5c4201...24a9b5 )
by Siwapun
05:35
created

ifElse()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 9.7998
c 0
b 0
f 0
1
<?php
2
namespace Aerophant\Ramda;
3
4
/**
5
 * @param bool $firstValue
6
 * @param bool $secondValue
7
 * @return bool|\Closure
8
 */
9
function andLogically()
10
{
11
  $and = function (bool $firstValue, bool $secondValue) {
12 2
    return $firstValue && $secondValue;
13 2
  };
14 2
  $arguments = func_get_args();
15 2
  $curried = curryN($and, 2);
16 2
  return call_user_func_array($curried, $arguments);
17
}
18
19
function defaultTo()
20
{
21
  $defaultTo = function ($defaultValue, $value) {
22 2
    return $value ? $value : $defaultValue;
23 2
  };
24 2
  $arguments = func_get_args();
25 2
  $curried = curryN($defaultTo, 2);
26 2
  return call_user_func_array($curried, $arguments);
27
}
28
29
/**
30
 * (*… → Boolean) → (*… → *) → (*… → *) → (*… → *)
31
 * @param callable $condition
32
 * @param callable $onTrue
33
 * @param callable $onFalse
34
 * @return \Closure
35
 */
36
function ifElse()
37
{
38
  $ifElse = function (callable $condition, callable $onTrue, callable $onFalse) {
39
    return function () use ($condition, $onTrue, $onFalse) {
40 2
      $arguments = func_get_args();
41 2
      if (call_user_func_array($condition, $arguments)) {
42 1
        return call_user_func_array($onTrue, $arguments);
43
      }
44 1
      return call_user_func_array($onFalse, $arguments);
45 2
    };
46 2
  };
47 2
  $arguments = func_get_args();
48 2
  $curried = curryN($ifElse, 3);
49 2
  return call_user_func_array($curried, $arguments);
50
}
51
52
/**
53
 * @param mixed $data
54
 * @return bool|\Closure
55
 */
56
function isEmpty()
57
{
58
  $isEmpty = function ($value) {
59 4
    return empty($value);
60 4
  };
61 4
  $arguments = func_get_args();
62 4
  $curried = curryN($isEmpty, 1);
63 4
  return call_user_func_array($curried, $arguments);
64
}
65
66
/**
67
 * @param bool $firstValue
68
 * @param bool $secondValue
69
 * @return bool|\Closure
70
 */
71
function orLogically()
72
{
73
  $and = function (bool $firstValue, bool $secondValue) {
74 2
    return $firstValue || $secondValue;
75 2
  };
76 2
  $arguments = func_get_args();
77 2
  $curried = curryN($and, 2);
78 2
  return call_user_func_array($curried, $arguments);
79
}
80