GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( cad621...d8b57f )
by Baptiste
04:31
created

Stream::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\{
7
    LogicException,
8
    GroupEmptySequenceException,
9
    InvalidArgumentException
10
};
11
12
/**
13
 * {@inheritdoc}
14
 */
15
class Stream implements StreamInterface
16
{
17
    use Type;
18
19
    private $type;
20
    private $spec;
21
    private $values;
22
23 50
    public function __construct(string $type)
24
    {
25 50
        $this->type = new Str($type);
26 50
        $this->spec = $this->getSpecFor($type);
27 50
        $this->values = new Sequence;
28 50
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 26
    public function type(): Str
34
    {
35 26
        return $this->type;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function size(): int
42
    {
43 3
        return $this->values->size();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function count(): int
50
    {
51 2
        return $this->values->size();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 19
    public function toPrimitive()
58
    {
59 19
        return $this->values->toPrimitive();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 7
    public function current()
66
    {
67 7
        return $this->values->current();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 5
    public function key()
74
    {
75 5
        return $this->values->key();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 7
    public function next()
82
    {
83 7
        $this->values->next();
84 7
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 7
    public function rewind()
90
    {
91 7
        $this->values->rewind();
92 7
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 7
    public function valid()
98
    {
99 7
        return $this->values->valid();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function offsetExists($offset): bool
106
    {
107 1
        return $this->values->offsetExists($offset);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 5
    public function offsetGet($offset)
114
    {
115 5
        return $this->get($offset);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function offsetSet($offset, $value)
122
    {
123 1
        throw new LogicException('You can\'t modify a stream');
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1
    public function offsetUnset($offset)
130
    {
131 1
        throw new LogicException('You can\'t modify a stream');
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 7
    public function get(int $index)
138
    {
139 7
        return $this->values->get($index);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function diff(StreamInterface $stream): StreamInterface
146
    {
147 1
        $this->validate($stream);
148
149 1
        $newStream = clone $this;
150 1
        $newStream->values = $this->values->diff(
151 1
            new Sequence(...$stream)
152
        );
153
154 1
        return $newStream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $newStream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::diff of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1
    public function distinct(): StreamInterface
161
    {
162 1
        $stream = clone $this;
163 1
        $stream->values = $this->values->distinct();
164
165 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::distinct of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 1
    public function drop(int $size): StreamInterface
172
    {
173 1
        $stream = clone $this;
174 1
        $stream->values = $this->values->drop($size);
175
176 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::drop of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 1
    public function dropEnd(int $size): StreamInterface
183
    {
184 1
        $stream = clone $this;
185 1
        $stream->values = $this->values->dropEnd($size);
186
187 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::dropEnd of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 2
    public function equals(StreamInterface $stream): bool
194
    {
195 2
        $this->validate($stream);
196
197 1
        return $this->values->equals(
198 1
            new Sequence(...$stream)
199
        );
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 1
    public function filter(callable $predicate): StreamInterface
206
    {
207 1
        $stream = clone $this;
208 1
        $stream->values = $this->values->filter($predicate);
209
210 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::filter of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function foreach(callable $function): StreamInterface
217
    {
218 1
        $this->values->foreach($function);
219
220 1
        return $this;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::foreach of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 2 View Code Duplication
    public function groupBy(callable $discriminator): MapInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228 2
        if ($this->size() === 0) {
229 1
            throw new GroupEmptySequenceException;
230
        }
231
232 1
        $map = null;
233
234 1
        foreach ($this->values as $value) {
235 1
            $key = $discriminator($value);
236
237 1
            if ($map === null) {
238 1
                $type = gettype($key);
239 1
                $map = new Map(
240 1
                    $type === 'object' ? get_class($key) : $type,
241 1
                    StreamInterface::class
242
                );
243
            }
244
245 1
            if ($map->contains($key)) {
246 1
                $map = $map->put(
247
                    $key,
248 1
                    $map->get($key)->add($value)
249
                );
250
            } else {
251 1
                $map = $map->put(
252
                    $key,
253 1
                    (new self((string) $this->type))->add($value)
254
                );
255
            }
256
        }
257
258 1
        return $map;
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 2
    public function first()
265
    {
266 2
        return $this->values->first();
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 2
    public function last()
273
    {
274 2
        return $this->values->last();
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280 1
    public function contains($element): bool
281
    {
282 1
        return $this->values->contains($element);
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288 1
    public function indexOf($element): int
289
    {
290 1
        return $this->values->indexOf($element);
291
    }
292
293
    /**
294
     * {@inheritdoc}
295
     */
296 1
    public function indices(): StreamInterface
297
    {
298 1
        $stream = new self('int');
299 1
        $stream->values = $this->values->indices();
300
301 1
        return $stream;
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307 3
    public function map(callable $function): StreamInterface
308
    {
309
        return $this
310 3
            ->values
311 3
            ->reduce(
312 3
                new self((string) $this->type),
313 3
                function(self $carry, $element) use($function): StreamInterface {
314 3
                    return $carry->add($function($element));
315 3
                }
316
            );
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322 2 View Code Duplication
    public function pad(int $size, $element): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
323
    {
324 2
        $this->spec->validate($element);
325
326 1
        $stream = clone $this;
327 1
        $stream->values = $this->values->pad($size, $element);
328
329 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::pad of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335 1
    public function partition(callable $predicate): MapInterface
336
    {
337 1
        $truthy = new self((string) $this->type);
338 1
        $falsy = new self((string) $this->type);
339
340 1
        foreach ($this->values as $value) {
341 1
            if ($predicate($value) === true) {
342 1
                $truthy = $truthy->add($value);
343
            } else {
344 1
                $falsy = $falsy->add($value);
345
            }
346
        }
347
348 1
        return (new Map('bool', StreamInterface::class))
349 1
            ->put(true, $truthy)
350 1
            ->put(false, $falsy);
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356 1
    public function slice(int $from, int $until): StreamInterface
357
    {
358 1
        $stream = clone $this;
359 1
        $stream->values = $this->values->slice($from, $until);
360
361 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::slice of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367 1
    public function splitAt(int $position): StreamInterface
368
    {
369 1
        $stream = new self(StreamInterface::class);
370 1
        $splitted = $this->values->splitAt($position);
371 1
        $first = new self((string) $this->type);
372 1
        $second = new self((string) $this->type);
373 1
        $first->values = $splitted->first();
374 1
        $second->values = $splitted->last();
375
376 1
        return $stream->add($first)->add($second);
2 ignored issues
show
Documentation introduced by
$first is of type object<Innmind\Immutable\Stream>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$second is of type object<Innmind\Immutable\Stream>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
377
    }
378
379
    /**
380
     * {@inheritdoc}
381
     */
382 1
    public function take(int $size): StreamInterface
383
    {
384 1
        $stream = clone $this;
385 1
        $stream->values = $this->values->take($size);
386
387 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::take of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
388
    }
389
390
    /**
391
     * {@inheritdoc}
392
     */
393 1
    public function takeEnd(int $size): StreamInterface
394
    {
395 1
        $stream = clone $this;
396 1
        $stream->values = $this->values->takeEnd($size);
397
398 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::takeEnd of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     */
404 2 View Code Duplication
    public function append(StreamInterface $stream): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
405
    {
406 2
        $this->validate($stream);
407
408 1
        $self = clone $this;
409 1
        $self->values = $this->values->append(
410 1
            new Sequence(...$stream)
411
        );
412
413 1
        return $self;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $self; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::append of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
414
    }
415
416
    /**
417
     * {@inheritdoc}
418
     */
419 2 View Code Duplication
    public function intersect(StreamInterface $stream): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
420
    {
421 2
        $this->validate($stream);
422
423 1
        $self = clone $this;
424 1
        $self->values = $this->values->intersect(
425 1
            new Sequence(...$stream)
426
        );
427
428 1
        return $self;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $self; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::intersect of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
429
    }
430
431
    /**
432
     * {@inheritdoc}
433
     */
434 2
    public function join(string $separator): Str
435
    {
436 2
        return new Str((string) $this->values->join($separator));
437
    }
438
439
    /**
440
     * {@inheritdoc}
441
     */
442 40 View Code Duplication
    public function add($element): StreamInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
443
    {
444 40
        $this->spec->validate($element);
445
446 39
        $stream = clone $this;
447 39
        $stream->values = $this->values->add($element);
448
449 39
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::add of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
450
    }
451
452
    /**
453
     * {@inheritdoc}
454
     */
455 1
    public function sort(callable $function): StreamInterface
456
    {
457 1
        $stream = clone $this;
458 1
        $stream->values = $this->values->sort($function);
459
460 1
        return $stream;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $stream; (Innmind\Immutable\Stream) is incompatible with the return type declared by the interface Innmind\Immutable\StreamInterface::sort of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
461
    }
462
463
    /**
464
     * {@inheritdoc}
465
     */
466 1
    public function reduce($carry, callable $reducer)
467
    {
468 1
        return $this->values->reduce($carry, $reducer);
469
    }
470
471
    /**
472
     * Make sure the stream is compatible with the current one
473
     *
474
     * @param StreamInterface $stream
475
     *
476
     * @throws InvalidArgumentException
477
     *
478
     * @return void
479
     */
480 7
    private function validate(StreamInterface $stream)
481
    {
482 7
        if (!$stream->type()->equals($this->type)) {
0 ignored issues
show
Documentation introduced by
$this->type is of type object<Innmind\Immutable\Str>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
483 3
            throw new InvalidArgumentException(
484 3
                'The 2 streams does not reference the same type'
485
            );
486
        }
487 4
    }
488
}
489