RawJsonFileRowInputHandler::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\Processor\Business\Model\InputHandler;
5
6
7
use DataProvider\ProcessRunDataProvider;
8
use Xervice\Processor\Business\Exception\ProcessException;
9
10
class RawJsonFileRowInputHandler implements InputHandlerInterface
11
{
12
    /**
13
     * @var resource
14
     */
15
    private $handler;
16
17
    /**
18
     * @return bool
19
     */
20 2
    public function eof(): bool
21
    {
22 2
        return feof($this->handler);
23
    }
24
25
    /**
26
     * @param \DataProvider\ProcessRunDataProvider $runDataProvider
27
     */
28 2
    public function init(ProcessRunDataProvider $runDataProvider): void
29
    {
30 2
        $this->handler = fopen($runDataProvider->getInput(), 'r');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($runDataProvider->getInput(), 'r') can also be of type false. However, the property $handler is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31 2
        $this->validateHandler();
32 2
    }
33
34
    /**
35
     * @return array
36
     */
37 2
    public function read(): array
38
    {
39 2
        $this->eof = true;
0 ignored issues
show
Bug Best Practice introduced by
The property eof does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40 2
        return json_decode(
41 2
            fgets($this->handler),
42 2
            true
43
        );
44
    }
45
46 2
    public function close(): void
47
    {
48 2
        fclose($this->handler);
49 2
    }
50
51
    /**
52
     * @throws \Xervice\Processor\Business\Exception\ProcessException
53
     */
54 2
    protected function validateHandler(): void
55
    {
56 2
        if (!$this->handler) {
57
            throw new ProcessException('Input file isn\'t readable');
58
        }
59 2
    }
60
61
}