RawJsonFileOutputHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 35
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateHandler() 0 4 2
A close() 0 3 1
A init() 0 4 1
A write() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\Processor\Business\Model\OutputHandler;
5
6
7
use DataProvider\ProcessRunDataProvider;
8
use Xervice\Processor\Business\Exception\ProcessException;
9
10
class RawJsonFileOutputHandler implements OutputHandlerInterface
11
{
12
    /**
13
     * @var resource
14
     */
15
    private $handler;
16
17
    /**
18
     * @param \DataProvider\ProcessRunDataProvider $runDataProvider
19
     *
20
     * @throws \Xervice\Processor\Business\Exception\ProcessException
21
     */
22 2
    public function init(ProcessRunDataProvider $runDataProvider): void
23
    {
24 2
        $this->handler = fopen($runDataProvider->getOutput(), 'w');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($runDataProvider->getOutput(), 'w') 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...
25 2
        $this->validateHandler();
26 2
    }
27
28
    /**
29
     * @param array $data
30
     */
31 1
    public function write(array $data): void
32
    {
33 1
        fwrite($this->handler, json_encode($data));
34 1
    }
35
36 2
    public function close(): void
37
    {
38 2
        fclose($this->handler);
39 2
    }
40
41 2
    protected function validateHandler(): void
42
    {
43 2
        if (!$this->handler) {
44
            throw new ProcessException('Output file isn\'t writeable');
45
        }
46 2
    }
47
48
}