Completed
Push — master ( 310596...ecf895 )
by Hannes
02:21
created

Parser::parse()   B

Complexity

Conditions 5
Paths 14

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
ccs 0
cts 28
cp 0
rs 8.439
cc 5
eloc 21
nc 14
nop 1
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\autogiro\Parser;
6
7
use byrokrat\autogiro\Line;
8
use byrokrat\autogiro\Exception;
9
10
/**
11
 * Prase raw autogiro files
12
 */
13
class Parser
14
{
15
    /**
16
     * @var Strategy\Strategy
17
     */
18
    private $strategy;
19
20
    public function __construct(Strategy\Strategy $strategy)
21
    {
22
        $this->strategy = $strategy;
23
    }
24
25
    // TODO vilken typ av objekt ska parse returnera??
26
    public function parse(\SplFileObject $file)
27
    {
28
        $lineNumber = 'undefined';
29
30
        try {
31
            $states = $this->strategy->createStates();
32
            $this->strategy->begin();
33
34
            foreach ($file as $lineNumber => $line) {
35
                $line = new Line($line);
0 ignored issues
show
Bug introduced by
It seems like $line defined by new \byrokrat\autogiro\Line($line) on line 35 can also be of type array; however, byrokrat\autogiro\Line::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
37
                if ($line->isEmpty()) {
38
                    continue;
39
                }
40
41
                $states->transitionTo($line->getTransactionCode());
42
                $handler = [$this->strategy, "on{$states->getState()}"];
43
44
                if (!is_callable($handler)) {
45
                    throw new Exception\LogicException("Missing handler for state {$states->getState()}");
46
                }
47
48
                $handler($line);
49
            }
50
51
            $states->transitionTo(StateMachine::STATE_DONE);
52
            return $this->strategy->done();
53
54
        } catch (\Exception $e) {
55
            throw new Exception\InvalidFileException(
56
                "{$e->getMessage()} on line $lineNumber in '{$file->getBasename()}'",
57
                0,
58
                $e
59
            );
60
        }
61
    }
62
}
63