Update to EasyCodingStandard 4 (#273)

* update ECS config to v4

* composer: require Symplify 4

* apply coding-standard: use constants over functions, protected setUp() in tests, array indentation

* ecs: add false positive case

* composer: update lock

* bump to ECS 4.4

* update composer.lock

* shorten ECS config name

* ecs: ignore assignments in while()

* fix cs
This commit is contained in:
Tomáš Votruba 2018-06-15 07:57:45 +02:00 committed by Arkadiusz Kondas
parent 31604ce792
commit 46fa2c2cca
30 changed files with 821 additions and 765 deletions

View File

@ -28,8 +28,8 @@
"phpstan/phpstan-shim": "^0.9",
"phpstan/phpstan-strict-rules": "^0.9.0",
"phpunit/phpunit": "^7.0.0",
"symplify/coding-standard": "^3.1",
"symplify/easy-coding-standard": "^3.1"
"symplify/coding-standard": "^4.4",
"symplify/easy-coding-standard": "^4.4"
},
"config": {
"preferred-install": "dist",

1339
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,65 +0,0 @@
includes:
- vendor/symplify/easy-coding-standard/config/psr2.neon
- vendor/symplify/easy-coding-standard/config/php71.neon
- vendor/symplify/easy-coding-standard/config/clean-code.neon
- vendor/symplify/easy-coding-standard/config/common.neon
checkers:
# spacing
- PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer
- PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer
- PhpCsFixer\Fixer\CastNotation\CastSpacesFixer
PhpCsFixer\Fixer\Operator\ConcatSpaceFixer:
spacing: none
- PhpCsFixer\Fixer\ClassNotation\MethodSeparationFixer
- PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer
PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer:
positions: ['inside', 'outside']
PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer:
align_double_arrow: false
align_equals: false
# phpdoc
- PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer
# Symplify
- Symplify\CodingStandard\Fixer\Import\ImportNamespacedNameFixer
- Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer
- Symplify\CodingStandard\Fixer\Property\ArrayPropertyDefaultValueFixer
- Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer
parameters:
exclude_checkers:
# from strict.neon
- PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer
- PhpCsFixer\Fixer\Strict\StrictComparisonFixer
# personal prefference
- PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer
skip:
PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer:
# random_int() breaks code
- src/CrossValidation/RandomSplit.php
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
# magic calls
- src/Preprocessing/Normalizer.php
PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer:
# bugged
- src/Classification/DecisionTree/DecisionTreeLeaf.php
Symplify\CodingStandard\Fixer\Commenting\RemoveUselessDocBlockFixer:
# bug in fixer
- src/Math/LinearAlgebra/LUDecomposition.php
PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer:
# covariant return types
- src/Classification/Linear/Perceptron.php
skip_codes:
# missing typehints
- SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingParameterTypeHint
- SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversableParameterTypeHintSpecification
- SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingReturnTypeHint
- SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversableReturnTypeHintSpecification
- SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingPropertyTypeHint
- SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversablePropertyTypeHintSpecification
- PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff.Found

68
ecs.yml Normal file
View File

@ -0,0 +1,68 @@
imports:
- { resource: 'vendor/symplify/easy-coding-standard/config/psr2.yml' }
- { resource: 'vendor/symplify/easy-coding-standard/config/php71.yml' }
- { resource: 'vendor/symplify/easy-coding-standard/config/clean-code.yml' }
- { resource: 'vendor/symplify/easy-coding-standard/config/common.yml' }
services:
# spacing
PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer: ~
PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer: ~
PhpCsFixer\Fixer\CastNotation\CastSpacesFixer: ~
PhpCsFixer\Fixer\Operator\ConcatSpaceFixer:
spacing: none
PhpCsFixer\Fixer\ClassNotation\MethodSeparationFixer: ~
PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer: ~
PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer:
positions: ['inside', 'outside']
PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer:
align_double_arrow: false
align_equals: false
# phpdoc
PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer: ~
PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer: ~
# Symplify
Symplify\CodingStandard\Fixer\Import\ImportNamespacedNameFixer: ~
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer: ~
Symplify\CodingStandard\Fixer\Property\ArrayPropertyDefaultValueFixer: ~
Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer: ~
parameters:
exclude_checkers:
# from strict.neon
- 'PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer'
- 'PhpCsFixer\Fixer\Strict\StrictComparisonFixer'
# personal prefference
- 'PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer'
skip:
PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer:
# random_int() breaks code
- 'src/CrossValidation/RandomSplit.php'
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
# magic calls
- 'src/Preprocessing/Normalizer.php'
PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer:
# bugged
- 'src/Classification/DecisionTree/DecisionTreeLeaf.php'
Symplify\CodingStandard\Fixer\Commenting\RemoveUselessDocBlockFixer:
# false positive - already fixed in master
- 'src/Helper/OneVsRest.php'
# bug in fixer
- 'src/Math/LinearAlgebra/LUDecomposition.php'
PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer:
# covariant return types
- 'src/Classification/Linear/Perceptron.php'
# missing typehints
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingParameterTypeHint: ~
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversableParameterTypeHintSpecification: ~
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingReturnTypeHint: ~
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversableReturnTypeHintSpecification: ~
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingPropertyTypeHint: ~
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversablePropertyTypeHintSpecification: ~
# assignment in "while ($var = ...)" are ok
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff.FoundInWhileCondition:

View File

@ -452,9 +452,7 @@ class DecisionTree implements Classifier
$rNodes = $this->getSplitNodesByColumn($column, $node->rightLeaf);
}
$nodes = array_merge($nodes, $lNodes, $rNodes);
return $nodes;
return array_merge($nodes, $lNodes, $rNodes);
}
/**

View File

@ -9,6 +9,7 @@ use Phpml\Classification\Classifier;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Helper\OneVsRest;
use Phpml\Helper\Optimizer\GD;
use Phpml\Helper\Optimizer\Optimizer;
use Phpml\Helper\Optimizer\StochasticGD;
use Phpml\Helper\Predictable;
use Phpml\IncrementalEstimator;
@ -19,7 +20,7 @@ class Perceptron implements Classifier, IncrementalEstimator
use Predictable, OneVsRest;
/**
* @var \Phpml\Helper\Optimizer\Optimizer|GD|StochasticGD|null
* @var Optimizer|GD|StochasticGD|null
*/
protected $optimizer;

View File

@ -155,7 +155,7 @@ class NaiveBayes implements Classifier
// some libraries adopt taking log of calculations such as
// scikit-learn did.
// (See : https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/naive_bayes.py)
$pdf = -0.5 * log(2.0 * pi() * $std * $std);
$pdf = -0.5 * log(2.0 * M_PI * $std * $std);
$pdf -= 0.5 * pow($value - $mean, 2) / ($std * $std);
return $pdf;

View File

@ -197,7 +197,9 @@ class Space extends SplObjectStorage
$sum = random_int(0, (int) $sum);
foreach ($this as $point) {
if (($sum -= $distances[$point]) > 0) {
$sum -= $distances[$point];
if ($sum > 0) {
continue;
}

View File

@ -33,9 +33,7 @@ class StratifiedRandomSplit extends RandomSplit
$split[$targets[$key]][] = $sample;
}
$datasets = $this->createDatasets($uniqueTargets, $split);
return $datasets;
return $this->createDatasets($uniqueTargets, $split);
}
private function createDatasets(array $uniqueTargets, array $split): array

View File

@ -6,13 +6,7 @@ namespace Phpml\Dataset;
interface Dataset
{
/**
* @return array
*/
public function getSamples(): array;
/**
* @return array
*/
public function getTargets(): array;
}

View File

@ -79,9 +79,7 @@ class SvmDataset extends ArrayDataset
$line = rtrim($line);
$line = str_replace("\t", ' ', $line);
$columns = explode(' ', $line);
return $columns;
return explode(' ', $line);
}
private static function parseTargetColumn(string $column): float

View File

@ -6,15 +6,9 @@ namespace Phpml;
interface Estimator
{
/**
* @param array $samples
* @param array $targets
*/
public function train(array $samples, array $targets);
/**
* @param array $samples
*
* @return mixed
*/
public function predict(array $samples);

View File

@ -7,8 +7,6 @@ namespace Phpml\Helper;
trait Predictable
{
/**
* @param array $samples
*
* @return mixed
*/
public function predict(array $samples)
@ -26,8 +24,6 @@ trait Predictable
}
/**
* @param array $sample
*
* @return mixed
*/
abstract protected function predictSample(array $sample);

View File

@ -16,10 +16,6 @@ trait Trainable
*/
private $targets = [];
/**
* @param array $samples
* @param array $targets
*/
public function train(array $samples, array $targets): void
{
$this->samples = array_merge($this->samples, $samples);

View File

@ -6,10 +6,5 @@ namespace Phpml;
interface IncrementalEstimator
{
/**
* @param array $samples
* @param array $targets
* @param array $labels
*/
public function partialTrain(array $samples, array $targets, array $labels = []);
}

View File

@ -6,9 +6,5 @@ namespace Phpml\Math;
interface Distance
{
/**
* @param array $a
* @param array $b
*/
public function distance(array $a, array $b): float;
}

View File

@ -18,7 +18,7 @@ declare(strict_types=1);
* conditioned, or even singular, so the validity of the equation
* A = V*D*inverse(V) depends upon V.cond().
*
* @author Paul Meagher
* @author Paul Meagher
* @license PHP v3.0
*
* @version 1.1
@ -344,7 +344,7 @@ class EigenvalueDecomposition
$iter = 0;
do {
// Could check iteration count here.
$iter += 1;
++$iter;
// Compute implicit shift
$g = $this->d[$l];
$p = ($this->d[$l + 1] - $g) / (2.0 * $this->e[$l]);
@ -598,7 +598,7 @@ class EigenvalueDecomposition
$this->e[$n] = 0.0;
--$n;
$iter = 0;
// Two roots found
// Two roots found
} elseif ($l == $n - 1) {
$w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n];
$p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0;
@ -661,7 +661,7 @@ class EigenvalueDecomposition
$n = $n - 2;
$iter = 0;
// No convergence yet
// No convergence yet
} else {
// Form shift
$x = $this->H[$n][$n];

View File

@ -36,8 +36,6 @@ class Correlation
$b2 += pow($b, 2);
}
$corr = $axb / sqrt((float) ($a2 * $b2));
return $corr;
return $axb / sqrt((float) ($a2 * $b2));
}
}

View File

@ -34,7 +34,7 @@ class Gaussian
$std2 = $this->std ** 2;
$mean = $this->mean;
return exp(-(($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * pi());
return exp(-(($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * M_PI);
}
/**

View File

@ -226,8 +226,7 @@ class ClassificationReport
{
$labels = array_values(array_unique(array_merge($actualLabels, $predictedLabels)));
sort($labels);
$labels = array_combine($labels, array_fill(0, count($labels), 0));
return $labels;
return array_combine($labels, array_fill(0, count($labels), 0));
}
}

View File

@ -25,7 +25,7 @@ class ConfusionMatrix
$column = $labels[$predicted];
}
$matrix[$row][$column] += 1;
++$matrix[$row][$column];
}
return $matrix;
@ -47,8 +47,7 @@ class ConfusionMatrix
{
$labels = array_values(array_unique($labels));
sort($labels);
$labels = array_flip($labels);
return $labels;
return array_flip($labels);
}
}

View File

@ -6,9 +6,5 @@ namespace Phpml\NeuralNetwork;
interface Training
{
/**
* @param array $samples
* @param array $targets
*/
public function train(array $samples, array $targets);
}

View File

@ -7,8 +7,6 @@ namespace Phpml\Preprocessing\Imputer;
interface Strategy
{
/**
* @param array $currentAxis
*
* @return mixed
*/
public function replaceValue(array $currentAxis);

View File

@ -15,7 +15,7 @@ class ChebyshevTest extends TestCase
*/
private $distanceMetric;
public function setUp(): void
protected function setUp(): void
{
$this->distanceMetric = new Chebyshev();
}

View File

@ -15,7 +15,7 @@ class EuclideanTest extends TestCase
*/
private $distanceMetric;
public function setUp(): void
protected function setUp(): void
{
$this->distanceMetric = new Euclidean();
}

View File

@ -15,7 +15,7 @@ class ManhattanTest extends TestCase
*/
private $distanceMetric;
public function setUp(): void
protected function setUp(): void
{
$this->distanceMetric = new Manhattan();
}

View File

@ -15,7 +15,7 @@ class MinkowskiTest extends TestCase
*/
private $distanceMetric;
public function setUp(): void
protected function setUp(): void
{
$this->distanceMetric = new Minkowski();
}

View File

@ -136,10 +136,10 @@ class MatrixTest extends TestCase
{
$this->expectException(MatrixException::class);
$matrix = new Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
$matrix->inverse();
}

View File

@ -18,8 +18,8 @@ class WhitespaceTokenizerTest extends TestCase
Nulla vitae congue lorem.';
$tokens = ['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit.',
'Cras', 'consectetur,', 'dui', 'et', 'lobortis', 'auctor.',
'Nulla', 'vitae', 'congue', 'lorem.', ];
'Cras', 'consectetur,', 'dui', 'et', 'lobortis', 'auctor.',
'Nulla', 'vitae', 'congue', 'lorem.', ];
$this->assertEquals($tokens, $tokenizer->tokenize($text));
}
@ -33,8 +33,8 @@ class WhitespaceTokenizerTest extends TestCase
殍涾烰 齞齝囃 蹅輶 , 孻憵 擙樲橚 藒襓謥 岯岪弨 廞徲 孻憵懥 趡趛踠 ';
$tokens = ['鋍鞎', '鳼', '鞮鞢騉', '袟袘觕,', '炟砏', '蒮', '謺貙蹖', '偢偣唲', '蒛', '箷箯緷', '鑴鱱爧', '覮轀,',
'剆坲', '煘煓瑐', '鬐鶤鶐', '飹勫嫢', '銪', '餀', '枲柊氠', '鍎鞚韕', '焲犈,',
'殍涾烰', '齞齝囃', '蹅輶', '鄜,', '孻憵', '擙樲橚', '藒襓謥', '岯岪弨', '蒮', '廞徲', '孻憵懥', '趡趛踠', '槏', ];
'剆坲', '煘煓瑐', '鬐鶤鶐', '飹勫嫢', '銪', '餀', '枲柊氠', '鍎鞚韕', '焲犈,',
'殍涾烰', '齞齝囃', '蹅輶', '鄜,', '孻憵', '擙樲橚', '藒襓謥', '岯岪弨', '蒮', '廞徲', '孻憵懥', '趡趛踠', '槏', ];
$this->assertEquals($tokens, $tokenizer->tokenize($text));
}

View File

@ -18,8 +18,8 @@ class WordTokenizerTest extends TestCase
Nulla vitae ,.,/ congue lorem.';
$tokens = ['Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
'Cras', 'consectetur', 'dui', 'et', 'lobortis', 'auctor',
'Nulla', 'vitae', 'congue', 'lorem', ];
'Cras', 'consectetur', 'dui', 'et', 'lobortis', 'auctor',
'Nulla', 'vitae', 'congue', 'lorem', ];
$this->assertEquals($tokens, $tokenizer->tokenize($text));
}
@ -33,8 +33,8 @@ class WordTokenizerTest extends TestCase
殍涾烰 齞齝囃 蹅輶 , 孻憵 擙樲橚 藒襓謥 岯岪弨 廞徲 孻憵懥 趡趛踠 ';
$tokens = ['鋍鞎', '鞮鞢騉', '袟袘觕', '炟砏', '謺貙蹖', '偢偣唲', '箷箯緷', '鑴鱱爧', '覮轀',
'剆坲', '煘煓瑐', '鬐鶤鶐', '飹勫嫢', '枲柊氠', '鍎鞚韕', '焲犈',
'殍涾烰', '齞齝囃', '蹅輶', '孻憵', '擙樲橚', '藒襓謥', '岯岪弨', '廞徲', '孻憵懥', '趡趛踠', ];
'剆坲', '煘煓瑐', '鬐鶤鶐', '飹勫嫢', '枲柊氠', '鍎鞚韕', '焲犈',
'殍涾烰', '齞齝囃', '蹅輶', '孻憵', '擙樲橚', '藒襓謥', '岯岪弨', '廞徲', '孻憵懥', '趡趛踠', ];
$this->assertEquals($tokens, $tokenizer->tokenize($text));
}