Add php-cs-fixer and fix code style

This commit is contained in:
Niklas Keller 2017-12-13 23:48:03 +01:00
parent 74c8b5f145
commit 36dc8ed92a
12 changed files with 72 additions and 21 deletions

8
.editorconfig Normal file
View File

@ -0,0 +1,8 @@
root = true
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
charset = utf-8

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/vendor/
/composer.lock
/.php_cs.cache

41
.php_cs.dist Normal file
View File

@ -0,0 +1,41 @@
<?php
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
"@PSR1" => true,
"@PSR2" => true,
"braces" => [
"allow_single_line_closure" => true,
"position_after_functions_and_oop_constructs" => "same",
],
"array_syntax" => ["syntax" => "short"],
"cast_spaces" => true,
"combine_consecutive_unsets" => true,
"function_to_constant" => true,
"native_function_invocation" => true,
"no_multiline_whitespace_before_semicolons" => true,
"no_unused_imports" => true,
"no_useless_else" => true,
"no_useless_return" => true,
"no_whitespace_before_comma_in_array" => true,
"no_whitespace_in_blank_line" => true,
"non_printable_character" => true,
"normalize_index_brace" => true,
"ordered_imports" => true,
"php_unit_construct" => true,
"php_unit_dedicate_assert" => true,
"php_unit_fqcn_annotation" => true,
"phpdoc_summary" => true,
"phpdoc_types" => true,
"psr4" => true,
"return_type_declaration" => ["space_before" => "none"],
"short_scalar_cast" => true,
"single_blank_line_before_namespace" => true,
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . "/examples")
->in(__DIR__ . "/src")
->in(__DIR__ . "/test")
);

View File

@ -24,7 +24,8 @@
"jeremeamia/SuperClosure": "^2.3"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"amphp/phpunit-util": "^1.0"
"amphp/phpunit-util": "^1.0",
"friendsofphp/php-cs-fixer": "^2.9",
"phpunit/phpunit": "^6.5"
}
}

View File

@ -5,7 +5,7 @@ require __DIR__ . '/../vendor/autoload.php';
use function Amp\ParallelFunctions\parallelMap;
use function Amp\Promise\wait;
$start = microtime(true);
$start = \microtime(true);
// sleep() is executed in child processes, the results are sent back to the parent.
//
@ -13,4 +13,4 @@ $start = microtime(true);
// in a traditional synchronous environment.
wait(parallelMap([1, 2, 3], 'sleep'));
print 'Took ' . (microtime(true) - $start) . ' milliseconds.' . \PHP_EOL;
print 'Took ' . (\microtime(true) - $start) . ' milliseconds.' . \PHP_EOL;

View File

@ -6,4 +6,4 @@ use function Amp\ParallelFunctions\parallelMap;
use function Amp\Promise\wait;
// We have seen that the order can vary in the previous example, values returned have a deterministic order.
var_dump(wait(parallelMap([1, 2, 3], 'abs')));
\var_dump(wait(parallelMap([1, 2, 3], 'abs')));

View File

@ -7,8 +7,8 @@ use function Amp\Promise\wait;
// Parallel function execution is nice, but it's even better being able to use closures instead of having to write a
// function that has to be autoloadable.
var_dump(wait(parallelMap([1, 2, 3], function ($time) {
sleep($time); // a blocking function call, might also do blocking I/O here
\var_dump(wait(parallelMap([1, 2, 3], function ($time) {
\sleep($time); // a blocking function call, might also do blocking I/O here
return $time * $time;
})));

View File

@ -53,7 +53,7 @@ function parallelMap(array $array, callable $callable): Promise {
return call(function () use ($array, $callable) {
// Amp\Promise\any() guarantees that all operations finished prior to resolving. Amp\Promise\all() doesn't.
// Additionally, we return all errors as a MultiReasonException instead of throwing on the first error.
list($errors, $results) = yield any(array_map(parallel($callable), $array));
list($errors, $results) = yield any(\array_map(parallel($callable), $array));
if ($errors) {
throw new MultiReasonException($errors);
@ -87,11 +87,11 @@ function parallelFilter(array $array, callable $callable = null, int $flag = 0):
// Amp\Promise\any() guarantees that all operations finished prior to resolving. Amp\Promise\all() doesn't.
// Additionally, we return all errors as a MultiReasonException instead of throwing on the first error.
if ($flag === \ARRAY_FILTER_USE_BOTH) {
list($errors, $results) = yield any(array_map(parallel($callable), $array, \array_keys($array)));
list($errors, $results) = yield any(\array_map(parallel($callable), $array, \array_keys($array)));
} elseif ($flag === \ARRAY_FILTER_USE_KEY) {
list($errors, $results) = yield any(array_map(parallel($callable), \array_keys($array)));
list($errors, $results) = yield any(\array_map(parallel($callable), \array_keys($array)));
} else {
list($errors, $results) = yield any(array_map(parallel($callable), $array));
list($errors, $results) = yield any(\array_map(parallel($callable), $array));
}
if ($errors) {

View File

@ -11,7 +11,7 @@ class ParallelFilterTest extends TestCase {
public function testWithoutCallback() {
$input = [1, 0, 3, false, true, null];
$this->assertSame(array_filter($input), wait(parallelFilter($input)));
$this->assertSame(\array_filter($input), wait(parallelFilter($input)));
}
public function testWithCallback() {
@ -20,7 +20,7 @@ class ParallelFilterTest extends TestCase {
return $value === false;
};
$this->assertSame(array_filter($input, $callback), wait(parallelFilter($input, $callback)));
$this->assertSame(\array_filter($input, $callback), wait(parallelFilter($input, $callback)));
}
public function testWithCallbackAndFlagKey() {
@ -29,7 +29,7 @@ class ParallelFilterTest extends TestCase {
return $key === 2;
};
$this->assertSame(array_filter($input, $callback, \ARRAY_FILTER_USE_KEY), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_KEY)));
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_KEY), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_KEY)));
}
public function testWithCallbackAndFlagBoth() {
@ -38,7 +38,7 @@ class ParallelFilterTest extends TestCase {
return $key === 2 || $value === true;
};
$this->assertSame(array_filter($input, $callback, \ARRAY_FILTER_USE_BOTH), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_BOTH)));
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_BOTH), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_BOTH)));
}
public function testException() {