Maybe   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 4 2
A isNothing() 0 4 1
1
<?php
2
3
namespace DaveRoss\FunctionalProgrammingUtils;
4
5
/**
6
 * Class Maybe
7
 * @package DaveRoss\FunctionalProgrammingUtils
8
 */
9
class Maybe extends Monad
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
10
{
11
12
    /**
13
     * Apply a function to this Monad's value only if the value is not null
14
     *
15
     * @param callable $f
16
     *
17
     * @return Maybe
18
     */
19
    public function map(callable $f)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
20
    {
21
        return ( $this->isNothing() ) ? Maybe::of(null) : Maybe::of($f( $this->value ));
22
    }
23
24
    public function isNothing()
25
    {
26
        return ( null == $this->value );
27
    }
28
}
29
30
/**
31
 * Helper function to apply a function to a Maybe that is not Maybe(null). Returns the value directly rather
32
 * than wrapping it in another Maybe.
33
 *
34
 * @param mixed    $x default value
35
 * @param callable $f function to apply to a non-empty Maybe's value
36
 * @param Maybe    $m the Maybe to run the function against if the Maybe is not Maybe(null)
37
 *
38
 * @return mixed
39
 */
40
function maybe($x, callable $f, Maybe $m)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $x. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $m. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
41
{
42
    return ( $m->isNothing() ) ? $x : $f( $m->value() );
43
}