Skip to content

Commit

Permalink
psr12 compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
bdelespierre committed Apr 1, 2021
1 parent 5cb2784 commit 8695727
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/KMeans/Cluster.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of PHP K-Means
*
Expand Down Expand Up @@ -33,7 +34,7 @@ class Cluster extends Point implements \IteratorAggregate, \Countable
public function __construct(Space $space, array $coordinates)
{
parent::__construct($space, $coordinates);
$this->points = new \SplObjectStorage;
$this->points = new \SplObjectStorage();
}

public function toArray(): array
Expand Down Expand Up @@ -84,12 +85,12 @@ public function updateCentroid(): void
$centroid = $this->space->newPoint(array_fill(0, $this->dimention, 0));

foreach ($this->points as $point) {
for ($n=0; $n<$this->dimention; $n++) {
for ($n = 0; $n < $this->dimention; $n++) {
$centroid->coordinates[$n] += $point->coordinates[$n];
}
}

for ($n=0; $n<$this->dimention; $n++) {
for ($n = 0; $n < $this->dimention; $n++) {
$this->coordinates[$n] = $centroid->coordinates[$n] / $count;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/KMeans/Point.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of PHP K-Means
*
Expand Down Expand Up @@ -53,7 +54,7 @@ public function getDistanceWith(self $point, bool $precise = true): float
}

$distance = 0;
for ($n=0; $n<$this->dimention; $n++) {
for ($n = 0; $n < $this->dimention; $n++) {
$difference = $this->coordinates[$n] - $point->coordinates[$n];
$distance += $difference * $difference;
}
Expand Down
15 changes: 8 additions & 7 deletions src/KMeans/Space.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of PHP K-Means
*
Expand Down Expand Up @@ -95,7 +96,7 @@ public function getBoundaries(): array
$max = $this->newPoint(array_fill(0, $this->dimention, null));

foreach ($this as $point) {
for ($n=0; $n < $this->dimention; $n++) {
for ($n = 0; $n < $this->dimention; $n++) {
if ($min[$n] === null || $min[$n] > $point[$n]) {
$min[$n] = $point[$n];
}
Expand All @@ -114,7 +115,7 @@ public function getRandomPoint(Point $min, Point $max): Point
$point = $this->newPoint(array_fill(0, $this->dimention, null));
$rng = static::$rng;

for ($n=0; $n < $this->dimention; $n++) {
for ($n = 0; $n < $this->dimention; $n++) {
$point[$n] = $rng($min[$n], $max[$n]);
}

Expand Down Expand Up @@ -154,7 +155,7 @@ protected function initializeClusters(int $nbClusters): array
list($min, $max) = $this->getBoundaries();

// initialize N clusters with a random point within space boundaries
for ($n=0; $n<$nbClusters; $n++) {
for ($n = 0; $n < $nbClusters; $n++) {
$clusters[] = new Cluster($this, $this->getRandomPoint($min, $max)->getCoordinates());
}

Expand All @@ -169,8 +170,8 @@ protected function iterate(array $clusters): bool
$continue = false;

// migration storages
$attach = new \SplObjectStorage;
$detach = new \SplObjectStorage;
$attach = new \SplObjectStorage();
$detach = new \SplObjectStorage();

// calculate proximity amongst points and clusters
foreach ($clusters as $cluster) {
Expand All @@ -181,11 +182,11 @@ protected function iterate(array $clusters): bool
// move the point from its old cluster to its closest
if ($closest !== $cluster) {
if (! isset($attach[$closest])) {
$attach[$closest] = new \SplObjectStorage;
$attach[$closest] = new \SplObjectStorage();
}

if (! isset($detach[$cluster])) {
$detach[$cluster] = new \SplObjectStorage;
$detach[$cluster] = new \SplObjectStorage();
}

$attach[$closest]->attach($point);
Expand Down
4 changes: 2 additions & 2 deletions tests/Kmeans/ClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testAttachAll()
new Point($space, [2,2]),
];

$storage = new \SplObjectStorage;
$storage = new \SplObjectStorage();
foreach ($points as $point) {
$storage->attach($point);
}
Expand All @@ -129,7 +129,7 @@ public function testDetachAll()
$cluster->attach($point);
}

$storage = new \SplObjectStorage;
$storage = new \SplObjectStorage();
foreach ($points as $point) {
$storage->attach($point);
}
Expand Down

0 comments on commit 8695727

Please sign in to comment.