Completed
Push — 8.x-1.x ( 3c8412...894b5c )
by Frédéric G.
01:50
created

ContentModerationGrid::getStates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\qa\Workflows;
4
5
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6
use Drupal\Core\Entity\EntityStorageInterface;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\StringTranslation\StringTranslationTrait;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class ContentModerationGrid implements ContainerInjectionInterface {
12
13
  use StringTranslationTrait;
14
15
  protected $stateStorage;
16
17
  protected $transStorage;
18
19
  public function __construct(EntityStorageInterface $stateStorage, EntityStorageInterface $transStorage) {
20
    $this->stateStorage = $stateStorage;
21
    $this->transStorage = $transStorage;
22
  }
23
24 View Code Duplication
  protected function buildGrid() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    $grid = [];
26
    $stateIds = array_keys($this->getStates());
27
    $stateCells = array_map(function ($stateId) {
0 ignored issues
show
Unused Code introduced by
The parameter $stateId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
      return [];
29
    }, array_flip($stateIds));
30
31
    foreach ($stateIds as $stateId) {
32
      $grid[$stateId] = $stateCells;
33
    }
34
35
    foreach ($this->getTrans() as $transId => $trans) {
36
      $grid[$trans['from']][$trans['to']][] = $transId;
37
    }
38
39
    return $grid;
40
  }
41
42 View Code Duplication
  protected function buildRow(string $stateId, array $transList) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    $states = $this->getStates();
44
    $trans = $this->getTrans();
45
    $row = ["${states[$stateId]}\n${stateId}"];
46
    foreach ($transList as $from => $transIds) {
47
      $cellArray = [];
48
      foreach ($transIds as $transId) {
49
        assert($trans[$transId]['from'] = $stateId);
50
        $transLabel = $trans[$transId]['label'];
51
      }
52
      $cellArray[] = $transLabel;
0 ignored issues
show
Bug introduced by
The variable $transLabel does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
53
      $cellArray[] = $transId;
0 ignored issues
show
Bug introduced by
The variable $transId seems to be defined by a foreach iteration on line 48. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
54
      $cellArray[] = "";
55
      $cell = implode("\n", $cellArray);
56
      $row[$trans[$transId]['to']] = $cell;
57
    }
58
59
    return $row;
60
  }
61
62 View Code Duplication
  public function getDuplicateTransLabels() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    $labels = [];
64
    foreach ($this->getTrans() as $trans) {
65
      if (isset($labels[$trans['label']])) {
66
        $labels[$trans['label']]++;
67
      }
68
      else {
69
        $labels[$trans['label']] = 1;
70
      }
71
    }
72
    $repeatedLabels = array_filter($labels, function ($count) {
73
      return $count > 1;
74
    });
75
76
    return $repeatedLabels;
77
  }
78
79
  public static function create(ContainerInterface $container) {
80
    /** @var EntityTypeManagerInterface $etm */
81
    $etm = $container->get('entity_type.manager');
82
83
    $stateStorage = $etm->getStorage('moderation_state');
84
    $transStorage = $etm->getStorage('moderation_state_transition');
85
86
    return new static($stateStorage, $transStorage);
87
  }
88
89 View Code Duplication
  protected function getStates() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    $fullStates = $this->stateStorage->loadMultiple();
91
    $simpleStates = array_map(function ($entity) {
92
      return $entity->label();
93
    }, $fullStates);
94
    asort($simpleStates);
95
    return $simpleStates;
96
  }
97
98 View Code Duplication
  protected function getTrans() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    $fullTrans = $this->transStorage->loadMultiple();
100
    $simpleTrans = array_map(function ($trans) {
101
      return [
102
        'label' => $trans->label(),
103
        'from' => $trans->getFromState(),
104
        'to' => $trans->getToState(),
105
      ];
106
    }, $fullTrans);
107
108
    return $simpleTrans;
109
  }
110
111
  public function report() {
112
    $grid = $this->buildGrid();
113
114
    $rows = [];
115
116
    // Build header rows.
117
    $headerStates = array_merge(["From \\\n" => "    \\ To"], $this->getStates());
118
    $rows[] = array_map(function ($id, $label) {
119
      return "${label}\n${id}";
120
    }, array_keys($headerStates), $headerStates);
121
122
    // Build data rows.
123
    foreach ($grid as $id => $transList) {
124
      $rows[] = $this->buildRow($id, $transList);
125
    }
126
127
    // Render.
128
    drush_print_table($rows, TRUE);
129
    drush_print("");
130
131
    $duplicates = $this->getDuplicateTransLabels();
132
    if (!empty($duplicates)) {
133
      $count = count($duplicates);
134
      $message = $this->formatPlural($count, "One repeated transition label: @info", "@count repeated transition labels: @info", [
135
        "@info" => implode(", ", array_keys($duplicates))
136
      ]);
137
      drush_log($message, 'warning');
138
    }
139
  }
140
141
}
142