Skip to content

Commit

Permalink
Inline static constructors of exceptions (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-uti authored and akondas committed Mar 3, 2018
1 parent af9ccfe commit cbd9f5f
Show file tree
Hide file tree
Showing 36 changed files with 66 additions and 214 deletions.
4 changes: 3 additions & 1 deletion src/Classification/MLPClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class MLPClassifier extends MultilayerPerceptron implements Classifier
public function getTargetClass($target): int
{
if (!in_array($target, $this->classes, true)) {
throw InvalidArgumentException::invalidTarget($target);
throw new InvalidArgumentException(
sprintf('Target with value "%s" is not part of the accepted classes', $target)
);
}

return array_search($target, $this->classes, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Clustering/FuzzyCMeans.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FuzzyCMeans implements Clusterer
public function __construct(int $clustersNumber, float $fuzziness = 2.0, float $epsilon = 1e-2, int $maxIterations = 100)
{
if ($clustersNumber <= 0) {
throw InvalidArgumentException::invalidClustersNumber();
throw new InvalidArgumentException('Invalid clusters number');
}

$this->clustersNumber = $clustersNumber;
Expand Down
2 changes: 1 addition & 1 deletion src/Clustering/KMeans.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KMeans implements Clusterer
public function __construct(int $clustersNumber, int $initialization = self::INIT_KMEANS_PLUS_PLUS)
{
if ($clustersNumber <= 0) {
throw InvalidArgumentException::invalidClustersNumber();
throw new InvalidArgumentException('Invalid clusters number');
}

$this->clustersNumber = $clustersNumber;
Expand Down
2 changes: 1 addition & 1 deletion src/CrossValidation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class Split
public function __construct(Dataset $dataset, float $testSize = 0.3, ?int $seed = null)
{
if ($testSize <= 0 || $testSize >= 1) {
throw InvalidArgumentException::percentNotInRange('testSize');
throw new InvalidArgumentException('testsize must be between 0.0 and 1.0');
}

$this->seedGenerator($seed);
Expand Down
2 changes: 1 addition & 1 deletion src/Dataset/ArrayDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ArrayDataset implements Dataset
public function __construct(array $samples, array $targets)
{
if (count($samples) != count($targets)) {
throw InvalidArgumentException::arraySizeNotMatch();
throw new InvalidArgumentException('Size of given arrays does not match');
}

$this->samples = $samples;
Expand Down
4 changes: 2 additions & 2 deletions src/Dataset/CsvDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class CsvDataset extends ArrayDataset
public function __construct(string $filepath, int $features, bool $headingRow = true, string $delimiter = ',', int $maxLineLength = 0)
{
if (!file_exists($filepath)) {
throw FileException::missingFile(basename($filepath));
throw new FileException(sprintf('File "%s" missing.', basename($filepath)));
}

$handle = fopen($filepath, 'rb');
if ($handle === false) {
throw FileException::cantOpenFile(basename($filepath));
throw new FileException(sprintf('File "%s" can\'t be open.', basename($filepath)));
}

if ($headingRow) {
Expand Down
2 changes: 1 addition & 1 deletion src/Dataset/FilesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FilesDataset extends ArrayDataset
public function __construct(string $rootPath)
{
if (!is_dir($rootPath)) {
throw DatasetException::missingFolder($rootPath);
throw new DatasetException(sprintf('Dataset root folder "%s" missing.', $rootPath));
}

$this->scanRootPath($rootPath);
Expand Down
14 changes: 7 additions & 7 deletions src/Dataset/SvmDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ private static function readProblem(string $filePath): array
private static function openFile(string $filePath)
{
if (!file_exists($filePath)) {
throw FileException::missingFile(basename($filePath));
throw new FileException(sprintf('File "%s" missing.', basename($filePath)));
}

$handle = fopen($filePath, 'rb');
if ($handle === false) {
throw FileException::cantOpenFile(basename($filePath));
throw new FileException(sprintf('File "%s" can\'t be open.', basename($filePath)));
}

return $handle;
Expand Down Expand Up @@ -87,7 +87,7 @@ private static function parseLine(string $line): array
private static function parseTargetColumn(string $column): float
{
if (!is_numeric($column)) {
throw DatasetException::invalidTarget($column);
throw new DatasetException(sprintf('Invalid target "%s".', $column));
}

return (float) $column;
Expand All @@ -97,7 +97,7 @@ private static function parseFeatureColumn(string $column): array
{
$feature = explode(':', $column, 2);
if (count($feature) != 2) {
throw DatasetException::invalidValue($column);
throw new DatasetException(sprintf('Invalid value "%s".', $column));
}

$index = self::parseFeatureIndex($feature[0]);
Expand All @@ -109,11 +109,11 @@ private static function parseFeatureColumn(string $column): array
private static function parseFeatureIndex(string $index): int
{
if (!is_numeric($index) || !ctype_digit($index)) {
throw DatasetException::invalidIndex($index);
throw new DatasetException(sprintf('Invalid index "%s".', $index));
}

if ((int) $index < 1) {
throw DatasetException::invalidIndex($index);
throw new DatasetException(sprintf('Invalid index "%s".', $index));
}

return (int) $index - 1;
Expand All @@ -122,7 +122,7 @@ private static function parseFeatureIndex(string $index): int
private static function parseFeatureValue(string $value): float
{
if (!is_numeric($value)) {
throw DatasetException::invalidValue($value);
throw new DatasetException(sprintf('Invalid value "%s".', $value));
}

return (float) $value;
Expand Down
19 changes: 0 additions & 19 deletions src/Exception/DatasetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,4 @@

class DatasetException extends Exception
{
public static function missingFolder(string $path): self
{
return new self(sprintf('Dataset root folder "%s" missing.', $path));
}

public static function invalidTarget(string $target): self
{
return new self(sprintf('Invalid target "%s".', $target));
}

public static function invalidIndex(string $index): self
{
return new self(sprintf('Invalid index "%s".', $index));
}

public static function invalidValue(string $value): self
{
return new self(sprintf('Invalid value "%s".', $value));
}
}
14 changes: 0 additions & 14 deletions src/Exception/FileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,4 @@

class FileException extends Exception
{
public static function missingFile(string $filepath): self
{
return new self(sprintf('File "%s" missing.', $filepath));
}

public static function cantOpenFile(string $filepath): self
{
return new self(sprintf('File "%s" can\'t be open.', $filepath));
}

public static function cantSaveFile(string $filepath): self
{
return new self(sprintf('File "%s" can\'t be saved.', $filepath));
}
}
92 changes: 0 additions & 92 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,4 @@

class InvalidArgumentException extends Exception
{
public static function arraySizeNotMatch(): self
{
return new self('Size of given arrays does not match');
}

public static function percentNotInRange($name): self
{
return new self(sprintf('%s must be between 0.0 and 1.0', $name));
}

public static function arrayCantBeEmpty(): self
{
return new self('The array has zero elements');
}

public static function arraySizeTooSmall(int $minimumSize = 2): self
{
return new self(sprintf('The array must have at least %d elements', $minimumSize));
}

public static function matrixDimensionsDidNotMatch(): self
{
return new self('Matrix dimensions did not match');
}

public static function inconsistentMatrixSupplied(): self
{
return new self('Inconsistent matrix supplied');
}

public static function invalidClustersNumber(): self
{
return new self('Invalid clusters number');
}

/**
* @param mixed $target
*/
public static function invalidTarget($target): self
{
return new self(sprintf('Target with value "%s" is not part of the accepted classes', $target));
}

public static function invalidStopWordsLanguage(string $language): self
{
return new self(sprintf('Can\'t find "%s" language for StopWords', $language));
}

public static function invalidLayerNodeClass(): self
{
return new self('Layer node class must implement Node interface');
}

public static function invalidLayersNumber(): self
{
return new self('Provide at least 1 hidden layer');
}

public static function invalidClassesNumber(): self
{
return new self('Provide at least 2 different classes');
}

public static function inconsistentClasses(): self
{
return new self('The provided classes don\'t match the classes provided in the constructor');
}

public static function fileNotFound(string $file): self
{
return new self(sprintf('File "%s" not found', $file));
}

public static function fileNotExecutable(string $file): self
{
return new self(sprintf('File "%s" is not executable', $file));
}

public static function pathNotFound(string $path): self
{
return new self(sprintf('The specified path "%s" does not exist', $path));
}

public static function pathNotWritable(string $path): self
{
return new self(sprintf('The specified path "%s" is not writable', $path));
}

public static function invalidOperator(string $operator): self
{
return new self(sprintf('Invalid operator "%s" provided', $operator));
}
}
4 changes: 0 additions & 4 deletions src/Exception/LibsvmCommandException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@

class LibsvmCommandException extends Exception
{
public static function failedToRun(string $command, string $reason): self
{
return new self(sprintf('Failed running libsvm command: "%s" with reason: "%s"', $command, $reason));
}
}
14 changes: 0 additions & 14 deletions src/Exception/MatrixException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,4 @@

class MatrixException extends Exception
{
public static function notSquareMatrix(): self
{
return new self('Matrix is not square matrix');
}

public static function columnOutOfRange(): self
{
return new self('Column out of range');
}

public static function singularMatrix(): self
{
return new self('Matrix is singular');
}
}
4 changes: 0 additions & 4 deletions src/Exception/NormalizerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@

class NormalizerException extends Exception
{
public static function unknownNorm(): self
{
return new self('Unknown norm supplied.');
}
}
9 changes: 0 additions & 9 deletions src/Exception/SerializeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,4 @@

class SerializeException extends Exception
{
public static function cantUnserialize(string $filepath): self
{
return new self(sprintf('"%s" can not be unserialized.', $filepath));
}

public static function cantSerialize(string $classname): self
{
return new self(sprintf('Class "%s" can not be serialized.', $classname));
}
}
2 changes: 1 addition & 1 deletion src/FeatureExtraction/StopWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function factory(string $language = 'English'): self
$className = __NAMESPACE__."\\StopWords\\${language}";

if (!class_exists($className)) {
throw InvalidArgumentException::invalidStopWordsLanguage($language);
throw new InvalidArgumentException(sprintf('Can\'t find "%s" language for StopWords', $language));
}

return new $className();
Expand Down
2 changes: 1 addition & 1 deletion src/FeatureSelection/SelectKBest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(int $k = 10, ?ScoringFunction $scoringFunction = nul
public function fit(array $samples, ?array $targets = null): void
{
if ($targets === null || empty($targets)) {
throw InvalidArgumentException::arrayCantBeEmpty();
throw new InvalidArgumentException('The array has zero elements');
}

$this->scores = $sorted = $this->scoringFunction->score($samples, $targets);
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function compare($a, $b, string $operator): bool
case '!==':
return $a !== $b;
default:
throw InvalidArgumentException::invalidOperator($operator);
throw new InvalidArgumentException(sprintf('Invalid operator "%s" provided', $operator));
}
}
}
2 changes: 1 addition & 1 deletion src/Math/Distance/Chebyshev.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Chebyshev implements Distance
public function distance(array $a, array $b): float
{
if (count($a) !== count($b)) {
throw InvalidArgumentException::arraySizeNotMatch();
throw new InvalidArgumentException('Size of given arrays does not match');
}

$differences = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Distance/Euclidean.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Euclidean implements Distance
public function distance(array $a, array $b): float
{
if (count($a) !== count($b)) {
throw InvalidArgumentException::arraySizeNotMatch();
throw new InvalidArgumentException('Size of given arrays does not match');
}

$distance = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Distance/Manhattan.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Manhattan implements Distance
public function distance(array $a, array $b): float
{
if (count($a) !== count($b)) {
throw InvalidArgumentException::arraySizeNotMatch();
throw new InvalidArgumentException('Size of given arrays does not match');
}

return array_sum(array_map(function ($m, $n) {
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Distance/Minkowski.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(float $lambda = 3.0)
public function distance(array $a, array $b): float
{
if (count($a) !== count($b)) {
throw InvalidArgumentException::arraySizeNotMatch();
throw new InvalidArgumentException('Size of given arrays does not match');
}

$distance = 0;
Expand Down
Loading

0 comments on commit cbd9f5f

Please sign in to comment.