Completed
Push — master ( 15a3ff...ddeb31 )
by Pol
02:16
created

Permutations   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
cbo 2
dl 0
loc 89
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A count() 0 3 1
B generator() 0 22 5
B permute() 0 23 6
A toArray() 0 9 2
1
<?php
2
3
namespace drupol\phpermutations;
4
5
/**
6
 * Class Permutations.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
class Permutations extends Combinatorics {
11
12
  /**
13
   * Permutations constructor.
14
   *
15
   * @param array $dataset
16
   * @param int $length
0 ignored issues
show
Documentation introduced by
Should the type for parameter $length not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
17
   */
18 3
  public function __construct(array $dataset = array(), $length = NULL) {
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
19 3
    parent::__construct($dataset, $length);
20 3
  }
21
22
  /**
23
   * @return int
24
   */
25 3
  public function count() {
26 3
    return $this->fact(count($this->getDataset())) / $this->fact(count($this->getDataset()) - $this->getLength());
27
  }
28
29
  /**
30
   * @return \Generator
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
31
   */
32 3
  public function generator() {
33 3
    $combinations = new Combinations($this->getDataset(), $this->getLength());
34 3
    $result = array();
35
36 3
    foreach ($combinations->toArray() as $subset) {
37 3
      $set = $subset;
38 3
      $perms = array();
39 3
      $size = count($subset) - 1;
40 3
      $perm = range(0, $size);
41 3
      $j = 0;
42
43
      do {
44 3
        foreach ($perm as $i) {
45 3
          $perms[$j][] = $set[$i];
46 3
        }
47 3
      } while ($perm = $this->permute($perm, $size) and ++$j);
48
49 3
      $result = array_merge($result, $perms);
50 3
    }
51
52 3
    return $result;
53
  }
54
55
  /**
56
   * @param $items
57
   * @param array $perms
0 ignored issues
show
Bug introduced by
There is no parameter named $perms. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
58
   *
59
   * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
60
   */
61 3
  protected function permute($p, $size) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
62
    // slide down the array looking for where we're smaller than the next guy
63 3
    for ($i = $size - 1; $i >= 0 && $p[$i] >= $p[$i+1]; --$i) { }
64
65
    // if this doesn't occur, we've finished our permutations
66
    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
67 3
    if ($i == -1) {
68 3
      return false;
69
    }
70
71
    // slide down the array looking for a bigger number than what we found before
72 3
    for ($j = $size; $p[$j] <= $p[$i]; --$j) { }
73
74
    // swap them
75 3
    $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
76
77
    // now reverse the elements in between by swapping the ends
78 3
    for (++$i, $j = $size; $i < $j; ++$i, --$j) {
79 3
      $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
80 3
    }
81
82 3
    return $p;
83
  }
84
85
  /**
86
   * @return array
87
   */
88 3
  public function toArray() {
89 3
    $results = array();
90
91 3
    foreach ($this->generator() as $value) {
92 3
      $results[] = $value;
93 3
    }
94
95 3
    return $results;
96
  }
97
98
}
99