Passed
Pull Request — 8.x-2.x (#26)
by Frédéric G.
02:49
created

EventTemplateListBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildHeader() 0 5 1
A buildRow() 0 13 1
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use Drupal\Core\Entity\EntityInterface;
6
use Drupal\Core\Entity\EntityListBuilder;
7
use Drupal\Core\Routing\LinkGeneratorTrait;
8
use Drupal\Core\Url;
9
10
/**
11
 * Defines a class to build a listing of Event template entities.
12
 *
13
 * @ingroup mongodb_watchdog
14
 */
15
class EventTemplateListBuilder extends EntityListBuilder {
16
17
  use LinkGeneratorTrait;
18
19
  /**
20
   * {@inheritdoc}
21
   */
22
  public function buildHeader() {
23
    $header['id'] = $this->t('Event template ID');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$header was never initialized. Although not strictly required by PHP, it is generally a good practice to add $header = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
24
    $header['name'] = $this->t('Name');
25
    return $header + parent::buildHeader();
26
  }
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function buildRow(EntityInterface $entity) {
32
    /* @var $entity \Drupal\mongodb_watchdog\Entity\EventTemplate */
33
    $row['id'] = $entity->id();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$row was never initialized. Although not strictly required by PHP, it is generally a good practice to add $row = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
34
    $row['name'] = $this->l(
35
      $entity->label(),
36
      new Url(
37
        'entity.mongodb_watchdog_event_template.edit_form', array(
38
          'mongodb_watchdog_event_template' => $entity->id(),
39
        )
40
      )
41
    );
42
    return $row + parent::buildRow($entity);
43
  }
44
45
}
46