Completed
Push — master ( 0f8fe7...ba014d )
by Pol
01:57
created

Permutations::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace drupol\phpermutations;
4
5
/**
6
 * Class Permutations.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
class Permutations {
11
12
  /**
13
   * @var mixed
14
   */
15
  protected $elements;
16
17
  /**
18
   * @var int
19
   */
20
  protected $size;
21
22
  /**
23
   * Permutations constructor.
24
   *
25
   * @param array $elements
26
   * @param int $size
27
   */
28
  public function __construct(array $elements, $size = NULL) {
29
    $this->setElements($elements);
30
    $size = $size ? $size : count($elements);
31
    $this->setSize($size);
32
33
    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
   * @param int $size
38
   */
39
  public function setSize($size) {
40
    $this->size = $size;
41
  }
42
43
  /**
44
   * @return int
45
   */
46
  public function getSize() {
47
    return (int) $this->size;
48
  }
49
50
  /**
51
   * @param array $elements
52
   *
53
   * @return $this
54
   */
55
  public function setElements(array $elements) {
56
    $this->elements = $elements;
57
58
    return $this;
59
  }
60
61
  /**
62
   * @return mixed
63
   */
64
  public function getElements() {
65
    return $this->elements;
66
  }
67
68
  /**
69
   * @return \Generator
70
   */
71
  public function generator() {
72
    return $this->generate($this->getElements(), $this->getSize());
73
  }
74
75
  /**
76
   * @param array $elements
77
   *
78
   * @return \Generator
79
   */
80
  protected function generate(array $elements, $length) {
0 ignored issues
show
Unused Code introduced by
The parameter $length is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    if (count($elements) <= 1) {
82
      yield $elements;
83
    } else {
84
      foreach ($this->generate(array_slice($elements, 1)) as $permutation) {
0 ignored issues
show
Bug introduced by
The call to generate() misses a required argument $length.

This check looks for function calls that miss required arguments.

Loading history...
85
        foreach (range(0, count($elements) - 1) as $i) {
86
          yield array_merge(
87
            array_slice($permutation, 0, $i),
88
            [$elements[0]],
89
            array_slice($permutation, $i)
90
          );
91
        }
92
      }
93
    }
94
  }
95
96
  /**
97
   * @return array
98
   */
99
  public function toArray() {
100
    $results = array();
101
102
    foreach($this->generator() as $key => $value) {
103
      $results[$key] = $value;
104
    }
105
106
    return $results;
107
  }
108
109
}
110