Completed
Push — trunk ( 0e7a2e...6a6c5e )
by SuperNova.WS
07:28
created

TJsonSerializable::fromJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 12.10.2017 13:20
4
 */
5
6
namespace Common\Traits;
7
8
9
trait TJsonSerializable {
10
11
  /**
12
   * @param $json
13
   *
14
   * @return static
15
   */
16
  public static function fromJson($json) {
17
    $stdobj = json_decode($json);
18
    $temp = serialize($stdobj);
19
20
    // Replacing default object name with ours
21
    $temp = preg_replace('@^O:8:"stdClass":@', 'O:' . strlen(static::class) . ':"' . static::class . '":', $temp);
22
    // Converting default objects to arrays
23
    $temp = preg_replace('@O:8:"stdClass":@', 'a:', $temp);
24
25
    //Unserialize and walk away like nothing happened
26
    $that = unserialize($temp);
27
28
//    var_dump($that);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
30
    return $that;
31
  }
32
33
  /**
34
   * @return string
35
   */
36
  public function toJson() {
37
    return json_encode($this);
38
  }
39
40
}
41