Skip to content

Commit 0fc8ce2

Browse files
authored
Merge pull request #16 from simPod/coding-standard
Add Doctrine Coding standard
2 parents add4fe2 + d8ede8c commit 0fc8ce2

12 files changed

+177
-83
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: "Coding Standards"
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- "master"
8+
9+
jobs:
10+
coding-standards:
11+
name: "Coding Standards"
12+
runs-on: "ubuntu-20.04"
13+
14+
strategy:
15+
matrix:
16+
php-version:
17+
- "7.3"
18+
19+
steps:
20+
- name: "Checkout"
21+
uses: "actions/checkout@v2"
22+
23+
- name: "Install PHP"
24+
uses: "shivammathur/setup-php@v2"
25+
with:
26+
coverage: "none"
27+
php-version: "${{ matrix.php-version }}"
28+
tools: "cs2pr"
29+
30+
- name: "Install dependencies with Composer"
31+
uses: "ramsey/composer-install@v1"
32+
33+
- name: "Run PHP_CodeSniffer"
34+
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/vendor
2-
1+
/.phpcs-cache
32
/composer.lock
3+
/phpcs.xml
44
/phpunit.xml
5+
/vendor/

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"files": ["src/iterable-functions.php"]
1111
},
1212
"autoload-dev": {
13+
"psr-4": {
14+
"BenTools\\IterableFunctions\\Tests\\": "tests"
15+
},
1316
"files": [
1417
"vendor/symfony/var-dumper/Resources/functions/dump.php"
1518
]
@@ -18,8 +21,8 @@
1821
"php": "^7.3 || ^8.0"
1922
},
2023
"require-dev": {
24+
"doctrine/coding-standard": "^8.2",
2125
"phpunit/phpunit": "^9",
22-
"squizlabs/php_codesniffer": "^2.0|^3.4",
2326
"symfony/var-dumper": "^5.2"
2427
},
2528
"config": {

phpcs.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<ruleset
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
5+
>
6+
<arg name="basepath" value="." />
7+
<arg name="extensions" value="php" />
8+
<arg name="parallel" value="80" />
9+
<arg name="cache" value=".phpcs-cache" />
10+
<arg name="colors" />
11+
12+
<!-- Ignore warnings, show progress of the run and show sniff names -->
13+
<arg value="nps" />
14+
15+
<rule ref="Doctrine">
16+
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint" />
17+
18+
<exclude name="Generic.Formatting.MultipleStatementAlignment"/>
19+
<exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType"/>
20+
</rule>
21+
22+
<file>src/</file>
23+
<file>tests/</file>
24+
</ruleset>

src/IterableObject.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace BenTools\IterableFunctions;
46

57
use EmptyIterator;
@@ -11,21 +13,18 @@
1113
*/
1214
final class IterableObject implements IteratorAggregate
1315
{
14-
/**
15-
* @var iterable|array|Traversable
16-
*/
16+
/** @var iterable<mixed> */
1717
private $iterable;
1818

19-
/**
20-
* @var callable
21-
*/
19+
/** @var callable */
2220
private $filterFn;
2321

24-
/**
25-
* @var callable
26-
*/
22+
/** @var callable */
2723
private $mapFn;
2824

25+
/**
26+
* @param iterable<mixed>|null $iterable
27+
*/
2928
public function __construct(?iterable $iterable = null, ?callable $filter = null, ?callable $map = null)
3029
{
3130
$this->iterable = $iterable ?? new EmptyIterator();
@@ -43,27 +42,35 @@ public function map(callable $map): self
4342
return new self($this->iterable, $this->filterFn, $map);
4443
}
4544

45+
/**
46+
* @return Traversable<mixed>
47+
*/
4648
public function getIterator(): Traversable
4749
{
4850
$iterable = $this->iterable;
49-
if (null !== $this->filterFn) {
51+
if ($this->filterFn !== null) {
5052
$iterable = iterable_filter($iterable, $this->filterFn);
5153
}
52-
if (null !== $this->mapFn) {
54+
55+
if ($this->mapFn !== null) {
5356
$iterable = iterable_map($iterable, $this->mapFn);
5457
}
58+
5559
return iterable_to_traversable($iterable);
5660
}
5761

62+
/** @return array<mixed> */
5863
public function asArray(): array
5964
{
6065
$iterable = $this->iterable;
61-
if (null !== $this->filterFn) {
66+
if ($this->filterFn !== null) {
6267
$iterable = iterable_filter($iterable, $this->filterFn);
6368
}
64-
if (null !== $this->mapFn) {
69+
70+
if ($this->mapFn !== null) {
6571
$iterable = iterable_map($iterable, $this->mapFn);
6672
}
73+
6774
return iterable_to_array($iterable);
6875
}
6976
}

src/iterable-functions.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?php
22

3-
namespace BenTools\IterableFunctions;
3+
declare(strict_types=1);
44

5+
namespace BenTools\IterableFunctions;
56

67
use ArrayIterator;
78
use CallbackFilterIterator;
89
use IteratorIterator;
910
use Traversable;
1011

12+
use function array_filter;
13+
use function array_map;
14+
use function array_values;
15+
use function iterator_to_array;
16+
1117
/**
1218
* Maps a callable to an iterable.
1319
*
14-
* @param array|Traversable $iterable
15-
* @return array|ArrayIterator
20+
* @param iterable<mixed> $iterable
21+
*
22+
* @return iterable<mixed>
1623
*/
1724
function iterable_map(iterable $iterable, callable $map): iterable
1825
{
@@ -23,27 +30,29 @@ function iterable_map(iterable $iterable, callable $map): iterable
2330
return array_map($map, $iterable);
2431
}
2532

26-
2733
/**
2834
* Copy the iterable into an array. If the iterable is already an array, return it.
2935
*
30-
* @param array|Traversable $iterable
31-
* @param bool $use_keys [optional] Whether to use the iterator element keys as index.
32-
* @return array
36+
* @param iterable<mixed> $iterable
37+
* @param bool $preserveKeys [optional] Whether to use the iterator element keys as index.
38+
*
39+
* @return array<mixed>
3340
*/
34-
function iterable_to_array(iterable $iterable, bool $use_keys = true): array
41+
function iterable_to_array(iterable $iterable, bool $preserveKeys = true): array
3542
{
3643
if ($iterable instanceof Traversable) {
37-
return iterator_to_array($iterable, $use_keys);
44+
return iterator_to_array($iterable, $preserveKeys);
3845
}
3946

40-
return $use_keys ? $iterable : array_values($iterable);
47+
return $preserveKeys ? $iterable : array_values($iterable);
4148
}
4249

4350
/**
44-
* If the iterable is not intance of Traversable, it is an array => convert it to an ArrayIterator.
51+
* If the iterable is not instance of Traversable, it is an array => convert it to an ArrayIterator.
52+
*
53+
* @param iterable<mixed> $iterable
4554
*
46-
* @param array|Traversable $iterable
55+
* @return Traversable<mixed>
4756
*/
4857
function iterable_to_traversable(iterable $iterable): Traversable
4958
{
@@ -54,16 +63,16 @@ function iterable_to_traversable(iterable $iterable): Traversable
5463
return new ArrayIterator($iterable);
5564
}
5665

57-
5866
/**
5967
* Filters an iterable.
6068
*
61-
* @param array|Traversable $iterable
62-
* @return array|CallbackFilterIterator
69+
* @param iterable<mixed> $iterable
70+
*
71+
* @return array<mixed>|CallbackFilterIterator
6372
*/
6473
function iterable_filter(iterable $iterable, ?callable $filter = null)
6574
{
66-
if (null === $filter) {
75+
if ($filter === null) {
6776
$filter = static function ($value) {
6877
return (bool) $value;
6978
};
@@ -76,21 +85,19 @@ function iterable_filter(iterable $iterable, ?callable $filter = null)
7685
return array_filter($iterable, $filter);
7786
}
7887

79-
8088
/**
8189
* Reduces an iterable.
8290
*
8391
* @param iterable<mixed> $iterable
8492
* @param callable(mixed, mixed) $reduce
93+
*
8594
* @return mixed
8695
*
8796
* @psalm-template TValue
8897
* @psalm-template TResult
89-
*
9098
* @psalm-param iterable<TValue> $iterable
9199
* @psalm-param callable(TResult|null, TValue) $reduce
92100
* @psalm-param TResult|null $initial
93-
*
94101
* @psalm-return TResult|null
95102
*/
96103
function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
@@ -102,6 +109,9 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
102109
return $initial;
103110
}
104111

112+
/**
113+
* @param iterable<mixed> $iterable
114+
*/
105115
function iterable(iterable $iterable, ?callable $filter = null, ?callable $map = null): IterableObject
106116
{
107117
return new IterableObject($iterable, $filter, $map);

tests/IterableFilterTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
37
use PHPUnit\Framework\TestCase;
8+
use SplFixedArray;
9+
410
use function BenTools\IterableFunctions\iterable_filter;
511
use function BenTools\IterableFunctions\iterable_to_array;
612

@@ -10,7 +16,7 @@ public function testArrayFilter(): void
1016
{
1117
$iterable = ['foo', 'bar'];
1218
$filter = static function ($input) {
13-
return 'bar' === $input;
19+
return $input === 'bar';
1420
};
1521
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
1622
}
@@ -19,7 +25,7 @@ public function testTraversableFilter(): void
1925
{
2026
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
2127
$filter = static function ($input) {
22-
return 'bar' === $input;
28+
return $input === 'bar';
2329
};
2430
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
2531
}

tests/IterableMapTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
37
use PHPUnit\Framework\TestCase;
8+
use SplFixedArray;
9+
410
use function BenTools\IterableFunctions\iterable_map;
511
use function BenTools\IterableFunctions\iterable_to_array;
612

0 commit comments

Comments
 (0)