From 3b2d59cf81459916f4d6c9e0746d693e9470d229 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Fri, 19 Oct 2018 22:28:28 +0900 Subject: [PATCH] CS and appearance updates, strict comparison everywhere in tests --- .php_cs.dist | 30 ++++++++++++++++++- Makefile | 2 +- example.php | 4 +-- src/Principal.php | 6 ++-- src/Standard.php | 6 ++-- tests/ArraysTest.php | 6 ++-- tests/EdgeCasesTest.php | 20 ++++++------- tests/ErrorsTest.php | 2 +- tests/IterableTest.php | 14 ++++----- tests/LeaguePipelineTest.php | 4 +-- tests/StandardTest.php | 56 ++++++++++++++++++------------------ tests/UnpackTest.php | 4 +-- 12 files changed, 91 insertions(+), 63 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 78bc9fa..362f8cc 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -1,4 +1,21 @@ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare(strict_types=1); $header = <<<'EOF' Copyright 2017, 2018 Alexey Kopytko @@ -20,6 +37,9 @@ return PhpCsFixer\Config::create() ->setRiskyAllowed(true) ->setRules([ '@Symfony' => true, + '@Symfony:risky' => true, + '@PHP70Migration:risky' => true, + '@PHP71Migration:risky' => true, 'array_syntax' => ['syntax' => 'short'], 'declare_strict_types' => true, 'explicit_indirect_variable' => true, @@ -31,13 +51,21 @@ return PhpCsFixer\Config::create() 'non_printable_character' => true, 'ordered_imports' => true, 'php_unit_test_class_requires_covers' => true, + 'php_unit_strict' => true, 'phpdoc_add_missing_param_annotation' => true, 'phpdoc_order' => true, 'visibility_required' => true, 'header_comment' => ['header' => $header, 'separate' => 'bottom', 'location' => 'after_open'], 'ternary_to_null_coalescing' => true, - 'yoda_style' => null, + 'yoda_style' => true, 'phpdoc_to_comment' => false, + 'strict_comparison' => true, + 'is_null' => true, + 'function_to_constant' => true, + 'void_return' => false, + 'return_assignment' => true, + 'array_syntax' => ['syntax' => 'short'], + 'array_indentation' => true, ]) ->setFinder( PhpCsFixer\Finder::create() diff --git a/Makefile b/Makefile index 9845e9f..5e66656 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ COMPOSER=$(PHP) $(shell which composer) # Infection INFECTION=vendor/bin/infection MIN_MSI=90 -MIN_COVERED_MSI=100 +MIN_COVERED_MSI=90 INFECTION_ARGS=--min-msi=$(MIN_MSI) --min-covered-msi=$(MIN_COVERED_MSI) --threads=$(JOBS) --coverage=build/logs --log-verbosity=default --show-mutations all: test diff --git a/example.php b/example.php index aaf8ae0..aadfe36 100644 --- a/example.php +++ b/example.php @@ -30,8 +30,8 @@ // next processing step $pipeline->map(function ($value) { - yield pow($value, 2); - yield pow($value, 3); + yield $value ** 2; + yield $value ** 3; }); // simple one-to-one mapper diff --git a/src/Principal.php b/src/Principal.php index 7c05378..37aaa75 100644 --- a/src/Principal.php +++ b/src/Principal.php @@ -108,7 +108,7 @@ public function filter(callable $func) } // We got an array, that's what we need. Moving along. - if (is_array($this->pipeline)) { + if (\is_array($this->pipeline)) { $this->pipeline = array_filter($this->pipeline, $func); return $this; @@ -153,7 +153,7 @@ public function toArray(): array } // We got what we need, moving along - if (is_array($this->pipeline)) { + if (\is_array($this->pipeline)) { return array_values($this->pipeline); } @@ -173,7 +173,7 @@ public function toArray(): array */ public function reduce(callable $func, $initial) { - if (is_array($this->pipeline)) { + if (\is_array($this->pipeline)) { return array_reduce($this->pipeline, $func, $initial); } diff --git a/src/Standard.php b/src/Standard.php index a3adca0..e1763bb 100644 --- a/src/Standard.php +++ b/src/Standard.php @@ -53,7 +53,7 @@ public function unpack(callable $func = null): self */ public function map(callable $func = null): self { - if (is_null($func)) { + if (null === $func) { return $this; } @@ -75,7 +75,7 @@ public function filter(callable $func = null): self }; // Strings usually are internal functions, which require exact number of parameters. - if (is_string($func)) { + if (\is_string($func)) { $func = static function ($value) use ($func) { return $func($value); }; @@ -94,7 +94,7 @@ public function filter(callable $func = null): self */ public function reduce(callable $func = null, $initial = null) { - if (is_null($func)) { + if (null === $func) { return parent::reduce(static function ($carry, $item) { /** @psalm-suppress MixedOperand */ $carry += $item; diff --git a/tests/ArraysTest.php b/tests/ArraysTest.php index ec8f757..d05ea13 100644 --- a/tests/ArraysTest.php +++ b/tests/ArraysTest.php @@ -35,7 +35,7 @@ public function testInitialCallbackNotGenerator() return PHP_INT_MAX; }); - $this->assertEquals([PHP_INT_MAX], iterator_to_array($pipeline)); + $this->assertSame([PHP_INT_MAX], iterator_to_array($pipeline)); } public function testArrayToArray() @@ -45,7 +45,7 @@ public function testArrayToArray() return 42; }); - $this->assertEquals([42], $pipeline->toArray()); + $this->assertSame([42], $pipeline->toArray()); } public function testArrayFilter() @@ -55,7 +55,7 @@ public function testArrayFilter() return false; })->filter()->filter(); - $this->assertEquals([], $pipeline->toArray()); + $this->assertSame([], $pipeline->toArray()); } public function testArrayReduce() diff --git a/tests/EdgeCasesTest.php b/tests/EdgeCasesTest.php index 16dce34..d8daeb9 100644 --- a/tests/EdgeCasesTest.php +++ b/tests/EdgeCasesTest.php @@ -33,7 +33,7 @@ public function testStandardStringFunctions() $pipeline = new Standard(new \ArrayIterator([1, 2, 'foo', 'bar'])); $pipeline->filter('is_int'); - $this->assertEquals([1, 2], iterator_to_array($pipeline)); + $this->assertSame([1, 2], iterator_to_array($pipeline)); } public function testFilterAnyFalseValue() @@ -61,7 +61,7 @@ public function testMapUnprimed() return 1; }); - $this->assertEquals([1], $pipeline->toArray()); + $this->assertSame([1], $pipeline->toArray()); } public function testFilterUnprimed() @@ -69,7 +69,7 @@ public function testFilterUnprimed() $pipeline = new Standard(); $pipeline->filter()->unpack(); - $this->assertEquals([], $pipeline->toArray()); + $this->assertSame([], $pipeline->toArray()); } public function testUnpackUnprimed() @@ -79,7 +79,7 @@ public function testUnpackUnprimed() return 1; }); - $this->assertEquals([1], $pipeline->toArray()); + $this->assertSame([1], $pipeline->toArray()); } public function testInitialInvokeReturnsScalar() @@ -87,7 +87,7 @@ public function testInitialInvokeReturnsScalar() $pipeline = new Standard(); $pipeline->map($this); - $this->assertEquals([null], iterator_to_array($pipeline)); + $this->assertSame([null], iterator_to_array($pipeline)); } private function firstValueFromIterator(\Iterator $iterator) @@ -106,11 +106,11 @@ public function testIteratorIterator() $iterator = new \IteratorIterator($pipeline); /* @var $iterator \Iterator */ - $this->assertEquals(42, $this->firstValueFromIterator($iterator)); + $this->assertSame(42, $this->firstValueFromIterator($iterator)); $pipeline = new Standard(new \ArrayIterator([42])); $iterator = new \IteratorIterator($pipeline); - $this->assertEquals(42, $this->firstValueFromIterator($iterator)); + $this->assertSame(42, $this->firstValueFromIterator($iterator)); } private function pipelineWithNonUniqueKeys(): Standard @@ -131,12 +131,12 @@ private function pipelineWithNonUniqueKeys(): Standard public function testIteratorToArrayWithSameKeys() { - $this->assertEquals([3, 4], iterator_to_array($this->pipelineWithNonUniqueKeys())); + $this->assertSame([3, 4], iterator_to_array($this->pipelineWithNonUniqueKeys())); } public function testIteratorToArrayWithAllValues() { - $this->assertEquals([2, 3, 3, 4], $this->pipelineWithNonUniqueKeys()->toArray()); + $this->assertSame([2, 3, 3, 4], $this->pipelineWithNonUniqueKeys()->toArray()); } public function testPipelineInvokeReturnsGenerator() @@ -150,7 +150,7 @@ public function testInvokeMaps() $pipeline = new Standard(new \ArrayIterator(range(1, 5))); $pipeline->map($this); - $this->assertEquals(range(1, 5), iterator_to_array($pipeline)); + $this->assertSame(range(1, 5), iterator_to_array($pipeline)); } public function __invoke($default = null) diff --git a/tests/ErrorsTest.php b/tests/ErrorsTest.php index ff915cb..8267b4b 100644 --- a/tests/ErrorsTest.php +++ b/tests/ErrorsTest.php @@ -63,7 +63,7 @@ public function testPipelineInPipelineUsesSelf() }); $pipeline->map($pipeline)->filter(function ($i) { - return $i % 2 != 0; + return 0 !== $i % 2; }); $this->expectExceptionMessage('Cannot rewind a generator that was already run'); diff --git a/tests/IterableTest.php b/tests/IterableTest.php index d3d9aca..3da3493 100644 --- a/tests/IterableTest.php +++ b/tests/IterableTest.php @@ -32,7 +32,7 @@ class IterableTest extends TestCase public static function setUpBeforeClass() { - if (PHP_VERSION_ID < 70100) { + if (\PHP_VERSION_ID < 70100) { self::$usesIterable = false; return; @@ -53,13 +53,13 @@ protected function setUp() public function testArrayToArray() { $pipeline = new Standard([1, 2, 3]); - $this->assertEquals([1, 2, 3], $pipeline->toArray()); + $this->assertSame([1, 2, 3], $pipeline->toArray()); } public function testArrayToIterator() { $pipeline = new Standard([1, 2, 3]); - $this->assertEquals([1, 2, 3], iterator_to_array($pipeline)); + $this->assertSame([1, 2, 3], iterator_to_array($pipeline)); } public function testEmptyArrayStaysEmpty() @@ -71,19 +71,19 @@ public function testEmptyArrayStaysEmpty() yield $value; })->filter()->unpack(); - $this->assertEquals([], $pipeline->toArray()); + $this->assertSame([], $pipeline->toArray()); } public function testArrayFilter() { $pipeline = new Standard([0, 1, 2, 3, 0]); - $this->assertEquals([1, 2, 3], $pipeline->filter()->toArray()); + $this->assertSame([1, 2, 3], $pipeline->filter()->toArray()); } public function testArrayMap() { $pipeline = new Standard([1 => 0, 1, 2, 3]); - $this->assertEquals([0 => 0, 1, 2, 3], $pipeline->map(function ($value) { + $this->assertSame([0 => 0, 1, 2, 3], $pipeline->map(function ($value) { return $value; })->toArray()); } @@ -91,7 +91,7 @@ public function testArrayMap() public function testArrayMapFilter() { $pipeline = new Standard([1 => 0, 1, 2, 3]); - $this->assertEquals([0 => 1, 2, 3], $pipeline->map(function ($value) { + $this->assertSame([0 => 1, 2, 3], $pipeline->map(function ($value) { return $value; })->filter()->toArray()); } diff --git a/tests/LeaguePipelineTest.php b/tests/LeaguePipelineTest.php index a0584fb..4dd07e4 100644 --- a/tests/LeaguePipelineTest.php +++ b/tests/LeaguePipelineTest.php @@ -34,11 +34,11 @@ public function testWithLeaguePipeline() return $payload * 2; }); - $this->assertEquals(22, $leaguePipeline(10)); + $this->assertSame(22, $leaguePipeline(10)); $pipeline = new \Pipeline\Standard(new \ArrayIterator([10, 20, 30])); $pipeline->map($leaguePipeline); - $this->assertEquals([22, 42, 62], iterator_to_array($pipeline)); + $this->assertSame([22, 42, 62], iterator_to_array($pipeline)); } } diff --git a/tests/StandardTest.php b/tests/StandardTest.php index 2d426b1..e897a20 100644 --- a/tests/StandardTest.php +++ b/tests/StandardTest.php @@ -30,10 +30,10 @@ class StandardTest extends TestCase { public function testEmpty() { - $this->assertEquals([], iterator_to_array(new Standard())); + $this->assertSame([], iterator_to_array(new Standard())); $pipeline = new Standard(); - $this->assertEquals([], $pipeline->toArray()); + $this->assertSame([], $pipeline->toArray()); } public function testSingle() @@ -46,7 +46,7 @@ public function testSingle() } }); - $this->assertEquals([1, 2, 3], iterator_to_array($pipeline)); + $this->assertSame([1, 2, 3], iterator_to_array($pipeline)); } public function testDouble() @@ -65,7 +65,7 @@ public function testDouble() yield $i * 1000; }); - $this->assertEquals([10, 100, 1000, 20, 200, 2000, 30, 300, 3000], $pipeline->toArray()); + $this->assertSame([10, 100, 1000, 20, 200, 2000, 30, 300, 3000], $pipeline->toArray()); } public function testTriple() @@ -73,8 +73,8 @@ public function testTriple() $pipeline = new Standard(new \ArrayIterator(range(1, 3))); $pipeline->map(function ($i) { - yield pow($i, 2); - yield pow($i, 3); + yield $i ** 2; + yield $i ** 3; }); $pipeline->map(function ($i) { @@ -92,7 +92,7 @@ public function testTriple() } }); - $this->assertEquals([52, 104], $pipeline->toArray()); + $this->assertSame([52, 104], $pipeline->toArray()); } public function testFilter() @@ -106,7 +106,7 @@ public function testFilter() }); $pipeline->filter(function ($i) { - return $i % 7 == 0; + return 0 === $i % 7; }); $pipeline->map(function ($i) { @@ -115,7 +115,7 @@ public function testFilter() $pipeline->filter(); - $this->assertEquals([6, 13, 20, 27, 34, 41, 48], $pipeline->toArray()); + $this->assertSame([6, 13, 20, 27, 34, 41, 48], $pipeline->toArray()); } public function testReduce() @@ -130,7 +130,7 @@ public function testReduce() $result = $pipeline->reduce(); - $this->assertEquals(55, $result); + $this->assertSame(55, $result); } public function testReduceFloat() @@ -145,7 +145,7 @@ public function testReduceFloat() $result = $pipeline->reduce(); - $this->assertEquals(55 * 1.05, $result); + $this->assertSame(55 * 1.05, $result); } public function testReduceArrays() @@ -160,7 +160,7 @@ public function testReduceArrays() $result = $pipeline->reduce(null, []); - $this->assertEquals([1, 2], $result); + $this->assertSame([1, 2], $result); } public function testReduceToArray() @@ -174,12 +174,12 @@ public function testReduceToArray() }); $pipeline->map(function ($i) { - yield pow($i, 2); - yield pow($i, 3); + yield $i ** 2; + yield $i ** 3; }); $pipeline->filter(function ($i) { - return $i % 3 == 0; + return 0 === $i % 3; }); // just what iterator_to_array does @@ -189,7 +189,7 @@ public function testReduceToArray() return $sum; }, []); - $this->assertEquals([9, 27, 36, 216, 81, 729], $result); + $this->assertSame([9, 27, 36, 216, 81, 729], $result); } public function testMeaningless() @@ -202,7 +202,7 @@ public function testMeaningless() yield $i + 1; }); - $this->assertEquals(0, $pipeline->reduce()); + $this->assertSame(0, $pipeline->reduce()); } public function testPipelineInPipeline() @@ -215,10 +215,10 @@ public function testPipelineInPipeline() $pipeline2 = new Standard(); $pipeline2->map($pipeline1)->filter(function ($i) { - return $i % 2 != 0; + return 0 !== $i % 2; }); - $this->assertEquals(3 + 5 + 7 + 11, $pipeline2->reduce()); + $this->assertSame(3 + 5 + 7 + 11, $pipeline2->reduce()); } public function testFiltersPipeline() @@ -231,10 +231,10 @@ public function testFiltersPipeline() $output = new Standard($input); $output->filter(function ($i) { - return $i % 2 != 0; + return 0 !== $i % 2; }); - $this->assertEquals(3 + 5 + 7 + 11, $output->reduce()); + $this->assertSame(3 + 5 + 7 + 11, $output->reduce()); } public function testPipelineReadsFromPipeline() @@ -247,7 +247,7 @@ public function testPipelineReadsFromPipeline() $bar = new Standard(); $bar->map($foo); - $this->assertEquals(3, $bar->reduce()); + $this->assertSame(3, $bar->reduce()); } private $double; @@ -259,14 +259,14 @@ public function testTestableGenerator() return $value * 2; }; - $this->assertSame(8, call_user_func($this->double, 4)); + $this->assertSame(8, \call_user_func($this->double, 4)); $this->plusone = function ($value) { yield $value; yield $value + 1; }; - $this->assertSame([4, 5], iterator_to_array(call_user_func($this->plusone, 4))); + $this->assertSame([4, 5], iterator_to_array(\call_user_func($this->plusone, 4))); // initial generator $sourceData = new \ArrayIterator(range(1, 5)); @@ -290,8 +290,8 @@ public function testMethodChaining() yield $b; yield $c; })->map(function ($i) { - yield pow($i, 2); - yield pow($i, 3); + yield $i ** 2; + yield $i ** 3; })->map(function ($i) { return $i - 1; })->map(function ($i) { @@ -303,7 +303,7 @@ public function testMethodChaining() return $i; }); - $this->assertEquals([52, 104], iterator_to_array($pipeline)); + $this->assertSame([52, 104], iterator_to_array($pipeline)); } public function testMapNoop() @@ -314,7 +314,7 @@ public function testMapNoop() return range(1, 3); })->unpack()->map()->map()->map(); - $this->assertEquals(range(1, 3), iterator_to_array($pipeline)); + $this->assertSame(range(1, 3), iterator_to_array($pipeline)); } public function testFinal() diff --git a/tests/UnpackTest.php b/tests/UnpackTest.php index e981f84..61a9d6c 100644 --- a/tests/UnpackTest.php +++ b/tests/UnpackTest.php @@ -45,7 +45,7 @@ public function testMapVector() return sqrt($x ** 2 + $y ** 2); }); - $this->assertEquals(37, round($pipeline->reduce())); + $this->assertSame(37.0, round($pipeline->reduce())); } /** @@ -62,7 +62,7 @@ public function testFlatMap() yield [7, 8, 9, 10]; })->unpack(); - $this->assertEquals((10 * 11) / 2, round($pipeline->reduce())); + $this->assertSame((10 * 11) / 2, $pipeline->reduce()); } /**