b2pweb /
bdf-collections
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\Collection\Stream\Collector; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Concatenates all elements into a string, with a separator, a prefix and a suffix |
||
| 7 | * |
||
| 8 | * <code> |
||
| 9 | * $stream = new ArrayStream([1, 2, 3]); |
||
| 10 | * $stream->collect(new Joining()); // '123' |
||
| 11 | * $stream->collect(new Joining(',')); // '1,2,3' |
||
| 12 | * $stream->collect(new Joining(',', '[', ']')); // '[1,2,3]' |
||
| 13 | * </code> |
||
| 14 | * |
||
| 15 | * @template V |
||
| 16 | * @template K |
||
| 17 | * @implements CollectorInterface<V, K, string> |
||
| 18 | */ |
||
| 19 | final class Joining implements CollectorInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $separator; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $prefix; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $suffix; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string|null |
||
| 38 | */ |
||
| 39 | private $aggregation = null; |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Joining constructor. |
||
| 44 | * |
||
| 45 | * @param string $separator The elements separator |
||
| 46 | * @param string $prefix The prefix |
||
| 47 | * @param string $suffix The suffix |
||
| 48 | */ |
||
| 49 | 18 | public function __construct(string $separator = '', string $prefix = '', string $suffix = '') |
|
| 50 | { |
||
| 51 | 18 | $this->separator = $separator; |
|
| 52 | 18 | $this->prefix = $prefix; |
|
| 53 | 18 | $this->suffix = $suffix; |
|
| 54 | 18 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 16 | public function aggregate($element, $key = null): void |
|
| 60 | { |
||
| 61 | 16 | if ($this->aggregation === null) { |
|
| 62 | 16 | $this->aggregation = (string) $element; |
|
| 63 | } else { |
||
| 64 | 15 | $this->aggregation .= $this->separator.$element; |
|
| 65 | } |
||
| 66 | 16 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | 18 | public function finalize(): string |
|
| 72 | { |
||
| 73 | 18 | return $this->prefix.(string)$this->aggregation.$this->suffix; |
|
|
0 ignored issues
–
show
|
|||
| 74 | } |
||
| 75 | } |
||
| 76 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: