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

Combinatorics::fact()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace drupol\phpermutations;
4
5
abstract class Combinatorics implements \Countable {
6
7
  /**
8
   * @var array
9
   */
10
  protected $dataset;
11
12
  /**
13
   * @var int
14
   */
15
  protected $datasetCount;
16
17
  /**
18
   * @var int
19
   */
20
  protected $length;
21
22
  /**
23
   * Combinatorics constructor.
24
   *
25
   * @param array $dataset
26
   * @param null $length
27
   */
28 6
  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 6
    $this->setDataset($dataset);
30 6
    $this->datasetCount = count($this->dataset);
31 6
    $this->setLength($length);
32 6
  }
33
34
  /**
35
   * @param int $length
1 ignored issue
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...
36
   *
37
   * @return $this
38
   */
39 6
  public function setLength($length = NULL) {
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
40 6
    $length = is_null($length) ? $this->datasetCount : $length;
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $length. This often makes code more readable.
Loading history...
41 6
    $this->length = ($length > $this->datasetCount) ? $this->datasetCount : $length;
42
43 6
    return $this;
44
  }
45
46
  /**
47
   * @return int
48
   */
49 6
  public function getLength() {
50 6
    return (int) $this->length;
51
  }
52
53
  /**
54
   * @param array $dataset
55
   *
56
   * @return $this
57
   */
58 6
  public function setDataset(array $dataset = array()) {
59 6
    $this->dataset = $dataset;
60
61 6
    return $this;
62
  }
63
64
  /**
65
   * @return mixed
66
   */
67 6
  public function getDataset() {
68 6
    return $this->dataset;
69
  }
70
71
  /**
72
   * @param $n
73
   * @param int $total
74
   *
75
   * @return int
76
   */
77 6
  protected function fact($n, $total = 1) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. 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...
78 6
      return ($n < 2) ? $total : $this->fact($n-1, $total * $n);
79
  }
80
81
}