Passed
Push — master ( 18bdd2...b79ef4 )
by Pol
04:33
created

Combinatorics::getDataset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
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 Combinatorics.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
abstract class Combinatorics implements \Countable
11
{
12
    /**
13
     * The dataset.
14
     *
15
     * @var array
16
     */
17
    protected $dataset;
18
19
    /**
20
     * The number of values in the dataset.
21
     *
22
     * @var int
23
     */
24
    protected $datasetCount;
25
26
    /**
27
     * The length.
28
     *
29
     * @var int
30
     */
31
    protected $length;
32
33
    /**
34
     * Combinatorics constructor.
35
     *
36
     * @param array $dataset
37
     *   The dataset.
38
     * @param int|null $length
39
     *   The length.
40
     */
41 30
    public function __construct(array $dataset = [], $length = null)
42
    {
43 30
        $this->setDataset($dataset);
44 30
        $this->datasetCount = count($this->dataset);
45 30
        $this->setLength($length);
46 30
    }
47
48
    /**
49
     * Set the length.
50
     *
51
     * @param int $length
52
     *   The length.
53
     *
54
     * @return $this
55
     */
56 30
    public function setLength($length = null)
57
    {
58 30
        $length = is_null($length) ? $this->datasetCount : $length;
59 30
        $this->length = ($length > $this->datasetCount) ? $this->datasetCount : $length;
60
61 30
        return $this;
62
    }
63
64
    /**
65
     * Get the length.
66
     *
67
     * @return int
68
     *   The length.
69
     */
70 10
    public function getLength()
71
    {
72 10
        return (int) $this->length;
73
    }
74
75
    /**
76
     * Set the dataset.
77
     *
78
     * @param array $dataset
79
     *   The dataset.
80
     *
81
     * @return $this
82
     */
83 30
    public function setDataset(array $dataset = [])
84
    {
85 30
        $this->dataset = $dataset;
86
87 30
        return $this;
88
    }
89
90
    /**
91
     * Get the dataset.
92
     *
93
     * @return mixed[]
94
     *   The dataset.
95
     */
96 22
    public function getDataset()
97
    {
98 22
        return $this->dataset;
99
    }
100
101
    /**
102
     * Compute the factorial of an integer.
103
     *
104
     * @param int $n
105
     *   The number to get its factorial.
106
     * @param int $total
107
     *   The total.
108
     *
109
     * @return int
110
     *   The factorial of $n.
111
     */
112 10
    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...
113
    {
114 10
        return ($n < 2) ? $total : $this->fact($n - 1, $total * $n);
115
    }
116
}
117