Skip to content

Commit

Permalink
feat: Upgrade to PHPStan 2.x (#2)
Browse files Browse the repository at this point in the history
* Upgrade to PHPStan 2.x

* Add tests on FakerProviderReflectionExtension

* Add Gitub Actions configuration
  • Loading branch information
jdecool authored Dec 5, 2024
1 parent d962f2d commit 9a7490f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 8 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Tests

on:
push:
pull_request:
schedule:
- cron: '0 1 * * *'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.1', '8.2', '8.3' ]
steps:
- uses: actions/checkout@v4
- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer
- run: composer install
- run: composer test

format:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.1' ]
steps:
- uses: actions/checkout@v4
- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer
- run: composer install
- run: composer format

analyse:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.1' ]
steps:
- uses: actions/checkout@v4
- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer
- run: composer install
- run: composer analyse
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@
"require": {
"php": "^7.4|^8.0",
"ext-json": "*",
"phpstan/phpstan": "^1.0"
"phpstan/phpstan": "^1.0|^2.0"
},
"require-dev": {
"fakerphp/faker": "^1.21",
"laravel/pint": "^1.2"
"laravel/pint": "^1.2",
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": {
"Swis\\PHPStan\\Reflection\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Swis\\PHPStan\\Reflection\\Tests\\": "tests"
}
},
"extra": {
"phpstan": {
"includes": [
Expand All @@ -40,7 +46,8 @@
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"test": "vendor/bin/phpunit"
},
"config": {
"sort-packages": true
Expand Down
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
10 changes: 5 additions & 5 deletions src/FakerProviderReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;

class FakerProviderReflectionExtension implements PropertiesClassReflectionExtension, MethodsClassReflectionExtension
class FakerProviderReflectionExtension implements MethodsClassReflectionExtension, PropertiesClassReflectionExtension
{
protected ReflectionProvider $reflectionProvider;

Expand Down Expand Up @@ -51,7 +51,7 @@ public function getMethod(ClassReflection $classReflection, string $methodName):
if ($method === null) {
// This should never happen, because hasMethod() should always be
// called first.
throw new \PHPStan\ShouldNotHappenException();
throw new \PHPStan\ShouldNotHappenException;
}

return $method;
Expand All @@ -69,7 +69,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
if ($property === null) {
// This should never happen, because hasProperty() should always be
// called first.
throw new \PHPStan\ShouldNotHappenException();
throw new \PHPStan\ShouldNotHappenException;
}

return $property;
Expand Down Expand Up @@ -102,8 +102,8 @@ protected function findMethod(ClassReflection $classReflection, string $methodNa
foreach ($this->providerClasses as $providerClass) {
$providerReflection = $this->reflectionProvider->getClass($providerClass);
try {
$methodReflection = $providerReflection->getMethod($methodName, new OutOfClassScope());
if (!$methodReflection->isStatic() && $methodReflection->isPublic()) {
$methodReflection = $providerReflection->getMethod($methodName, new OutOfClassScope);
if (! $methodReflection->isStatic() && $methodReflection->isPublic()) {
return $methodReflection;
}
} catch (MissingMethodFromReflectionException $e) {
Expand Down
13 changes: 13 additions & 0 deletions tests/BookProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Swis\PHPStan\Reflection\Tests;

use Faker\Provider\Base;

class BookProvider extends Base
{
public function customBookIsbn(): string
{
return $this->generator->ean13();
}
}
35 changes: 35 additions & 0 deletions tests/FakerProviderReflectionExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Swis\PHPStan\Reflection\Tests;

use Faker;
use PHPStan\Testing\PHPStanTestCase;
use Swis\PHPStan\Reflection\FakerProviderReflectionExtension;

class FakerProviderReflectionExtensionTest extends PHPStanTestCase
{
public function testProviderCustomDataIsAvailableOnFakerGenerator(): void
{
$faker = new Faker\Generator;
$faker->addProvider(new BookProvider($faker));

$reflectionProvider = self::createReflectionProvider();
$sut = new FakerProviderReflectionExtension($reflectionProvider, [BookProvider::class]);

$classReflection = $reflectionProvider->getClass(Faker\Generator::class);

$this->assertTrue($sut->hasMethod($classReflection, 'customBookIsbn'));
$this->assertTrue($sut->hasProperty($classReflection, 'customBookIsbn'));
}

public function testProviderCustomDataIsNotAvailabledOutsideOfFakerGenerator(): void
{
$reflectionProvider = self::createReflectionProvider();
$sut = new FakerProviderReflectionExtension($reflectionProvider, [BookProvider::class]);

$classReflection = $reflectionProvider->getClass(BookProvider::class);

$this->assertFalse($sut->hasMethod($classReflection, 'customBookIsbn'));
$this->assertFalse($sut->hasProperty($classReflection, 'customBookIsbn'));
}
}

0 comments on commit 9a7490f

Please sign in to comment.