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

ContentModerationGraph::buildGraph()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 0
dl 0
loc 24
rs 8.9713
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 Grafizzi\Graph\Attribute;
10
use Grafizzi\Graph\Edge;
11
use Grafizzi\Graph\Graph;
12
use Grafizzi\Graph\Node;
13
use Monolog\Handler\StreamHandler;
14
use Monolog\Logger;
15
use Pimple\Container;
16
use Pimple\Tests\Fixtures\PimpleServiceProvider;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
class ContentModerationGraph extends ContentModerationReportBase implements ContainerInjectionInterface {
20
21
  use StringTranslationTrait;
22
23
  /**
24
   * The Pimple instance used by Grafizzi.
25
   *
26
   * @var \Pimple\Container
27
   */
28
  protected $pimple;
29
30
  public function __construct(EntityStorageInterface $stateStorage, EntityStorageInterface $transStorage, Container $pimple) {
31
    $this->pimple = $pimple;
32
    $this->stateStorage = $stateStorage;
33
    $this->transStorage = $transStorage;
34
  }
35
36
  /**
37
   * @return \Grafizzi\Graph\Graph
38
   */
39
  protected function buildGraph() {
40
    $dic = $this->pimple;
41
    $g = new Graph($dic);
42
    $states = $this->getStates();
43
    $transList = $this->getTrans();
44
45
    $nodes = [];
46
    foreach ($states as $name => $label) {
47
      $nodes[$name] = new Node($dic, $name, [
48
        new Attribute($dic, 'label', "${label}\n${name}"),
49
      ]);
50
      $g->addChild($nodes[$name]);
51
    }
52
53
    foreach ($transList as $name => $trans) {
54
      $g->addChild(new Edge($dic,
55
        $nodes[$trans['from']],
56
        $nodes[$trans['to']],
57
        [new Attribute($dic, 'label', "${trans['label']}\n$name")]
58
      ));
59
    }
60
61
    return $g;
62
  }
63
64 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...
65
    $states = $this->getStates();
66
    $trans = $this->getTrans();
67
    $row = ["${states[$stateId]}\n${stateId}"];
68
    foreach ($transList as $from => $transIds) {
69
      $cellArray = [];
70
      foreach ($transIds as $transId) {
71
        assert($trans[$transId]['from'] = $stateId);
72
        $transLabel = $trans[$transId]['label'];
73
      }
74
      $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...
75
      $cellArray[] = $transId;
0 ignored issues
show
Bug introduced by
The variable $transId seems to be defined by a foreach iteration on line 70. 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...
76
      $cellArray[] = "";
77
      $cell = implode("\n", $cellArray);
78
      $row[$trans[$transId]['to']] = $cell;
79
    }
80
81
    return $row;
82
  }
83
84
  public static function create(ContainerInterface $container) {
85
    /** @var EntityTypeManagerInterface $etm */
86
    $etm = $container->get('entity_type.manager');
87
88
    $stateStorage = $etm->getStorage('moderation_state');
89
    $transStorage = $etm->getStorage('moderation_state_transition');
90
91
    $logger = new Logger(basename(__FILE__, '.php'));
92
    // Change the minimum logging level using the Logger:: constants.
93
    $logger->pushHandler(new StreamHandler('php://stderr', Logger::INFO));
94
95
    $pimple = new Container([
96
      'logger' => $logger,
97
      'directed' => TRUE,
98
    ]);
99
100
    return new static($stateStorage, $transStorage, $pimple);
101
  }
102
103
  public function report() {
104
    $grid = $this->buildGrid();
105
    return $this->buildGraph($grid)
106
      ->build();
107
  }
108
109
}
110