| Conditions | 14 |
| Paths | 45 |
| Total Lines | 39 |
| Code Lines | 25 |
| 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 declare(strict_types=1); |
||
| 30 | public function jsonLDSerialize(string $context=self::DEFAULT_CONTEXT) |
||
| 31 | { |
||
| 32 | $json = [ ]; |
||
| 33 | |||
| 34 | foreach ($this as $key => $value) { |
||
|
|
|||
| 35 | if (isset($value)) { |
||
| 36 | if ($value instanceof PrettyJsonSerializable) { |
||
| 37 | $value = $value->jsonLDSerialize(''); |
||
| 38 | } elseif (is_array($value) and !count(array_filter(array_keys($value), 'is_string'))) { |
||
| 39 | $a = []; |
||
| 40 | foreach ($value as $m) { |
||
| 41 | if ($m instanceof PrettyJsonSerializable) { |
||
| 42 | $m = $m->jsonLDSerialize(''); |
||
| 43 | } |
||
| 44 | $a[] = $m; |
||
| 45 | } |
||
| 46 | $value = $a; |
||
| 47 | } |
||
| 48 | $json[$key] = $value; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($context) { |
||
| 53 | $json['@context'] = $context; |
||
| 54 | $types = defined(get_called_class().'::TYPES') ? static::TYPES : []; |
||
| 55 | if (property_exists($this, 'type') and count($types)) { |
||
| 56 | if (isset($json['type'])) { |
||
| 57 | if (empty(array_intersect($json['type'], $types))) { |
||
| 58 | array_unshift($json['type'], $types[0]); |
||
| 59 | } |
||
| 60 | } else { |
||
| 61 | $json['type'] = [$types[0]]; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | ksort($json); |
||
| 67 | return $json; |
||
| 68 | } |
||
| 69 | |||
| 86 |