Skip to content

Commit

Permalink
Add rector (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilario-pierbattista authored Apr 24, 2023
1 parent f56f63e commit 7196242
Show file tree
Hide file tree
Showing 36 changed files with 108 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
php-version: ${{ matrix.php }}
coverage: pcov
- name: Install dependencies
uses: "ramsey/composer-install@v1"
uses: ramsey/composer-install@v2
- name: Run tests
run: ./vendor/bin/phpunit --coverage-clover=build/coverage-report.xml
- name: Upload code coverage
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ cs-check:
ci: test phpstan psalm type-assertions cs-fix

ci-check: test phpstan psalm type-assertions cs-check

.PHONY: rector
rector:
./vendor/bin/rector
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"facile-it/facile-coding-standard": "^0.5.1",
"vimeo/psalm": "4.30.0",
"friendsofphp/php-cs-fixer": "^3.3",
"phpstan/phpstan": "^1.8"
"phpstan/phpstan": "^1.8",
"rector/rector": "^0.15.25"
},
"license": "MIT",
"authors": [
Expand Down
20 changes: 20 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
]);
};
8 changes: 2 additions & 6 deletions src/Decoders.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ public static function union(Decoder $a, Decoder $b, ?Decoder $c = null, ?Decode
$args = array_values(
array_filter(
func_get_args(),
static function ($x): bool {
return $x instanceof Decoder;
}
static fn ($x): bool => $x instanceof Decoder
)
);
$argc = count($args);
Expand Down Expand Up @@ -255,9 +253,7 @@ public static function classFromArrayPropsDecoder(
): Decoder {
/** @psalm-var Decoder<Properties, T> $mapDecoder */
$mapDecoder = new MapDecoder(
function (array $props) use ($factory) {
return Internal\FunctionUtils::destructureIn($factory)(\array_values($props));
},
fn (array $props) => Internal\FunctionUtils::destructureIn($factory)(\array_values($props)),
\sprintf('%s(%s)', $decoderName, $propsDecoder->getName())
);

Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Arrays/ListOfDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
final class ListOfDecoder implements Decoder
{
/** @var Decoder<IT, T> */
private $elementDecoder;
private \Facile\PhpCodec\Decoder $elementDecoder;

/**
* @psalm-param Decoder<IT, T> $elementDecoder
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Combinators/ArrayPropsDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class ArrayPropsDecoder implements Decoder
{
/** @var PD */
private $props;
private array $props;

/**
* @psalm-param PD $props
Expand Down
8 changes: 3 additions & 5 deletions src/Internal/Combinators/ComposeDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
final class ComposeDecoder implements Decoder
{
/** @var Decoder<A, B> */
private $db;
private \Facile\PhpCodec\Decoder $db;
/** @var Decoder<IA, A> */
private $da;
private \Facile\PhpCodec\Decoder $da;

/**
* @psalm-param Decoder<A, B> $db
Expand Down Expand Up @@ -51,9 +51,7 @@ public function validate($i, Context $context): Validation
*
* @param mixed $aValue
*/
function ($aValue) use ($context): Validation {
return $this->db->validate($aValue, $context);
},
fn ($aValue): Validation => $this->db->validate($aValue, $context),
$this->da->validate($i, $context)
);
}
Expand Down
9 changes: 3 additions & 6 deletions src/Internal/Combinators/IntersectionDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
final class IntersectionDecoder implements Decoder
{
/** @var Decoder<IA, A> */
private $a;
private \Facile\PhpCodec\Decoder $a;
/** @var Decoder<IB, B> */
private $b;
private \Facile\PhpCodec\Decoder $b;

/**
* @psalm-param Decoder<IA, A> $a
Expand All @@ -46,10 +46,7 @@ public function validate($i, Context $context): Validation

if ($va instanceof ValidationFailures && $vb instanceof ValidationFailures) {
return ValidationFailures::failures(
array_merge(
$va->getErrors(),
$vb->getErrors()
)
[...$va->getErrors(), ...$vb->getErrors()]
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Internal/Combinators/MapDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ final class MapDecoder implements Decoder
{
/** @var callable(A):B */
private $f;
/** @var string */
private $name;
private string $name;

/**
* @psalm-param callable(A):B $f
Expand Down
12 changes: 4 additions & 8 deletions src/Internal/Combinators/UnionDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
final class UnionDecoder implements Decoder
{
/** @var Decoder<IA, A> */
private $a;
private \Facile\PhpCodec\Decoder $a;
/** @var Decoder<IB, B> */
private $b;
/** @var int */
private $indexBegin;
private \Facile\PhpCodec\Decoder $b;
private int $indexBegin;

/**
* @psalm-param Decoder<IA, A> $a
Expand Down Expand Up @@ -71,10 +70,7 @@ public function validate($i, Context $context): Validation

if ($vb instanceof ValidationFailures) {
return Validation::failures(
array_merge(
$va->getErrors(),
$vb->getErrors()
)
[...$va->getErrors(), ...$vb->getErrors()]
);
}

Expand Down
18 changes: 7 additions & 11 deletions src/Internal/FunctionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ public static function nameFromProps(array $props): string
\implode(
', ',
\array_map(
static function (Decoder $t, $k): string {
return \sprintf(
'%s: %s',
\is_string($k) ? $k : \sprintf('[%d]', $k),
$t->getName()
);
},
static fn (Decoder $t, $k): string => \sprintf(
'%s: %s',
\is_string($k) ? $k : \sprintf('[%d]', $k),
$t->getName()
),
$props,
\array_keys($props)
)
Expand Down Expand Up @@ -64,9 +62,7 @@ public static function standardDecode(Decoder $decoder, $input): Validation
*/
public static function destructureIn(callable $f): callable
{
return function (array $params) use ($f) {
return $f(...$params);
};
return fn (array $params) => $f(...$params);
}

/**
Expand All @@ -86,7 +82,7 @@ public static function strigify($x): string

if (\is_array($x)) {
return \function_exists('json_encode')
? \json_encode($x)
? \json_encode($x, JSON_THROW_ON_ERROR)
: \serialize($x);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Internal/Useful/DateTimeFromStringDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
final class DateTimeFromStringDecoder implements Decoder
{
/**
* @var string
* @psalm-readonly
*/
private $format;
private string $format;

public function __construct(string $format = \DATE_ATOM)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Internal/Useful/RegexDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
final class RegexDecoder implements Decoder
{
/** @var string */
private $regex;
private string $regex;

public function __construct(string $regex)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Internal/Useful/StringMatchingRegexDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
final class StringMatchingRegexDecoder implements Decoder
{
/** @var string */
private $regex;
private string $regex;

public function __construct(string $regex)
{
Expand Down
14 changes: 5 additions & 9 deletions src/Reporters/PathReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ public static function create(): self
public function report(Validation $validation): array
{
return Validation::fold(
function (array $errors): array {
return \array_map(
[self::class, 'getMessage'],
$errors
);
},
function (): array {
return ['No errors!'];
},
fn (array $errors): array => \array_map(
[self::class, 'getMessage'],
$errors
),
fn (): array => ['No errors!'],
$validation
);
}
Expand Down
14 changes: 5 additions & 9 deletions src/Reporters/SimplePathReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ public static function create(): self
public function report(Validation $validation): array
{
return Validation::fold(
static function (array $errors): array {
return array_map(
[self::class, 'getMessage'],
$errors
);
},
static function (): array {
return ['No errors'];
},
static fn (array $errors): array => array_map(
[self::class, 'getMessage'],
$errors
),
static fn (): array => ['No errors'],
$validation
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Utils/ConcreteDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ final class ConcreteDecoder implements Decoder
{
/** @var callable(I, Context):Validation<A> */
private $validateFunc;
/** @var string */
private $name;
private string $name;

/**
* @psalm-param callable(I, Context):Validation<A> $validate
Expand Down
9 changes: 3 additions & 6 deletions src/Validation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
final class Context implements \Iterator
{
/** @var ContextEntry[] */
private $entries;
/** @var int */
private $currentIndex;
/** @var Decoder */
private $decoder;
private array $entries;
private int $currentIndex = 0;
private \Facile\PhpCodec\Decoder $decoder;

/**
* @psalm-param Decoder $decoder
Expand All @@ -24,7 +22,6 @@ public function __construct(
ContextEntry ...$entries
) {
$this->entries = $entries;
$this->currentIndex = 0;
$this->decoder = $decoder;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Validation/ContextEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

final class ContextEntry
{
/** @var string */
private $key;
/** @var Decoder */
private $decoder;
private string $key;
private \Facile\PhpCodec\Decoder $decoder;
/** @var mixed */
private $actual;

Expand Down
6 changes: 2 additions & 4 deletions src/Validation/VError.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ final class VError
{
/** @var mixed */
private $value;
/** @var Context */
private $context;
/** @var string|null */
private $message;
private \Facile\PhpCodec\Validation\Context $context;
private ?string $message = null;

/**
* @psalm-param mixed $value
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/ValidationFailures.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
final class ValidationFailures extends Validation
{
/** @var list<VError> */
private $errors;
private array $errors;

/**
* @psalm-param list<VError> $errors
Expand Down
6 changes: 2 additions & 4 deletions tests/examples/DecodeApiResponse/Coordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
*/
class Coordinates
{
/** @var float */
private $longitude;
/** @var float */
private $latitude;
private float $longitude;
private float $latitude;

public function __construct(float $longitude, float $latitude)
{
Expand Down
Loading

0 comments on commit 7196242

Please sign in to comment.