| Conditions | 16 |
| Paths | 13 |
| Total Lines | 55 |
| Code Lines | 36 |
| 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); |
||
| 58 | protected function setField(string $field, $value, bool $strict = true) |
||
| 59 | { |
||
| 60 | if ($field == '@context') { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | $type = static::fieldType($field, $strict); |
||
| 65 | if (!$type) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (is_null($value)) { |
||
| 70 | $value = null; |
||
| 71 | } elseif (is_array($type)) { # Set or Listing |
||
| 72 | if ($type[0] == 'Set') { |
||
| 73 | if (!($value instanceof Set)) { |
||
| 74 | if (is_array($value)) { |
||
| 75 | $class = 'JSKOS\\' . $type[1]; |
||
| 76 | $value = new Set( |
||
| 77 | array_map(function ($m) use ($class) { |
||
| 78 | if (is_null($m)) { |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | if ($m instanceof $class) { |
||
| 82 | return $m; |
||
| 83 | } |
||
| 84 | return new $class($m); |
||
| 85 | }, $value) |
||
| 86 | ); |
||
| 87 | } else { |
||
| 88 | throw $this->fieldException($field, "be a Set"); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | # TODO: check member types |
||
| 92 | } else { # Listing |
||
| 93 | if (!($value instanceof Listing)) { |
||
| 94 | if (is_array($value)) { |
||
| 95 | $value = new Listing($value); |
||
| 96 | } else { |
||
| 97 | throw $this->fieldException($field, "be a Listing"); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | # TODO: check member types |
||
| 101 | } |
||
| 102 | } elseif (in_array($type, ['LanguageMapOfStrings', 'LanguageMapOfLists', 'ConceptScheme', 'Item'])) { |
||
| 103 | $type = "JSKOS\\$type"; |
||
| 104 | if (!($value instanceof $type)) { |
||
| 105 | $value = new $type($value); |
||
| 106 | } |
||
| 107 | } elseif ($type != '*' && !DataType::hasType($value, $type)) { |
||
| 108 | throw $this->fieldException($field, "match JSKOS\DataType::is$type"); |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->$field = $value; |
||
| 112 | } |
||
| 113 | |||
| 235 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.