Completed
Push — master ( ecf895...d8752e )
by Hannes
03:16
created

Parser::parse()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 8.439
cc 6
eloc 21
nc 14
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\autogiro\Parser;
6
7
use byrokrat\autogiro\Section;
8
use byrokrat\autogiro\Line;
9
use byrokrat\autogiro\Exception;
10
11
/**
12
 * Prase raw autogiro files
13
 */
14
class Parser
15
{
16
    /**
17
     * @var Strategy\Strategy
18
     */
19
    private $strategy;
20
21
    /**
22
     * Set parsing strategy
23
     */
24 2
    public function __construct(Strategy\Strategy $strategy)
25
    {
26 2
        $this->strategy = $strategy;
27 2
    }
28
29
    /**
30
     * Parse file
31
     */
32 2
    public function parse(\SplFileObject $file): Section
33
    {
34 2
        $lineNumber = 'undefined';
35
36
        try {
37 2
            $states = $this->strategy->createStates();
38 2
            $this->strategy->begin();
39
40 2
            foreach ($file as $lineNumber => $line) {
41 2
                $line = new Line($line);
0 ignored issues
show
Bug introduced by
It seems like $line defined by new \byrokrat\autogiro\Line($line) on line 41 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...
42
43 2
                if ($line->isEmpty()) {
44 1
                    continue;
45
                }
46
47 2
                $states->transitionTo($line->getTransactionCode());
48 2
                $handler = [$this->strategy, "on{$states->getState()}"];
49
50 2
                if (!method_exists(...$handler) || !is_callable($handler)) {
51 1
                    throw new Exception\LogicException("Missing handler for state {$states->getState()}");
52
                }
53
54 1
                $handler($line);
55
            }
56
57 1
            $states->transitionTo(StateMachine::STATE_DONE);
58 1
            return $this->strategy->done();
59 1
        } catch (\Exception $e) {
60 1
            throw new Exception\InvalidFileException(
61 1
                "{$e->getMessage()} on line $lineNumber in '{$file->getBasename()}'",
62 1
                0,
63
                $e
64
            );
65
        }
66
    }
67
}
68