Issues (2)

src/EdifactParser.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser;
6
7
use EDI\Parser;
8
use EdifactParser\Exception\InvalidFile;
9
use EdifactParser\Segments\SegmentFactory;
10
use EdifactParser\Segments\SegmentFactoryInterface;
11
12
/** @psalm-immutable */
13
final class EdifactParser
14
{
15
    private SegmentFactoryInterface $segmentFactory;
16
17 3
    public static function createWithDefaultSegments(): self
18
    {
19 3
        return new self(SegmentFactory::withDefaultSegments());
20
    }
21
22 4
    public function __construct(SegmentFactoryInterface $segmentFactory)
23
    {
24 4
        $this->segmentFactory = $segmentFactory;
25 4
    }
26
27
    /** @codeCoverageIgnore */
28
    private function __clone()
29
    {
30
    }
31
32
    /** @return TransactionMessage[] */
33 4
    public function parse(string $fileContent): array
34
    {
35 4
        $parser = new Parser($fileContent);
36 4
        $errors = $parser->errors();
37
38 4
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
39 1
            throw InvalidFile::withErrors($errors);
40
        }
41
42 3
        $segmentList = new SegmentList($this->segmentFactory);
43 3
        $segments = $segmentList->fromRaw($parser->get());
44
45 3
        return TransactionMessage::groupSegmentsByMessage(...$segments);
46
    }
47
}
48