Test Setup Failed
Push — master ( 9a00d4...fd0b8a )
by Pol
06:00
created

FiniteGroup   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 85%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
cbo 1
dl 0
loc 155
ccs 34
cts 40
cp 0.85
rs 10
c 2
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 3 1
A next() 0 4 1
A key() 0 3 1
A valid() 0 3 1
A rewind() 0 3 1
A count() 0 3 1
A setSize() 0 4 1
A getSize() 0 3 1
A gcd() 0 3 2
A toArray() 0 9 2
A computeGroup() 0 9 3
A order() 0 10 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
   * {@inheritdoc}
39
   */
40 2
  public function current() {
41 2
    return current($this->group);
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47 2
  public function next() {
48 2
    $this->key++;
49 2
    next($this->group);
50 2
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55 2
  public function key() {
56 2
    return $this->key;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62 2
  public function valid() {
63 2
    return isset($this->group[$this->key()]);
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69 2
  public function rewind() {
70 2
    $this->key = 0;
71 2
  }
72
73
  /**
74
   * Count elements of an object.
75
   *
76
   * @return int
77
   *   The number of element.
78
   */
79 2
  public function count() {
80 2
    return count($this->group);
81
  }
82
83
  /**
84
   * Convert the iterator into an array.
85
   *
86
   * @return array
87
   *   The elements.
88
   */
89 2
  public function toArray() {
90 2
    $data = array();
91
92 2
    for ($this->rewind(); $this->valid(); $this->next()) {
93 2
      $data[] = $this->current();
94
    }
95
96 2
    return $data;
97
  }
98
99
  /**
100
   * Set the group size.
101
   *
102
   * @param int $size
103
   *   The size.
104
   */
105 2
  public function setSize($size) {
106 2
    $this->size = $size;
107 2
    $this->computeGroup();
108 2
  }
109
110
  /**
111
   * Get the group size.
112
   *
113
   * @return int
114
   *   The size.
115
   */
116 2
  public function getSize() {
117 2
    return intval($this->size);
118
  }
119
120
  /**
121
   * Clean out the group from unwanted values.
122
   */
123 2
  private function computeGroup() {
124 2
    $this->group = array();
125
126 2
    foreach (range(1, $this->getSize() - 1) as $number) {
127 2
      if ($this->gcd($number, $this->getSize() - 1) == 1) {
128 2
        $this->group[] = $number;
129
      }
130
    }
131 2
  }
132
133
  /**
134
   * Get the greater common divisor between two numbers.
135
   *
136
   * @param int $a
137
   *   The first number.
138
   * @param int $b
139
   *   The second number.
140
   *
141
   * @return int
142
   *   The greater common divisor between $a and $b.
143
   */
144 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...
145 2
    return $b ? $this->gcd($b, $a % $b) : $a;
146
  }
147
148
  /**
149
   * Get the order.
150
   *
151
   * @param int $generator
152
   *   The generator.
153
   *
154
   * @return int
155
   *   The order.
156
   */
157
  public function order($generator) {
158
    $result = array();
159
160
    foreach (range(1, $this->getSize() - 1) as $number) {
161
      $value = pow($generator, $number) % $this->getSize();
162
      $result[$value] = $value;
163
    }
164
165
    return count($result);
166
  }
167
168
}
169