psr12 compliance

This commit is contained in:
Benjamin Delespierre 2021-04-02 00:21:27 +02:00
parent 5cb2784379
commit 86957271f9
4 changed files with 16 additions and 13 deletions

View File

@ -1,4 +1,5 @@
<?php
/**
* This file is part of PHP K-Means
*
@ -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
@ -84,12 +85,12 @@ class Cluster extends Point implements \IteratorAggregate, \Countable
$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;
}
}

View File

@ -1,4 +1,5 @@
<?php
/**
* This file is part of PHP K-Means
*
@ -53,7 +54,7 @@ class Point implements \ArrayAccess
}
$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;
}

View File

@ -1,4 +1,5 @@
<?php
/**
* This file is part of PHP K-Means
*
@ -95,7 +96,7 @@ class Space extends \SplObjectStorage
$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];
}
@ -114,7 +115,7 @@ class Space extends \SplObjectStorage
$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]);
}
@ -154,7 +155,7 @@ class Space extends \SplObjectStorage
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());
}
@ -169,8 +170,8 @@ class Space extends \SplObjectStorage
$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) {
@ -181,11 +182,11 @@ class Space extends \SplObjectStorage
// 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);

View File

@ -106,7 +106,7 @@ class ClusterTest extends TestCase
new Point($space, [2,2]),
];
$storage = new \SplObjectStorage;
$storage = new \SplObjectStorage();
foreach ($points as $point) {
$storage->attach($point);
}
@ -129,7 +129,7 @@ class ClusterTest extends TestCase
$cluster->attach($point);
}
$storage = new \SplObjectStorage;
$storage = new \SplObjectStorage();
foreach ($points as $point) {
$storage->attach($point);
}