Json   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A read() 0 4 1
A write() 0 4 1
A initFlags() 0 8 2
1
<?php
2
namespace Balloon\Format;
3
4
use Balloon\Reader\IFileReader;
5
6
/**
7
 * Class Json
8
 * @package Balloon\Format
9
 * @author Raphaël Lefebvre <[email protected]>
10
 */
11
class Json implements IFileReader
12
{
13
    /**
14
     * @var IFileReader
15
     */
16
    private $fileReader;
17
18
    /**
19
     * @var int
20
     */
21
    private $flags;
22
23
    /**
24
     * @param IFileReader $fileReader
25
     * @param int $flags
0 ignored issues
show
Documentation introduced by
Should the type for parameter $flags not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     */
27
    public function __construct(IFileReader $fileReader, $flags = null)
28
    {
29
        $this->fileReader = $fileReader;
30
        $this->initFlags($flags);
31
    }
32
33
    /**
34
     * @return array|null
35
     */
36
    public function read()
37
    {
38
        return json_decode($this->fileReader->read(), true);
39
    }
40
41
    /**
42
     * @param mixed $data
43
     * @param int $mode
44
     * @return int
45
     */
46
    public function write($data, $mode = 0)
47
    {
48
        return $this->fileReader->write(json_encode($data, $this->flags), $mode);
49
    }
50
51
    /**
52
     * @param $flags
53
     */
54
    private function initFlags($flags)
55
    {
56
        if ($flags === null) {
57
            $this->flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
58
        } else {
59
            $this->flags = $flags;
60
        }
61
    }
62
}
63