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

FiniteGroup   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 85.11%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
cbo 1
dl 0
loc 174
ccs 40
cts 47
cp 0.8511
rs 10
c 2
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A count() 0 4 1
A toArray() 0 10 2
A getSize() 0 4 1
A __construct() 0 4 1
A next() 0 5 1
A rewind() 0 4 1
A setSize() 0 5 1
A order() 0 11 2
A computeGroup() 0 10 3
A gcd() 0 4 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class FiniteGroup.
9
 *
10
 * The finite group is an abelian finite cyclic group.
11
 *
12
 * @package drupol\phpermutations\Iterators
13
 */
14
class FiniteGroup extends Combinatorics implements \Iterator, \Countable
15
{
16
    /**
17
     * The group size.
18
     *
19
     * @var int
20
     */
21
    protected $size;
22
23
    /**
24
     * The group.
25
     *
26
     * @var int[]
27
     */
28
    protected $group;
29
30
    /**
31
     * The key.
32
     *
33
     * @var int
34
     */
35
    protected $key;
36
37
    /**
38
     * Combinatorics constructor.
39
     */
40 2
    public function __construct()
41
    {
42 2
        parent::__construct([], null);
43 2
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function current()
49
    {
50 2
        return current($this->group);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 2
    public function next()
57
    {
58 2
        $this->key++;
59 2
        next($this->group);
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function key()
66
    {
67 2
        return $this->key;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function valid()
74
    {
75 2
        return isset($this->group[$this->key()]);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function rewind()
82
    {
83 2
        $this->key = 0;
84 2
    }
85
86
    /**
87
     * Count elements of an object.
88
     *
89
     * @return int
90
     *   The number of element.
91
     */
92 2
    public function count()
93
    {
94 2
        return count($this->group);
95
    }
96
97
    /**
98
     * Convert the iterator into an array.
99
     *
100
     * @return array
101
     *   The elements.
102
     */
103 2
    public function toArray()
104
    {
105 2
        $data = [];
106
107 2
        for ($this->rewind(); $this->valid(); $this->next()) {
108 2
            $data[] = $this->current();
109 2
        }
110
111 2
        return $data;
112
    }
113
114
    /**
115
     * Set the group size.
116
     *
117
     * @param int $size
118
     *   The size.
119
     */
120 2
    public function setSize($size)
121
    {
122 2
        $this->size = $size;
123 2
        $this->computeGroup();
124 2
    }
125
126
    /**
127
     * Get the group size.
128
     *
129
     * @return int
130
     *   The size.
131
     */
132 2
    public function getSize()
133
    {
134 2
        return intval($this->size);
135
    }
136
137
    /**
138
     * Get the order.
139
     *
140
     * @param int $generator
141
     *   The generator.
142
     *
143
     * @return int
144
     *   The order.
145
     */
146
    public function order($generator)
147
    {
148
        $result = [];
149
150
        foreach (range(1, $this->getSize() - 1) as $number) {
151
            $value = pow($generator, $number) % $this->getSize();
152
            $result[$value] = $value;
153
        }
154
155
        return count($result);
156
    }
157
158
    /**
159
     * Clean out the group from unwanted values.
160
     */
161 2
    private function computeGroup()
162
    {
163 2
        $this->group = [];
164
165 2
        foreach (range(1, $this->getSize() - 1) as $number) {
166 2
            if ($this->gcd($number, $this->getSize() - 1) === 1) {
167 2
                $this->group[] = $number;
168 2
            }
169 2
        }
170 2
    }
171
172
    /**
173
     * Get the greater common divisor between two numbers.
174
     *
175
     * @param int $a
176
     *   The first number.
177
     * @param int $b
178
     *   The second number.
179
     *
180
     * @return int
181
     *   The greater common divisor between $a and $b.
182
     */
183 2
    private function gcd($a, $b)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. 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...
Comprehensibility introduced by
Avoid variables with short names like $b. 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...
184
    {
185 2
        return $b ? $this->gcd($b, $a % $b) : $a;
186
    }
187
}
188