memoize.php ➔ memoize()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace DaveRoss\FunctionalProgrammingUtils;
4
5
/**
6
 * Memoize a function
7
 *
8
 * @param callable $f function to memoize
9
 *
10
 * @return \Closure
11
 */
12
function memoize(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...
13
{
14
    return function () use ($f) {
15
        static $memoize = array();
16
        $memoize_key = md5(json_encode(func_get_args()));
0 ignored issues
show
Coding Style introduced by
$memoize_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
        if (isset($memoize[ $memoize_key ])) {
0 ignored issues
show
Coding Style introduced by
$memoize_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
            return $memoize[ $memoize_key ];
0 ignored issues
show
Coding Style introduced by
$memoize_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
        }
20
21
        return $memoize[ $memoize_key ] = call_user_func_array($f, func_get_args());
0 ignored issues
show
Coding Style introduced by
$memoize_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
22
    };
23
}
24