Completed
Pull Request — 8.x-2.x (#28)
by Frédéric G.
07:56
created

SanityCheckCommand::initBucketsList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Command;
4
5
use Doctrine\Common\Util\Debug;
6
use Drupal\Console\Command\ContainerAwareCommand;
7
use Drupal\mongodb_watchdog\Logger;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Drupal\Console\Style\DrupalStyle;
11
12
/**
13
 * Class SanityCheckCommand.
14
 *
15
 * @package Drupal\mongodb_watchdog
16
 */
17
class SanityCheckCommand extends ContainerAwareCommand {
18
19
  protected $buckets;
20
21
  protected $items;
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  protected function configure() {
27
    $this
28
      ->setName('mongodb:watchdog:sanitycheck')
29
      ->setDescription($this->trans('Check the sizes of the watchdog collections'));
30
  }
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  protected function execute(InputInterface $input, OutputInterface $output) {
36
37
    $this->io = $io = new DrupalStyle($input, $output);
38
39
    $this->buildCollectionstats();
40
    $io->info(print_r($this->buckets, TRUE));
41
  }
42
43
  protected function initBucketsList() {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
44
45
    $config = $this->getConfigFactory()->get(Logger::CONFIG_NAME);
46
    $this->items = $items = $config->get('items');
47
    unset($config);
48
49
    $barCount = 10;
50
    $barWidth = $items / $barCount;
51
    $buckets = [
52
      0 => 0,
53
      1 => 0,
54
      $items - 1 => 0,
55
      $items => 0,
56
    ];
57
58
    // 0, 1 and $items are reserved.
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
    for ($i = 1 ; $i < $barCount ; $i++) {
0 ignored issues
show
Coding Style introduced by
Space found before first semicolon of FOR loop
Loading history...
Coding Style introduced by
Space found before second semicolon of FOR loop
Loading history...
Coding Style introduced by
Space found before semicolon; expected "1;" but found "1 ;"
Loading history...
Coding Style introduced by
Space found before semicolon; expected "$barCount;" but found "$barCount ;"
Loading history...
60
      $buckets[$i * $barWidth] = 0;
61
    }
62
    ksort($buckets);
63
    $this->buckets = $buckets;
64
  }
65
66
  protected function store(int $value): void {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
67
    if ($value <= 1 || $value >= $this->items - 1) {
68
      $this->buckets[$value]++;
69
      return;
70
    }
71
    $keys = array_slice(array_keys($this->buckets), 1, -1);
72
    foreach ($keys as $key) {
73
      if ($value < $key) {
74
        $this->buckets[$key]++;
75
        return;
76
      }
77
    }
78
  }
79
80
  function buildCollectionstats() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
introduced by
Missing function doc comment
Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for buildCollectionstats.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
81
    /** @var \Drupal\mongodb\DatabaseFactory $df */
82
    $df = $this->getService('mongodb.database_factory');
83
    $db = $df->get('default');
84
    $this->initBucketsList();
85
86
    $collections = $db->listCollections();
87
    foreach ($collections as $collectionInfo) {
88
      $name = $collectionInfo->getName();// . " " . $collection->count() ."\n"
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Comments may not appear after statements
Loading history...
89
      $collection = $db->selectCollection($name);
90
      $count = $collection->count();
91
      if (preg_match('/' . Logger::EVENT_COLLECTIONS_PATTERN . '/', $name)) {
92
        $this->store($count);
93
      }
94
    }
95
  }
96
}
97