Passed
Push — master ( 348fca...23f099 )
by Smoren
02:12
created

UniqueExtractor::getString()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 26
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 12
eloc 24
c 1
b 0
f 1
nc 12
nop 2
dl 0
loc 26
ccs 23
cts 23
cp 1
crap 12
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\PartialIntersection\Util;
6
7
use Closure;
8
use Generator;
9
10
/**
11
 * Tool for extracting unique IDs and hashes of any PHP variables and data structures.
12
 *
13
 * Based on PHP Type Tool's UniqueExtractor.
14
 * @see https://github.com/Smoren/type-tools-php
15
 * @see https://github.com/Smoren/type-tools-php/blob/master/src/UniqueExtractor.php
16
 */
17
class UniqueExtractor
18
{
19
    /**
20
     * Returns unique ID string of given variable by its value and type.
21
     *
22
     * If $strict is true:
23
     *  - scalars: result is unique strictly by type;
24
     *  - objects: result is unique by instance;
25
     *  - arrays: result is unique by serialized value;
26
     *  - resources: result is unique by instance.
27
     *
28
     * If $strict is false:
29
     *  - scalars: result is unique by value;
30
     *  - objects: result is unique by serialized value;
31
     *  - arrays: result is unique by serialized value.
32
     *  - resources: result is unique by instance.
33
     *
34
     * @param mixed $var
35
     * @param bool $strict
36
     *
37
     * @return string
38
     */
39 296
    public static function getString($var, bool $strict): string
40
    {
41
        switch (true) {
42 296
            case is_array($var):
43 32
                return 'array_'.serialize($var);
44 264
            case is_resource($var):
45 32
                preg_match('/#([0-9]+)$/', (string)$var, $matches);
46 32
                return 'resource_'.$matches[1];
47 232
            case $var instanceof Generator:
48 32
                return 'generator_'.spl_object_id($var);
49 200
            case $var instanceof Closure:
50 32
                return 'closure_'.spl_object_id($var);
51 168
            case is_object($var):
52 32
                return 'object_'.($strict ? spl_object_id($var) : serialize($var));
53 136
            case gettype($var) === 'boolean':
54 46
                return 'boolean_'.(int)$var;
55 96
            case $strict:
56 48
                return gettype($var).'_'.$var;
57 48
            case !$var:
58 3
                return 'boolean_0';
59 48
            case strval($var) === '1':
60 36
                return 'boolean_1';
61 48
            case is_numeric($var):
62 48
                return 'numeric_'.(float)$var;
63
            default:
64 3
                return 'scalar_'.$var;
65
        }
66
    }
67
}
68