Passed
Pull Request — master (#2)
by Sergey
03:39
created

JsonHelperTrait::arrayToObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.3755

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 14
ccs 6
cts 11
cp 0.5455
rs 9.9
cc 2
nc 2
nop 1
crap 2.3755
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * Project: json-rpc-server
6
 * User: sv
7
 * Date: 06.01.2021
8
 * Time: 15:37
9
 */
10
11
declare(strict_types=1);
12
13
namespace Onnov\JsonRpcServer\Traits;
14
15
use Exception;
16
use Onnov\JsonRpcServer\Exception\ParseErrorException;
17
use stdClass;
18
19
/**
20
 * Class JsonHelperTrait
21
 * @package Onnov\JsonRpcServer\Traits
22
 */
23
trait JsonHelperTrait
24
{
25
    /**
26
     * @param mixed[] $array
27
     * @return stdClass|null
28
     */
29 5
    public function arrayToObject(array $array): ?stdClass
30
    {
31
        try {
32 5
            return json_decode(
33 5
                json_encode($array, JSON_THROW_ON_ERROR),
34 5
                false,
35 5
                512,
36 5
                JSON_THROW_ON_ERROR
37
            );
38
        } catch (Exception $e) {
39
            throw new ParseErrorException(
40
                $e->getMessage(),
41
                $e->getCode(),
42
                $e->getPrevious()
43
            );
44
        }
45
    }
46
}
47