Completed
Push — master ( 5a66ad...309b32 )
by Amine
01:58
created

common.php ➔ type()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 26
nc 1
nop 0
dl 0
loc 29
rs 5.1234
c 0
b 0
f 0

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 namespace Tarsana\Functional;
2
3
/**
4
 * This file contains generic common functions.
5
 */
6
7
/**
8
 * Gets the type of the given argument.
9
 * ```php
10
 * type(null); // 'Null'
11
 * type(true); // 'Boolean'
12
 * type(false); // 'Boolean'
13
 * type('Hello World'); // 'String'
14
 * type(1234); // 'Number'
15
 * type('123'); // 'String'
16
 * type(function($x) {return $x;}); // 'Function'
17
 * type(new \stdClass); // 'Object'
18
 * type(['name' => 'Foo', 'age' => 21]); // 'Array'
19
 * type(['Hello', 'World', 123, true]); // 'List'
20
 * type(['name' => 'Foo', 'Hello', 'Mixed']); // 'Array'
21
 * type(fopen('php://temp')); // 'Resource'
22
 * type(Error::of('Ooops !')); // 'Error'
23
 * // Anything else is 'Unknown'
24
 * ```
25
 *
26
 * @signature * -> String
27
 * @param  mixed $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. 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...
28
 * @return string
29
 */
30
function type() {
31
    $type = function($data) {
32
        if ($data instanceof Error) return 'Error';
33
        if ($data instanceof Stream) return "Stream({$data->type})";
0 ignored issues
show
Bug introduced by
The property type cannot be accessed from this context as it is declared protected in class Tarsana\Functional\Stream.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
34
        if (is_callable($data)) return 'Function';
35
        switch (gettype($data)) {
36
            case 'boolean':
37
                return 'Boolean';
38
            case 'NULL':
39
                return 'Null';
40
            case 'integer':
41
            case 'double':
42
                return 'Number';
43
            case 'string':
44
                return 'String';
45
            case 'resource':
46
                return 'Resource';
47
            case 'array':
48
                if (allSatisfies('is_numeric', keys($data)))
49
                    return 'List';
50
                return 'Array';
51
            case 'object':
52
                return 'Object';
53
            default:
54
                return 'Unknown';
55
        }
56
    };
57
    return apply(curry($type), func_get_args());
58
}
59
60
/**
61
 * Converts a variable to its string value.
62
 * ```php
63
 * toString(53)); // '53'
64
 * toString(true)); // 'true'
65
 * toString(false)); // 'false'
66
 * toString(null)); // 'null'
67
 * toString('Hello World')); // '"Hello World"'
68
 * toString([])); // '[]'
69
 * toString(new \stdClass)); // '{}'
70
 * toString(function(){})); // '[Function]'
71
 * toString(Error::of('Ooops'))); // '[Error: Ooops]'
72
 * toString(fopen('php://temp', 'r'))); // '[Resource]'
73
 * toString(['hi', 'hello', 'yo'])); // '["hi", "hello", "yo"]'
74
 * toString([
75
 *     'object' => Stream::of(null),
76
 *     'numbers' => [1, 2, 3],
77
 *     'message'
78
 * ]); // '{object: Stream(Null), numbers: [1, 2, 3], 0: "message"]'
79
 * ```
80
 *
81
 * @signature * -> String
82
 * @param  mixed $something
0 ignored issues
show
Bug introduced by
There is no parameter named $something. 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...
83
 * @return string
84
 */
85
function toString () {
86
    $toString = function($something) {
87
        switch (type($something)) {
88
            case 'String':
89
                return "\"{$something}\"";
90
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
91
            case 'Boolean':
92
                return $something ? 'true' : 'false';
93
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
94
            case 'Null':
95
                return 'null';
96
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
97
            case 'Number':
98
                return (string) $something;
99
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
100
            case 'List':
101
                return '[' . join(', ', map(toString(), $something)) . ']';
102
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
103
            case 'Error':
104
            case 'Stream':
105
                return $something->__toString();
106
            case 'Object':
107
            case 'Array':
108
                return '{' . join(', ', map(function($pair){
109
                    return $pair[0].': '. toString($pair[1]);
110
                }, toPairs($something))) . '}';
111
            default:
112
                return '['.type($something).']';
113
        }
114
    };
115
    return apply(curry($toString), func_get_args());
116
}
117
118
/**
119
 * Creates a `Stream` containing the provided data.
120
 * ```php
121
 * s('! World Hello')
122
 *     ->then(split(' '))
123
 *     ->then('array_reverse')
124
 *     ->then(join(' '))
125
 *     ->get(); // 'Hello World !'
126
 * ```
127
 *
128
 * @signature a -> Stream(a)
129
 * @param  mixed $data
130
 * @return Stream
131
 */
132
function s($data) {
133
    return Stream::of($data);
134
}
135