Skip to content

Commit

Permalink
Merge pull request #66 from sanmai/pr/legacy/strict-comparison
Browse files Browse the repository at this point in the history
Strict comparison updates
  • Loading branch information
sanmai authored Oct 20, 2018
2 parents 79f6c8a + a3eac56 commit 5fd37be
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 52 deletions.
29 changes: 28 additions & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
/*
* Copyright 2017, 2018 Alexey Kopytko <alexey@kopytko.com>
*
* 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 <alexey@kopytko.com>
Expand All @@ -20,6 +37,8 @@ return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP70Migration:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => true,
'explicit_indirect_variable' => true,
Expand All @@ -31,13 +50,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()
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ COMPOSER=$(PHP) $(shell which composer)

# Infection
INFECTION=vendor/bin/infection
MIN_MSI=90
MIN_MSI=100
MIN_COVERED_MSI=100
INFECTION_ARGS=--min-msi=$(MIN_MSI) --min-covered-msi=$(MIN_COVERED_MSI) --threads=$(JOBS) --coverage=build/logs --log-verbosity=default --show-mutations

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
"psr-4": {"Pipeline\\": "src/"}
},
"autoload-dev": {
"psr-4": {"Pipeline\\": "tests/"}
"psr-4": {
"Tests\\Pipeline\\": "tests/",
"Pipeline\\": "tests/"
}
},
"require": {
"php": ">=7"
Expand Down
6 changes: 3 additions & 3 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
});

// next processing step
$pipeline->map(function ($i) {
yield pow($i, 2);
yield pow($i, 3);
$pipeline->map(function ($value) {
yield $value ** 2;
yield $value ** 3;
});

// simple one-to-one mapper
Expand Down
5 changes: 5 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"src"
]
},
"mutators": {
"@default": true,
"IdenticalEqual": false,
"NotIdenticalNotEqual": false
},
"logs": {
"text": "infection-log.txt",
"badge": {"branch": "master"}
Expand Down
4 changes: 2 additions & 2 deletions src/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function map(callable $func)
// This also allows inheriting classes to replace the pipeline
if ($func instanceof self) {
/** @psalm-suppress MixedAssignment */
$this->pipeline = call_user_func($func);
$this->pipeline = \call_user_func($func);

return $this;
}

if (!$this->pipeline) {
/** @psalm-suppress MixedAssignment */
$this->pipeline = call_user_func($func);
$this->pipeline = \call_user_func($func);

// Not a generator means we were given a simple value to be treated as an array
if (!($this->pipeline instanceof \Generator)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Legacy alias from early days. Please do not remove.
*
* @deprecated
* @deprecated use Standard
*/
class Simple extends Standard
{
Expand Down
2 changes: 1 addition & 1 deletion src/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function unpack(callable $func = null)
public function filter(callable $func = null)
{
// 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);
};
Expand Down
70 changes: 70 additions & 0 deletions tests/ArraysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/*
* Copyright 2017, 2018 Alexey Kopytko <alexey@kopytko.com>
*
* 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);

namespace Tests\Pipeline;

use PHPUnit\Framework\TestCase;
use Pipeline\Standard;

/**
* @covers \Pipeline\Standard
* @covers \Pipeline\Principal
*/
class ArraysTest extends TestCase
{
public function testInitialCallbackNotGenerator()
{
$pipeline = new Standard();
$pipeline->map(function () {
return PHP_INT_MAX;
});

$this->assertSame([PHP_INT_MAX], iterator_to_array($pipeline));
}

public function testArrayToArray()
{
$pipeline = new Standard();
$pipeline->map(function () {
return 42;
});

$this->assertSame([42], $pipeline->toArray());
}

public function testArrayFilter()
{
$pipeline = new Standard();
$pipeline->map(function () {
return false;
})->filter()->filter();

$this->assertSame([], $pipeline->toArray());
}

public function testArrayReduce()
{
$pipeline = new Standard();
$pipeline->map(function () {
return 3;
});

$this->assertSame(3, $pipeline->reduce());
}
}
20 changes: 10 additions & 10 deletions tests/EdgeCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ 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 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()
Expand Down Expand Up @@ -70,15 +70,15 @@ public function testMapUnprimed()
return 1;
});

$this->assertEquals([1], $pipeline->toArray());
$this->assertSame([1], $pipeline->toArray());
}

public function testFilterUnprimed()
{
$pipeline = new Standard();
$pipeline->filter()->unpack();

$this->assertEquals([], $pipeline->toArray());
$this->assertSame([], $pipeline->toArray());
}

public function testUnpackUnprimed()
Expand All @@ -88,15 +88,15 @@ public function testUnpackUnprimed()
return 1;
});

$this->assertEquals([1], $pipeline->toArray());
$this->assertSame([1], $pipeline->toArray());
}

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)
Expand All @@ -115,11 +115,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));
}

public function testIteratorToArrayWithSameKeys()
Expand All @@ -135,7 +135,7 @@ public function testIteratorToArrayWithSameKeys()
yield $i + 2;
});

$this->assertEquals([3, 4], iterator_to_array($pipeline));
$this->assertSame([3, 4], iterator_to_array($pipeline));
}

public function testPointlessReplace()
Expand Down Expand Up @@ -171,7 +171,7 @@ public function testInvokeMaps()
$pipeline = new \Pipeline\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)
Expand Down
2 changes: 1 addition & 1 deletion tests/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testPipelineInPipelineUsesSelf()
});

$pipeline->map($pipeline)->filter(function ($i) {
return $i % 2 != 0;
return 0 !== $i % 2;
});

$this->expectExceptionMessage('Cannot resume an already running generator');
Expand Down
98 changes: 98 additions & 0 deletions tests/IterableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/*
* Copyright 2017, 2018 Alexey Kopytko <alexey@kopytko.com>
*
* 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);

namespace Tests\Pipeline;

use PHPUnit\Framework\TestCase;
use Pipeline\Standard;

/**
* @covers \Pipeline\Standard
* @covers \Pipeline\Principal
*/
class IterableTest extends TestCase
{
private static $usesIterable;

public static function setUpBeforeClass()
{
if (\PHP_VERSION_ID < 70100) {
self::$usesIterable = false;

return;
}

$reflection = new \ReflectionClass(Standard::class);
$type = $reflection->getConstructor()->getParameters()[0]->getType();
self::$usesIterable = $type->isBuiltin(); // Traversable isn't builtin
}

protected function setUp()
{
if (!self::$usesIterable) {
$this->markTestSkipped('Not testing iterables: not yet supported by the interface');
}
}

public function testArrayToArray()
{
$pipeline = new Standard([1, 2, 3]);
$this->assertSame([1, 2, 3], $pipeline->toArray());
}

public function testArrayToIterator()
{
$pipeline = new Standard([1, 2, 3]);
$this->assertSame([1, 2, 3], iterator_to_array($pipeline));
}

public function testEmptyArrayStaysEmpty()
{
$pipeline = new Standard([]);

$pipeline->filter()->map(function ($value) {
yield $value;
yield $value;
})->filter()->unpack();

$this->assertSame([], $pipeline->toArray());
}

public function testArrayFilter()
{
$pipeline = new Standard([0, 1, 2, 3, 0]);
$this->assertSame([1, 2, 3], $pipeline->filter()->toArray());
}

public function testArrayMap()
{
$pipeline = new Standard([1 => 0, 1, 2, 3]);
$this->assertSame([0 => 0, 1, 2, 3], $pipeline->map(function ($value) {
return $value;
})->toArray());
}

public function testArrayMapFilter()
{
$pipeline = new Standard([1 => 0, 1, 2, 3]);
$this->assertSame([0 => 1, 2, 3], $pipeline->map(function ($value) {
return $value;
})->filter()->toArray());
}
}
Loading

0 comments on commit 5fd37be

Please sign in to comment.