Completed
Push — master ( b5dbe1...4c076d )
by Pol
02:14
created

Permutations::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace drupol\phpermutations;
4
5
/**
6
 * Class Permutations.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
class Permutations implements \Countable {
11
12
  /**
13
   * @var mixed
14
   */
15
  protected $dataset;
16
17
  /**
18
   * @var int
19
   */
20
  protected $length;
21
22
  /**
23
   * Permutations constructor.
24
   *
25
   * @param array $dataset
26
   * @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...
27
   */
28 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...
29 3
    $this->setDataset($dataset);
30 3
    $length = ($length > $this->count()) ? $this->count() : $length;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $length. This often makes code more readable.
Loading history...
31 3
    $this->setLength($length);
32
33 3
    return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
34
  }
35
36
  /**
37
   * @return int
38
   */
39 3
  public function count() {
40 3
    return count($this->getDataset());
41
  }
42
43
  /**
44
   * @param int $length
45
   */
46 3
  public function setLength($length) {
47 3
    $this->length = $length;
48 3
  }
49
50
  /**
51
   * @return int
52
   */
53 3
  public function getLength() {
54 3
    return (int) $this->length;
55
  }
56
57
  /**
58
   * @param array $dataset
59
   *
60
   * @return $this
61
   */
62 3
  public function setDataset(array $dataset = array()) {
63 3
    $this->dataset = $dataset;
64
65 3
    return $this;
66
  }
67
68
  /**
69
   * @return mixed
70
   */
71 3
  public function getDataset() {
72 3
    return $this->dataset;
73
  }
74
75
  /**
76
   * @return array
77
   */
78 3
  public function generator() {
79 3
    return $this->permute($this->getDataset(), array());
80
  }
81
82
  /**
83
   * @param $items
84
   * @param array $perms
85
   *
86
   * @return array
87
   */
88 3
  protected function permute($items, $perms = array( )) {
89 3
    $result = array();
90
91 3
    foreach (new Combinations($items, $this->getLength()) as $dataset) {
92 3
      if (empty($dataset)) {
93 3
        $return = array($perms);
94 3
      }  else {
95 3
        $return = array();
96 3
        for ($i = count($dataset) - 1; $i >= 0; --$i) {
97 3
          $newitems = $dataset;
98 3
          $newperms = $perms;
99 3
          list($foo) = array_splice($newitems, $i, 1);
100 3
          array_unshift($newperms, $foo);
101 3
          $return = array_merge($return, $this->permute($newitems, $newperms));
102 3
        }
103
      }
104 3
      $result = array_merge($result, $return);
105 3
    }
106
107 3
    return $result;
108
  }
109
110
  /**
111
   * @return array
112
   */
113 3
  public function toArray() {
114 3
    $results = array();
115
116 3
    foreach($this->generator() as $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
117 3
      $results[] = $value;
118 3
    }
119
120 3
    return $results;
121
  }
122
123
}
124