| Conditions | 16 |
| Paths | 13 |
| Total Lines | 57 |
| 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); |
||
| 63 | protected function setFieldStrict(string $field, $value) |
||
| 64 | { |
||
| 65 | if ($field == '@context') { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | $type = static::fieldType($field); |
||
| 70 | if (!$type) { |
||
| 71 | throw new InvalidArgumentException( |
||
| 72 | get_called_class() . "->$field does not exist" |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | if (is_null($value)) { |
||
| 77 | $value = null; |
||
| 78 | } elseif (is_array($type)) { # Set or Listing |
||
| 79 | if ($type[0] == 'Set') { |
||
| 80 | if (!($value instanceof Set)) { |
||
| 81 | if (is_array($value)) { |
||
| 82 | $class = 'JSKOS\\' . $type[1]; |
||
| 83 | $value = new Set( |
||
| 84 | array_map(function ($m) use ($class) { |
||
| 85 | if (is_null($m)) { |
||
| 86 | return null; |
||
| 87 | } |
||
| 88 | if ($m instanceof $class) { |
||
| 89 | return $m; |
||
| 90 | } |
||
| 91 | return new $class($m); |
||
| 92 | }, $value) |
||
| 93 | ); |
||
| 94 | } else { |
||
| 95 | throw $this->fieldException($field, "be a Set"); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | # TODO: check member types |
||
| 99 | } else { # Listing |
||
| 100 | if (!($value instanceof Listing)) { |
||
| 101 | if (is_array($value)) { |
||
| 102 | $value = new Listing($value); |
||
| 103 | } else { |
||
| 104 | throw $this->fieldException($field, "be a Listing"); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | # TODO: check member types |
||
| 108 | } |
||
| 109 | } elseif (in_array($type, ['LanguageMapOfStrings', 'LanguageMapOfLists', 'ConceptScheme', 'Item'])) { |
||
| 110 | $type = "JSKOS\\$type"; |
||
| 111 | if (!($value instanceof $type)) { |
||
| 112 | $value = new $type($value); |
||
| 113 | } |
||
| 114 | } elseif ($type != '*' && !DataType::hasType($value, $type)) { |
||
| 115 | throw $this->fieldException($field, "match JSKOS\DataType::is$type"); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->$field = $value; |
||
| 119 | } |
||
| 120 | |||
| 242 |