Skip to content

Commit

Permalink
Add PHPStan, update PHPAt (#46)
Browse files Browse the repository at this point in the history
PHPAt tests are now ran by PHPStan. Added PHP 8.0 and 8.1 in CI test matrix.
  • Loading branch information
ilario-pierbattista authored Oct 8, 2022
1 parent be1f2cc commit 9cd82db
Show file tree
Hide file tree
Showing 29 changed files with 282 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [7.2, 7.3, 7.4]
php: [7.4, 8.0, 8.1]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
script: composer validate
- description: Code style
script: make cs-check
- description: PHPStan
script: make phpstan
- description: PSalm
script: make psalm
- description: Type assertions
script: make type-assertions
- description: Architecture assertions
script: make architecture

name: ${{ matrix.description }}
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Removed
- Support for PHP < 7.4
## [0.0.3] - 2022-01-16
### Added
- `SimplePathReporter` error reporter.
Expand Down
27 changes: 21 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: setup sh psalm usage test type-assertions architecture
.PHONY: setup sh usage

usage:
@echo "select target"
Expand All @@ -10,6 +10,9 @@ sh:
docker-compose up -d
docker-compose exec php bash


.PHONY: psalm psalm-src psalm-tests psalm-update-baseline

psalm-src:
./vendor/bin/psalm src --no-cache

Expand All @@ -18,22 +21,34 @@ psalm-tests:

psalm: psalm-src psalm-tests

psalm-update-baseline:
./vendor/bin/psalm --update-baseline src tests


.PHONY: phpstan phpstan-update-baseline

phpstan:
./vendor/bin/phpstan analyse src tests

phpstan-update-baseline:
./vendor/bin/phpstan analyse src tests --generate-baseline


.PHONY: type-assertions test

type-assertions:
./vendor/bin/psalm tests/type-assertions --no-cache

test:
./vendor/bin/phpunit

architecture:
./vendor/bin/phpat

.PHONY: ci cs-check cs-fix
cs-fix:
./vendor/bin/php-cs-fixer fix --ansi --verbose

cs-check:
./vendor/bin/php-cs-fixer fix --ansi --verbose --dry-run

ci: test cs-fix psalm type-assertions architecture
ci: test cs-fix psalm type-assertions

ci-check: test cs-check psalm type-assertions architecture
ci-check: test cs-check psalm type-assertions
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# PHP-codec
PHP-codec is a partial porting of [io-ts](https://github.com/gcanti/io-ts) in PHP.

[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.4-8892BF.svg)](https://php.net/)

[![CI](https://github.com/facile-it/php-codec/actions/workflows/ci.yaml/badge.svg?branch=master&event=push)](https://github.com/facile-it/php-codec/actions/workflows/ci.yaml)
[![Static analysis](https://github.com/facile-it/php-codec/actions/workflows/static-analysis.yaml/badge.svg?branch=master&event=push)](https://github.com/facile-it/php-codec/actions/workflows/static-analysis.yaml)
[![codecov](https://codecov.io/gh/facile-it/php-codec/branch/master/graph/badge.svg?token=HP4OFEEPY6)](https://codecov.io/gh/facile-it/php-codec) [![Join the chat at https://gitter.im/php-codec/community](https://badges.gitter.im/php-codec/community.svg)](https://gitter.im/php-codec/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Install it now. It only requires PHP > 7.2.
Install it now. It only requires PHP >= 7.4.

composer require facile-it/php-codec

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"phpunit/phpunit": "^8.5",
"giorgiosironi/eris": "^0.13.0",
"phpunit/php-code-coverage": "^7.0",
"phpat/phpat": "=0.7.5",
"phpat/phpat": "0.10.*",
"facile-it/facile-coding-standard": "^0.5.1",
"vimeo/psalm": "4.8.1",
"friendsofphp/php-cs-fixer": "^3.3"
"friendsofphp/php-cs-fixer": "^3.3",
"phpstan/phpstan": "^1.8"
},
"license": "MIT",
"authors": [
Expand All @@ -36,7 +37,7 @@
}
},
"require": {
"php": "^7.2 | ^8.0"
"php": "^7.4 | ^8.0"
},
"prefer-stable": true,
"archive": {
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7.2
FROM php:7.4

COPY --from=composer /usr/bin/composer /usr/bin/composer

Expand Down
5 changes: 5 additions & 0 deletions phpat.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
-
class: ArchitectureAssertions\Facile\PhpCodec\ArchitectureTest
tags:
- phpat.test
4 changes: 0 additions & 4 deletions phpat.yml

This file was deleted.

46 changes: 46 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
parameters:
ignoreErrors:
-
message: "#^Template type U of method Facile\\\\PhpCodec\\\\Codecs\\:\\:mixed\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Codecs.php

-
message: "#^Template type RA of method Facile\\\\PhpCodec\\\\Decoder\\:\\:validate\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Decoder.php

-
message: "#^Template type CF of method Facile\\\\PhpCodec\\\\Decoders\\:\\:classFromArrayPropsDecoder\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Decoders.php

-
message: "#^Template type T of method Facile\\\\PhpCodec\\\\Decoders\\:\\:classFromArrayPropsDecoder\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Decoders.php

-
message: "#^Template type X of method Facile\\\\PhpCodec\\\\Internal\\\\Encode\\:\\:identity\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Internal/Encode.php

-
message: "#^Template type VP of method Facile\\\\PhpCodec\\\\Validation\\\\ListOfValidation\\:\\:reduceToIndexedSuccessOrAllFailures\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Validation/ListOfValidation.php

-
message: "#^Template type T of method Facile\\\\PhpCodec\\\\Validation\\\\Validation\\:\\:failure\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Validation/Validation.php

-
message: "#^Template type T of method Facile\\\\PhpCodec\\\\Validation\\\\Validation\\:\\:failures\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: src/Validation/Validation.php

-
message: "#^Template type R of method Tests\\\\Facile\\\\PhpCodec\\\\BaseTestCase\\:\\:asserSuccessSameTo\\(\\) is not referenced in a parameter\\.$#"
count: 1
path: tests/unit/BaseTestCase.php
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- vendor/phpat/phpat/extension.neon
- phpat.neon
- phpstan-baseline.neon
parameters:
level: 0
paths:
- src
- tests
25 changes: 25 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.8.1@f73f2299dbc59a3e6c4d66cff4605176e728ee69">
<file src="tests/unit/CodecsTest.php">
<MixedArgument occurrences="1"/>
<TooManyArguments occurrences="4"/>
</file>
<file src="tests/unit/GeneratorUtils.php">
<TooManyArguments occurrences="1"/>
</file>
<file src="tests/unit/Internal/Combinators/IntersectionDecoderTest.php">
<TooManyArguments occurrences="4">
<code>Generators::oneOf(Generators::int(), Generators::float(), Generators::bool())</code>
<code>Generators::oneOf(Generators::string(), Generators::float(), Generators::bool())</code>
</TooManyArguments>
</file>
<file src="tests/unit/Internal/Combinators/LiteralCodecTest.php">
<TooManyArguments occurrences="2"/>
</file>
<file src="tests/unit/Internal/Primitives/CallableDecoderTest.php">
<TooManyArguments occurrences="2"/>
</file>
<file src="tests/unit/Internal/Primitives/NullTypeTest.php">
<TooManyArguments occurrences="1"/>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
Expand Down
4 changes: 1 addition & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Facile\PhpCodec;

use Examples\Facile\PhpCodec\internal\A;

/**
* @const callable
*/
Expand Down Expand Up @@ -53,7 +51,7 @@ function strigify($x): string

/**
* @psalm-template R
* @psalm-param callable(...mixed):R $f
* @psalm-param callable(mixed...):R $f
* @psalm-return callable(list<mixed>):R
*/
function destructureIn(callable $f): callable
Expand Down
65 changes: 38 additions & 27 deletions tests/architecture/ArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,52 @@
use Facile\PhpCodec\Decoder;
use Facile\PhpCodec\Decoders;
use Facile\PhpCodec\Encoder;
use PhpAT\Rule\Rule;
use PhpAT\Selector\Selector;
use Facile\PhpCodec\Reporters;
use PHPat\Selector\Selector;
use PHPat\Test\Builder\Rule;
use PHPat\Test\PHPat;

class ArchitectureTest extends \PhpAT\Test\ArchitectureTest
class ArchitectureTest
{
public function testCodecsShouldntBeUsed(): Rule
public function testAnyInternalClassShouldNotDependFromAnythingOutsideExceptDefinitionInterfaces(): Rule
{
return $this->newRule
->classesThat(Selector::havePath('*'))
->excludingClassesThat(Selector::haveClassName(Codecs::class))
->mustNotDependOn()
->classesThat(Selector::haveClassName(Codecs::class))
->build();
return PHPat::rule()
->classes(Selector::namespace('Facile\PhpCodec\Internal'))
->shouldNotDependOn()
->classes(Selector::namespace('Facile\PhpCodec'))
->excluding(
Selector::namespace('Facile\PhpCodec\Internal'),
Selector::namespace('Facile\PhpCodec\Validation'),
Selector::classname(Decoder::class),
Selector::classname(Encoder::class),
Selector::classname(Codec::class)
);
}

public function testAnyInternalClassShouldNotDependFromAnythingOutsideExceptDefinitionInterfaces(): Rule
public function testCodecsBeAnEndpointClass(): Rule
{
return PHPat::rule()
->classes(Selector::namespace('Facile\PhpCodec'))
->excluding(Selector::classname(Codecs::class))
->shouldNotDependOn()
->classes(Selector::classname(Codecs::class));
}

public function testDecodersBeAnEndpointClass(): Rule
{
return $this->newRule
->classesThat(Selector::havePath('Internal/*'))
->mustNotDependOn()
->classesThat(Selector::havePath('*'))
->excludingClassesThat(Selector::havePath('Internal/*'))
->andExcludingClassesThat(Selector::havePath('Validation/*'))
->andExcludingClassesThat(Selector::haveClassName(Decoder::class))
->andExcludingClassesThat(Selector::haveClassName(Encoder::class))
->andExcludingClassesThat(Selector::haveClassName(Codec::class))
->build();
return PHPat::rule()
->classes(Selector::namespace('Facile\PhpCodec'))
->excluding(Selector::classname(Decoders::class))
->shouldNotDependOn()
->classes(Selector::classname(Decoders::class));
}

public function testDecodersMustExposeEveryInteralDecoder(): Rule
public function testReportersBeAnEndpointClass(): Rule
{
return $this->newRule
->classesThat(Selector::haveClassName(Decoders::class))
->mustDependOn()
->classesThat(Selector::implementInterface(Decoder::class))
->build();
return PHPat::rule()
->classes(Selector::namespace('Facile\PhpCodec'))
->excluding(Selector::classname(Reporters::class))
->shouldNotDependOn()
->classes(Selector::classname(Reporters::class));
}
}
22 changes: 11 additions & 11 deletions tests/examples/CodecForSumtype/CodecForSumtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Examples\Facile\PhpCodec\CodecForSumtype;

use Eris\Generator as g;
use Eris\Generators;
use Eris\TestTrait;
use Facile\PhpCodec\Decoders;
use Tests\Facile\PhpCodec\BaseTestCase;
Expand Down Expand Up @@ -53,11 +53,11 @@ function (string $t, int $case, float $amount, bool $flag): B {
/** @psalm-suppress UndefinedFunction */
$this
->forAll(
g\associative([
'type' => g\constant(P::Type_a),
'subType' => g\elements(A::SUB_foo, A::SUB_bar),
'propA' => g\int(),
'propB' => g\string(),
Generators::associative([
'type' => Generators::constant(P::Type_a),
'subType' => Generators::elements(A::SUB_foo, A::SUB_bar),
'propA' => Generators::int(),
'propB' => Generators::string(),
])
)
->then(function (array $i) use ($codec): void {
Expand All @@ -73,11 +73,11 @@ function (string $t, int $case, float $amount, bool $flag): B {
/** @psalm-suppress UndefinedFunction */
$this
->forAll(
g\associative([
'type' => g\constant(P::Type_b),
'case' => g\elements(B::CASE_B1, B::CASE_B2, B::CASE_B3),
'amount' => g\float(),
'flag' => g\bool(),
Generators::associative([
'type' => Generators::constant(P::Type_b),
'case' => Generators::elements(B::CASE_B1, B::CASE_B2, B::CASE_B3),
'amount' => Generators::float(),
'flag' => Generators::bool(),
])
)
->then(function (array $i) use ($codec): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public function test(): void
* @psalm-type V = string | int
* @psalm-param Validation<non-empty-array<K, V>> $v
*/
function assert1(Validation $v): void
{
}
$assert1 = function (Validation $v): void {
};

assert1($v);
$assert1($v);
}
}
Loading

0 comments on commit 9cd82db

Please sign in to comment.