Joining::finalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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
Bug Best Practice introduced by
The expression return $this->prefix . (...egation . $this->suffix returns the type string which is incompatible with the return type mandated by Bdf\Collection\Stream\Co...orInterface::finalize() of Bdf\Collection\Stream\Collector\R.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
74
    }
75
}
76