Conditions | 16 |
Paths | 68 |
Total Lines | 57 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public static function import($data) |
||
31 | { |
||
32 | if (!is_array($data)) { |
||
|
|||
33 | throw new Exception('Array expected in JsonPatch::import'); |
||
34 | } |
||
35 | $result = new JsonPatch(); |
||
36 | foreach ($data as $operation) { |
||
37 | /** @var OpPath|OpPathValue|OpPathFrom $operation */ |
||
38 | if (is_array($operation)) { |
||
39 | $operation = (object)$operation; |
||
40 | } |
||
41 | |||
42 | if (!isset($operation->op)) { |
||
43 | throw new Exception('Missing "op" in operation data'); |
||
44 | } |
||
45 | if (!isset($operation->path)) { |
||
46 | throw new Exception('Missing "path" in operation data'); |
||
47 | } |
||
48 | |||
49 | $op = null; |
||
50 | switch ($operation->op) { |
||
51 | case Add::OP: |
||
52 | $op = new Add(); |
||
53 | break; |
||
54 | case Copy::OP: |
||
55 | $op = new Copy(); |
||
56 | break; |
||
57 | case Move::OP: |
||
58 | $op = new Move(); |
||
59 | break; |
||
60 | case Remove::OP: |
||
61 | $op = new Remove(); |
||
62 | break; |
||
63 | case Replace::OP: |
||
64 | $op = new Replace(); |
||
65 | break; |
||
66 | case Test::OP: |
||
67 | $op = new Test(); |
||
68 | break; |
||
69 | default: |
||
70 | throw new Exception('Unknown "op": ' . $operation->op); |
||
71 | } |
||
72 | $op->path = $operation->path; |
||
73 | if ($op instanceof OpPathValue) { |
||
74 | if (!array_key_exists('value', (array)$operation)) { |
||
75 | throw new Exception('Missing "value" in operation data'); |
||
76 | } |
||
77 | $op->value = $operation->value; |
||
78 | } elseif ($op instanceof OpPathFrom) { |
||
79 | if (!isset($operation->from)) { |
||
80 | throw new Exception('Missing "from" in operation data'); |
||
81 | } |
||
82 | $op->from = $operation->from; |
||
83 | } |
||
84 | $result->operations[] = $op; |
||
85 | } |
||
86 | return $result; |
||
87 | } |
||
151 | } |