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

ContentModerationReportBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 79.1 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 53
loc 67
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildGrid() 17 17 3
A getDuplicateTransLabels() 16 16 3
A getStates() 8 8 1
A getTrans() 12 12 1
report() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Drupal\magpjm\Workflows;
4
5
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6
use Drupal\Core\StringTranslation\StringTranslationTrait;
7
8
abstract class ContentModerationReportBase implements ContainerInjectionInterface {
9
10
  use StringTranslationTrait;
11
12
  protected $stateStorage;
13
14
  protected $transStorage;
15
16 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...
17
    $grid = [];
18
    $stateIds = array_keys($this->getStates());
19
    $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...
20
      return [];
21
    }, array_flip($stateIds));
22
23
    foreach ($stateIds as $stateId) {
24
      $grid[$stateId] = $stateCells;
25
    }
26
27
    foreach ($this->getTrans() as $transId => $trans) {
28
      $grid[$trans['from']][$trans['to']][] = $transId;
29
    }
30
31
    return $grid;
32
  }
33
34 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...
35
    $labels = [];
36
    foreach ($this->getTrans() as $trans) {
37
      if (isset($labels[$trans['label']])) {
38
        $labels[$trans['label']]++;
39
      }
40
      else {
41
        $labels[$trans['label']] = 1;
42
      }
43
    }
44
    $repeatedLabels = array_filter($labels, function ($count) {
45
      return $count > 1;
46
    });
47
48
    return $repeatedLabels;
49
  }
50
51 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...
52
    $fullStates = $this->stateStorage->loadMultiple();
53
    $simpleStates = array_map(function ($entity) {
54
      return $entity->label();
55
    }, $fullStates);
56
    asort($simpleStates);
57
    return $simpleStates;
58
  }
59
60 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...
61
    $fullTrans = $this->transStorage->loadMultiple();
62
    $simpleTrans = array_map(function ($trans) {
63
      return [
64
        'label' => $trans->label(),
65
        'from' => $trans->getFromState(),
66
        'to' => $trans->getToState(),
67
      ];
68
    }, $fullTrans);
69
70
    return $simpleTrans;
71
  }
72
73
  public abstract function report();
74
}
75