Total Complexity | 8 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
11 | final class TransactionMessage |
||
12 | { |
||
13 | /** @var array<string, array<string,SegmentInterface>> */ |
||
14 | private array $groupedSegments; |
||
15 | |||
16 | /** |
||
17 | * A message starts with the "UNHMessageHeader" segment until another |
||
18 | * "UNHMessageHeader" segment appears, then starts another segment and so on. |
||
19 | * |
||
20 | * @psalm-pure |
||
21 | * @psalm-return list<TransactionMessage> |
||
22 | */ |
||
23 | 6 | public static function groupSegmentsByMessage(SegmentInterface...$segments): array |
|
24 | { |
||
25 | 6 | $messages = []; |
|
26 | 6 | $groupedSegments = []; |
|
27 | |||
28 | 6 | foreach ($segments as $segment) { |
|
29 | 6 | if ($segment instanceof UNHMessageHeader && $groupedSegments) { |
|
|
|||
30 | 2 | $messages[] = self::groupSegmentsByName(...$groupedSegments); |
|
31 | 2 | $groupedSegments = []; |
|
32 | } |
||
33 | 6 | $groupedSegments[] = $segment; |
|
34 | } |
||
35 | |||
36 | 6 | $messages[] = self::groupSegmentsByName(...$groupedSegments); |
|
37 | |||
38 | 6 | return $messages; |
|
39 | } |
||
40 | |||
41 | /** @param array<string, array<string,SegmentInterface>> $groupedSegments */ |
||
42 | 6 | public function __construct(array $groupedSegments) |
|
45 | 6 | } |
|
46 | |||
47 | 2 | public function segmentByName(string $name): array |
|
48 | { |
||
49 | 2 | return $this->groupedSegments[$name] ?? []; |
|
50 | } |
||
51 | |||
52 | /** @psalm-pure */ |
||
53 | 6 | private static function groupSegmentsByName(SegmentInterface...$segments): self |
|
63 | } |
||
64 | } |
||
65 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.