Passed
Pull Request — master (#1856)
by
unknown
11:54
created

BaseInterface   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromJson() 0 7 2
A fromArray() 0 6 2
1
<?php
2
3
namespace MySociety\TheyWorkForYou\DataClass;
4
5
use function Rutek\Dataclass\transform;
6
7
trait BaseInterface {
8
    public static function fromJson(string $json): self {
9
        $data = json_decode($json, true);
10
        try {
11
            return transform(static::class, $data);
12
        } catch (\Exception $transformException) {
13
            echo json_encode($transformException, JSON_PRETTY_PRINT);
14
            throw $transformException;
15
        }
16
    }
17
18
    public static function fromArray(array $data): self {
19
        try {
20
            return transform(static::class, $data);
21
        } catch (\Exception $transformException) {
22
            echo json_encode($transformException, JSON_PRETTY_PRINT);
23
            throw $transformException;
24
        }
25
    }
26
}
27