Completed
Push — master ( 7f834d...3d4931 )
by Pol
02:09
created

Permutations::permute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 16
nc 3
nop 2
crap 4
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);
0 ignored issues
show
Bug introduced by
It seems like $length defined by parameter $length on line 18 can also be of type integer; however, drupol\phpermutations\Combinatorics::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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 array
31
   */
32 3
  public function generator() {
33 3
    return $this->permute($this->getDataset(), array());
34
  }
35
36
  /**
37
   * @param $items
38
   * @param array $perms
39
   *
40
   * @return array
41
   */
42 3
  protected function permute($items, $perms = array( )) {
43 3
    $result = array();
44
45 3
    foreach (new Combinations($items, $this->getLength()) as $dataset) {
46 3
      if (empty($dataset)) {
47 3
        $return = array($perms);
48
      }  else {
49 3
        $return = array();
50 3
        $datasetCount = count($dataset) - 1;
51 3
        for ($i = $datasetCount; $i >= 0; --$i) {
52 3
          $newitems = $dataset;
53 3
          $newperms = $perms;
54 3
          list($foo) = array_splice($newitems, $i, 1);
55 3
          array_unshift($newperms, $foo);
56 3
          $return = array_merge($return, $this->permute($newitems, $newperms));
57
        }
58
      }
59 3
      $result = array_merge($result, $return);
60
    }
61
62 3
    return $result;
63
  }
64
65
  /**
66
   * @return array
67
   */
68 3
  public function toArray() {
69 3
    $results = array();
70
71 3
    foreach($this->generator() as $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
72 3
      $results[] = $value;
73
    }
74
75 3
    return $results;
76
  }
77
78
}
79