Debian Codegraph @

vendor/ramsey/collection/src/AbstractSet.php

raw
1 <?php
2
3 /**
4 * This file is part of the ramsey/collection library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Collection;
16
17 /**
18 * This class contains the basic implementation of a collection that does not
19 * allow duplicated values (a set), to minimize the effort required to implement
20 * this specific type of collection.
21 *
22 * @template T
23 * @extends AbstractCollection<T>
24 */
25 abstract class AbstractSet extends AbstractCollection
26 {
27 public function add(mixed $element): bool
28 {
29 if ($this->contains($element)) {
30 return false;
31 }
32
33 // Call offsetSet() on the parent instead of add(), since calling
34 // parent::add() will invoke $this->offsetSet(), which will call
35 // $this->contains() a second time. This can cause performance issues
36 // with extremely large collections. For more information, see
37 // https://github.com/ramsey/collection/issues/68.
38 parent::offsetSet(null, $element);
39
40 return true;
41 }
42
43 public function offsetSet(mixed $offset, mixed $value): void
44 {
45 if ($this->contains($value)) {
46 return;
47 }
48
49 parent::offsetSet($offset, $value);
50 }
51 }