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

Set::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\InvalidArgumentException;
7
8
class Set implements SetInterface
9
{
10
    use Type;
11
12
    private $type;
13
    private $spec;
14
    private $values;
15
16 24
    public function __construct(string $type)
17
    {
18 24
        $this->type = new StringPrimitive($type);
0 ignored issues
show
Deprecated Code introduced by
The class Innmind\Immutable\StringPrimitive has been deprecated with message: To be removed in 2.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19 24
        $this->spec = $this->getSpecFor($type);
20 24
        $this->values = new Sequence;
21 24
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 13
    public function type(): StringPrimitive
27
    {
28 13
        return $this->type;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function size(): int
35
    {
36 2
        return $this->values->size();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function count(): int
43
    {
44 1
        return $this->values->size();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 13
    public function toPrimitive()
51
    {
52 13
        return $this->values->toPrimitive();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function current()
59
    {
60 1
        return $this->values->current();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function key()
67
    {
68 1
        return $this->values->key();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function next()
75
    {
76 1
        $this->values->next();
77 1
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function rewind()
83
    {
84 1
        $this->values->rewind();
85 1
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function valid()
91
    {
92 1
        return $this->values->valid();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 2 View Code Duplication
    public function intersect(SetInterface $set): SetInterface
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...
99
    {
100 2
        $this->validate($set);
101
102 1
        $newSet = clone $this;
103 1
        $newSet->values = $this->values->intersect(
104 1
            new Sequence(...$set->toPrimitive())
105
        );
106
107 1
        return $newSet;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $newSet; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::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...
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 19
    public function add($element): SetInterface
114
    {
115 19
        $this->spec->validate($element);
116
117 18
        if ($this->contains($element)) {
118 2
            return $this;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::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...
119
        }
120
121 18
        $set = clone $this;
122 18
        $set->values = $this->values->add($element);
123
124 18
        return $set;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $set; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::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...
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 18
    public function contains($element): bool
131
    {
132 18
        return $this->values->contains($element);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function remove($element): SetInterface
139
    {
140 1
        if (!$this->contains($element)) {
141
            return $this;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::remove 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...
142
        }
143
144 1
        $index = $this->values->indexOf($element);
145 1
        $set = clone $this;
146 1
        $set->values = (new Sequence)
147 1
            ->append($this->values->slice(0, $index))
148 1
            ->append($this->values->slice($index + 1, $this->size()));
149
150 1
        return $set;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $set; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::remove 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...
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 2 View Code Duplication
    public function diff(SetInterface $set): SetInterface
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...
157
    {
158 2
        $this->validate($set);
159
160 1
        $newSet = clone $this;
161 1
        $newSet->values = $this->values->diff(
162 1
            new Sequence(...$set->toPrimitive())
163
        );
164
165 1
        return $newSet;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $newSet; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::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...
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 3
    public function equals(SetInterface $set): bool
172
    {
173 3
        $this->validate($set);
174
175 2
        return $this->values->equals(
176 2
            new Sequence(...$set->toPrimitive())
177
        );
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function filter(callable $predicate): SetInterface
184
    {
185 1
        $set = clone $this;
186 1
        $set->values = $this->values->filter($predicate);
187
188 1
        return $set;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $set; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::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...
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function foreach(callable $function): SetInterface
195
    {
196 1
        $this->values->foreach($function);
197
198 1
        return $this;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this; (Innmind\Immutable\Set) is incompatible with the return type declared by the interface Innmind\Immutable\SetInterface::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...
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 1
    public function groupBy(callable $discriminator): MapInterface
205
    {
206 1
        return $this->values->groupBy($discriminator);
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 2
    public function map(callable $function): SetInterface
213
    {
214 2
        $set = clone $this;
215 2
        $set->values = new Sequence;
216
217 2
        return $this->reduce(
218
            $set,
219
            function(self $carry, $value) use ($function): self {
220 2
                return $carry->add($function($value));
221 2
            }
222
        );
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 1
    public function partition(callable $predicate): MapInterface
229
    {
230 1
        $truthy = clone $this;
231 1
        $falsy = clone $this;
232 1
        $partitions = $this->values->partition($predicate);
233 1
        $truthy->values = $partitions->get(0);
234 1
        $falsy->values = $partitions->get(1);
235
236 1
        return (new Map('bool', SetInterface::class))
237 1
            ->put(true, $truthy)
238 1
            ->put(false, $falsy);
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 1
    public function join(string $separator): StringPrimitive
245
    {
246 1
        return $this->values->join($separator);
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252 1
    public function sort(callable $function): SequenceInterface
253
    {
254 1
        return $this->values->sort($function);
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 2
    public function merge(SetInterface $set): SetInterface
261
    {
262 2
        $this->validate($set);
263
264 1
        return $set->reduce(
265 1
            clone $this,
266 1
            function(self $carry, $value): self {
267 1
                return $carry->add($value);
268 1
            }
269
        );
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275 4
    public function reduce($carry, callable $reducer)
276
    {
277 4
        return $this->values->reduce($carry, $reducer);
278
    }
279
280
    /**
281
     * Make sure the set is compatible with the current one
282
     *
283
     * @param SetInterface $set
284
     *
285
     * @throws InvalidArgumentException
286
     *
287
     * @return void
288
     */
289 8
    private function validate(SetInterface $set)
290
    {
291 8
        if (!$set->type()->equals($this->type)) {
292 4
            throw new InvalidArgumentException(
293 4
                'The 2 sets does not reference the same type'
294
            );
295
        }
296 4
    }
297
}
298