Passed
03:28
created

### Cluster   A

#### Complexity

 Total Complexity 17

#### Size/Duplication

 Total Lines 106 Duplicated Lines 5.66 %

#### Coupling/Cohesion

 Components 1 Dependencies 2

#### Importance

 Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 6
loc 106
rs 10
c 0
b 0
f 0

#### 11 Methods

Rating   Name   Duplication   Size   Complexity
A __construct() 0 6 1
A getPoints() 0 9 2
A toArray() 0 7 1
A attach() 0 10 2
A detach() 0 6 1
A attachAll() 0 4 1
A detachAll() 0 4 1
B updateCentroid() 6 19 5
A getIterator() 0 4 1
A count() 0 4 1
A setCoordinates() 0 4 1

#### Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

 1 `space = \$space;` 28 ` \$this->points = new SplObjectStorage();` 29 ` }` 30 31 ` public function getPoints(): array` 32 ` {` 33 ` \$points = [];` 34 ` foreach (\$this->points as \$point) {` 35 ` \$points[] = \$point->toArray();` 36 ` }` 37 38 ` return \$points;` 39 ` }` 40 41 ` public function toArray(): array` 42 ` {` 43 ` return [` 44 ` 'centroid' => parent::toArray(),` 45 ` 'points' => \$this->getPoints(),` 46 ` ];` 47 ` }` 48 49 ` public function attach(Point \$point): Point` 50 ` {` 51 ` if (\$point instanceof self) {` 52 ` throw new LogicException('cannot attach a cluster to another');` 53 ` }` 54 55 ` \$this->points->attach(\$point);` 56 57 ` return \$point;` 58 ` }` 59 60 ` public function detach(Point \$point): Point` 61 ` {` 62 ` \$this->points->detach(\$point);` 63 64 ` return \$point;` 65 ` }` 66 67 ` public function attachAll(SplObjectStorage \$points): void` 68 ` {` 69 ` \$this->points->addAll(\$points);` 70 ` }` 71 72 ` public function detachAll(SplObjectStorage \$points): void` 73 ` {` 74 ` \$this->points->removeAll(\$points);` 75 ` }` 76 77 ` public function updateCentroid(): void` 78 ` {` 79 ` \$count = count(\$this->points);` 80 ` if (!\$count) {` 81 ` return;` 82 ` }` 83 84 ` \$centroid = \$this->space->newPoint(array_fill(0, \$this->dimension, 0));` 85 86 ` foreach (\$this->points as \$point) {` 87 View Code Duplication ` for (\$n = 0; \$n < \$this->dimension; ++\$n) {` 0 ignored issues – show Duplication introduced 2016-05-01 21:46 UTC by This code seems to be duplicated across your project. Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history... 88 ` \$centroid->coordinates[\$n] += \$point->coordinates[\$n];` 89 ` }` 90 ` }` 91 92 View Code Duplication ` for (\$n = 0; \$n < \$this->dimension; ++\$n) {` 0 ignored issues – show Duplication introduced 2016-05-01 21:46 UTC by This code seems to be duplicated across your project. Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history... 93 ` \$this->coordinates[\$n] = \$centroid->coordinates[\$n] / \$count;` 94 ` }` 95 ` }` 96 97 ` /**` 98 ` * @return Point[]|SplObjectStorage` 99 ` */` 100 ` public function getIterator()` 101 ` {` 102 ` return \$this->points;` 0 ignored issues – show Bug Compatibility introduced 2017-08-04 08:58 UTC by The expression `\$this->points;` of type `SplObjectStorage|Phpml\Clustering\KMeans\Point[]` adds the type `Phpml\Clustering\KMeans\Point[]` to the return on line 102 which is incompatible with the return type declared by the interface `IteratorAggregate::getIterator` of type `Traversable`. Loading history... 103 ` }` 104 105 ` /**` 106 ` * @return mixed` 107 ` */` 108 ` public function count()` 109 ` {` 110 ` return count(\$this->points);` 111 ` }` 112 113 ` public function setCoordinates(array \$newCoordinates): void` 114 ` {` 115 ` \$this->coordinates = \$newCoordinates;` 116 ` }` 117 `}` 118