JsonHelper::encode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 4
c 2
b 1
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Rexlabs\Smokescreen\Helpers;
4
5
use Rexlabs\Smokescreen\Exception\JsonEncodeException;
6
7
class JsonHelper
8
{
9
    /**
10
     * Encodes data as a JSON string representation.
11
     *
12
     * @param mixed $data
13
     * @param int   $options
14
     *
15
     * @throws \Rexlabs\Smokescreen\Exception\JsonEncodeException
16
     *
17
     * @return string
18
     */
19 2
    public static function encode($data, $options = 0): string
20
    {
21 2
        $json = json_encode($data, $options);
22 2
        if (JSON_ERROR_NONE !== json_last_error()) {
23 1
            throw new JsonEncodeException(json_last_error_msg());
24
        }
25
26 1
        return $json;
27
    }
28
}
29