| Conditions | 14 |
| Paths | 60 |
| Total Lines | 38 |
| 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); |
||
| 31 | public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null) |
||
| 32 | { |
||
| 33 | $json = []; |
||
| 34 | |||
| 35 | foreach ($this as $key => $value) { |
||
| 36 | if (isset($value)) { |
||
| 37 | if ($value instanceof PrettyJsonSerializable) { |
||
| 38 | $value = $value->jsonLDSerialize('', false); |
||
| 39 | } elseif (is_array($value) and !count(array_filter(array_keys($value), 'is_string'))) { |
||
| 40 | $a = []; |
||
| 41 | foreach ($value as $m) { |
||
| 42 | if ($m instanceof PrettyJsonSerializable) { |
||
| 43 | $m = $m->jsonLDSerialize('', false); |
||
| 44 | } |
||
| 45 | $a[] = $m; |
||
| 46 | } |
||
| 47 | $value = $a; |
||
| 48 | } |
||
| 49 | $json[$key] = $value; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | # don't serialize implicitly deriveable types for brevity |
||
| 54 | if ($context) { |
||
| 55 | $json['@context'] = $context; |
||
| 56 | } |
||
| 57 | if ($types === null) { |
||
| 58 | $types = (bool)$context; |
||
| 59 | } |
||
| 60 | if (!$types && count($json['type'] ?? []) == 1 && count(static::TYPES)) { |
||
| 61 | if ($json['type'][0] == static::TYPES[0]) { |
||
| 62 | unset($json['type']); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | ksort($json); |
||
| 67 | return $json; |
||
| 68 | } |
||
| 69 | |||
| 86 |