Stream::type()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Tarsana\Functional;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 144.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Stream is a lazy data container.
5
 */
6
class Stream {
7
8
    /**
9
     * The list of predefined operations.
10
     *
11
     * @var array
12
     */
13
    protected static $operations = null;
14
15
    /**
16
     * The internal Stream structure described here: src/Internal/_stream.php
17
     *
18
     * @var array
19
     */
20
    protected $stream;
21
22
    /**
23
     * Loads the stream operations from the file src/Internal/_stream_operations.php
24
     * Executed when the first Stream instance is created.
25
     *
26
     * @return void
27
     */
28
    public static function init()
29
    {
30
        if (null === self::$operations) {
31
            self::$operations = require __DIR__ . '/../Internal/_stream_operations.php';
32
        }
33
    }
34
35
    /**
36
     * Creates a new Stream.
37
     *
38
     * @param  mixed $data
39
     * @return Tarsana\Functional\Stream
40
     */
41
    public static function of($data)
42
    {
43
        return new Stream(_stream(self::$operations, $data));
44
    }
45
46
    /**
47
     * Adds a new operation to the Stream class.
48
     *
49
     * @param  string $name
50
     * @param  string $signature
0 ignored issues
show
Documentation introduced by
There is no parameter named $signature. Did you maybe mean $signatures?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
51
     * @param  callable $fn
52
     * @return void
53
     */
54
    public static function operation($name, $signatures, $fn = null)
55
    {
56
        if (! is_array($signatures)) {
57
            $signatures = [$signatures];
58
        }
59
        foreach ($signatures as $signature) {
60
            self::$operations[] = _stream_operation($name, $signature, $fn);
61
        }
62
    }
63
64
    /**
65
     * Checks if the Stream class has an operation with the given name.
66
     *
67
     * @param  string  $name
68
     * @return boolean
69
     */
70
    public static function hasOperation($name)
71
    {
72
        return contains($name, map(get('name'), self::$operations));
73
    }
74
75
    /**
76
     * Removes one or many operation from the Stream class.
77
     *
78
     * @param  string $name
79
     * @return void
80
     */
81
    public static function removeOperations($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $names = func_get_args();
84
        self::$operations = filter(function($operation) use($names) {
85
            return !in_array(get('name', $operation), $names);
86
        }, self::$operations);
87
    }
88
89
    /**
90
     * Creates a new Stream with some data.
91
     *
92
     * @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...
93
     */
94
    protected function __construct($stream)
95
    {
96
        $this->stream = $stream;
97
    }
98
99
    /**
100
     * Returns the type of contained data in the stream.
101
     *
102
     * @return string
103
     */
104
    public function type()
105
    {
106
        return get('type', $this->stream);
107
    }
108
109
    /**
110
     * Apply all the transformations in the stream and returns the result.
111
     *
112
     * @return mixed
113
     * @throws Tarsana\Functional\Error
114
     */
115
    public function result()
116
    {
117
        return get('result', _stream_resolve($this->stream));
118
    }
119
120
    /**
121
     * Adds a new transformation to the stream.
122
     *
123
     * @param  string $name The name of the operation
124
     * @param  array  $args
125
     * @return Tarsana\Functional\Stream
126
     */
127
    public function __call($name, $args)
128
    {
129
        return new Stream(_stream_apply_operation($name, $args, $this->stream));
130
    }
131
132
    /**
133
     * Returns the string representation of the stream.
134
     *
135
     * @return string
136
     */
137
    public function __toString()
138
    {
139
        return "[Stream of {$this->type()}]";
140
    }
141
142
}
143
144
Stream::init();
145