| Conditions | 2 |
| Paths | 2 |
| Total Lines | 71 |
| Code Lines | 50 |
| 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 |
||
| 47 | public function testBasicFields() { |
||
| 48 | $user = $this->createUser(); |
||
| 49 | |||
| 50 | $node = $this->createNode([ |
||
| 51 | 'title' => 'Node in default language', |
||
| 52 | 'type' => 'test', |
||
| 53 | 'status' => 1, |
||
| 54 | 'uid' => $user->id(), |
||
| 55 | ]); |
||
| 56 | |||
| 57 | $translation = $node->addTranslation('fr', ['title' => 'French node']); |
||
| 58 | $translation->save(); |
||
| 59 | |||
| 60 | $created = (new DateTime())->setTimestamp($node->getCreatedTime())->format(DateTime::ISO8601); |
||
| 61 | $changed = (new DateTime())->setTimestamp($node->getChangedTime())->format(DateTime::ISO8601); |
||
| 62 | |||
| 63 | $values = [ |
||
| 64 | 'entityId' => $node->id(), |
||
| 65 | 'entityUuid' => $node->uuid(), |
||
| 66 | 'entityLabel' => $node->label(), |
||
| 67 | 'entityType' => $node->getEntityTypeId(), |
||
| 68 | 'entityBundle' => $node->bundle(), |
||
| 69 | 'entityLanguage' => [ |
||
| 70 | 'id' => $node->language()->getId(), |
||
| 71 | 'name' => $node->language()->getName(), |
||
| 72 | 'direction' => $node->language()->getDirection(), |
||
| 73 | 'weight' => $node->language()->getWeight(), |
||
| 74 | ], |
||
| 75 | 'entityRoute' => [ |
||
| 76 | 'internalPath' => '/node/' . $node->id(), |
||
| 77 | 'aliasedPath' => '/node/' . $node->id(), |
||
| 78 | ], |
||
| 79 | 'entityOwner' => [ |
||
| 80 | 'entityLabel' => $user->label(), |
||
| 81 | ], |
||
| 82 | 'entityTranslation' => [ |
||
| 83 | 'entityLabel' => $translation->label(), |
||
| 84 | ], |
||
| 85 | // EntityPublishedInterface has been added with 8.3. |
||
| 86 | // Below the field will return false. |
||
| 87 | 'entityPublished' => version_compare(\Drupal::VERSION, '8.3', '<') ? FALSE : TRUE, |
||
| 88 | 'entityCreated' => $created, |
||
| 89 | 'entityChanged' => $changed, |
||
| 90 | 'viewAccess' => TRUE, |
||
| 91 | 'updateAccess' => TRUE, |
||
| 92 | 'deleteAccess' => FALSE, |
||
| 93 | ]; |
||
| 94 | |||
| 95 | |||
| 96 | $query = $this->getQueryFromFile('basic_fields.gql'); |
||
| 97 | |||
| 98 | // TODO: Check cache metadata. |
||
| 99 | $metadata = $this->defaultCacheMetaData(); |
||
| 100 | $metadata->addCacheContexts([ |
||
| 101 | 'languages:language_content', |
||
| 102 | 'user.node_grants:view', |
||
| 103 | ]); |
||
| 104 | |||
| 105 | $metadata->addCacheTags([ |
||
| 106 | 'entity_bundles', |
||
| 107 | 'entity_field_info', |
||
| 108 | 'entity_types', |
||
| 109 | 'node:1', |
||
| 110 | 'node_list', |
||
| 111 | 'user:1', |
||
| 112 | ]); |
||
| 113 | |||
| 114 | $this->assertResults($query, ['nid' => (int) $node->id()], [ |
||
| 115 | 'node' => ['entities' => [$values]], |
||
| 116 | ], $metadata); |
||
| 117 | } |
||
| 118 | |||
| 120 |