Skip to content

Commit

Permalink
Merge pull request #11 from ondrejmirtes/phpstan-1
Browse files Browse the repository at this point in the history
PHPStan 1.0 support
  • Loading branch information
marc-mabe authored Nov 4, 2021
2 parents f9fa35c + eaa2359 commit 0a20a9f
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 140 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.yml]
indent_size = 2
37 changes: 37 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PHPStan

on:
pull_request:
push:
branches:
- master

jobs:
phpstan:
name: "PHPStan"
runs-on: "ubuntu-latest"
timeout-minutes: 30

strategy:
fail-fast: false
matrix:
php-version:
- "7.4"
- "8.0"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
extensions: mbstring

- name: "Install dependencies"
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "PHPStan"
run: "vendor/bin/phpstan"
18 changes: 0 additions & 18 deletions .github/workflows/test.Dockerfile

This file was deleted.

76 changes: 0 additions & 76 deletions .github/workflows/test.yml

This file was deleted.

47 changes: 47 additions & 0 deletions .github/workflows/tests-code-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Tests with code coverage

on:
pull_request:
push:
branches:
- master

jobs:
tests:
name: "Tests"
runs-on: "ubuntu-latest"
timeout-minutes: 30

strategy:
fail-fast: false
matrix:
php-version:
- "7.2"
- "7.3"
- "7.4"
- "8.0"
- "8.1"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
extensions: mbstring

- name: "Install dependencies"
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Tests"
run: |
php -d 'zend.assertions=1' -d 'pcov.enabled=1' -d 'pcov.directory=src' vendor/bin/phpunit --coverage-clover=.clover.xml
- name: "Upload Codecov Report"
uses: "codecov/codecov-action@v1"
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: .clover.xml
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Tests

on:
pull_request:
push:
branches:
- master

jobs:
tests:
name: "Tests"
runs-on: "ubuntu-latest"
timeout-minutes: 30

strategy:
fail-fast: false
matrix:
php-version:
- "7.1"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
extensions: mbstring

- name: "Install dependencies"
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Tests"
run: "vendor/bin/phpunit"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
!.gitignore
!.gitattributes
!.github
!.editorconfig

#composer
composer.lock
vendor

#phpstan
phpstan.neon
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^7.1 | ^8.0",
"marc-mabe/php-enum": "^1.1 || ^2.0 || ^3.0 || ^4.0",
"phpstan/phpstan": "^0.12"
"phpstan/phpstan": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5 | ^8.5 | 9.4"
Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- extension.neon

parameters:
level: 5
paths:
- src
- tests
excludePaths:
- tests/assets
- tests/integration/data
14 changes: 10 additions & 4 deletions src/EnumDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\ConstantTypeHelper;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
Expand Down Expand Up @@ -159,10 +158,17 @@ private function detectGetValueReturnType(string $enumeration): Type
* Returns return type of Enum::getValues()
* @phpstan-param class-string<Enum> $enumeration
*/
private function detectGetValuesReturnType(string $enumeration): ArrayType
private function detectGetValuesReturnType(string $enumeration): Type
{
$keyTypes = $this->enumOrdinalTypes($enumeration);
$valueTypes = $this->enumValueTypes($enumeration);
return new ConstantArrayType($keyTypes, $valueTypes, count($keyTypes));

$builder = ConstantArrayTypeBuilder::createEmpty();
foreach ($keyTypes as $i => $keyType) {
$valueType = $valueTypes[$i];
$builder->setOffsetValueType($keyType, $valueType);
}

return $builder->getArray();
}
}
33 changes: 20 additions & 13 deletions tests/unit/EnumDynamicReturnTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use MabeEnum\Enum;
use MabeEnumPHPStan\EnumDynamicReturnTypeExtension;
use MabeEnumPHPStan\EnumMethodsClassReflectionExtension;
use PHPStan\Reflection\Dummy\DummyMethodReflection;
use PHPStan\Testing\TestCase;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Testing\PHPStanTestCase;

class EnumDynamicReturnTypeExtensionTest extends TestCase
class EnumDynamicReturnTypeExtensionTest extends PHPStanTestCase
{
/**
* @var EnumMethodsClassReflectionExtension
* @var EnumDynamicReturnTypeExtension
*/
protected $extension;

Expand All @@ -25,41 +24,49 @@ public function testGetClass(): void
$this->assertSame(Enum::class, $this->extension->getClass());
}

private function createMethodWithName(string $name): MethodReflection
{
$method = $this->createMock(MethodReflection::class);
$method->method('getName')->willReturn($name);

return $method;
}

/** @dataProvider staticMethodsProvider */
public function testIsStaticMethodSupportedShouldReturnTrue(string $method): void
{
$reflectionMethod = new DummyMethodReflection($method);
$reflectionMethod = $this->createMethodWithName($method);
$this->assertTrue($this->extension->isStaticMethodSupported($reflectionMethod));

$reflectionMethod = new DummyMethodReflection(strtolower($method));
$reflectionMethod = $this->createMethodWithName(strtolower($method));
$this->assertTrue($this->extension->isStaticMethodSupported($reflectionMethod));

$reflectionMethod = new DummyMethodReflection(strtoupper($method));
$reflectionMethod = $this->createMethodWithName(strtoupper($method));
$this->assertTrue($this->extension->isStaticMethodSupported($reflectionMethod));
}

public function testIsStaticMethodSupportedShouldReturnFalse(): void
{
$reflectionMethod = new DummyMethodReflection('fooBar');
$reflectionMethod = $this->createMethodWithName('fooBar');
$this->assertFalse($this->extension->isStaticMethodSupported($reflectionMethod));
}

/** @dataProvider objectMethodsProvider */
public function testIsMethodSupportedShouldReturnTrue(string $method): void
{
$reflectionMethod = new DummyMethodReflection($method);
$reflectionMethod = $this->createMethodWithName($method);
$this->assertTrue($this->extension->isMethodSupported($reflectionMethod));

$reflectionMethod = new DummyMethodReflection(strtolower($method));
$reflectionMethod = $this->createMethodWithName(strtolower($method));
$this->assertTrue($this->extension->isMethodSupported($reflectionMethod));

$reflectionMethod = new DummyMethodReflection(strtoupper($method));
$reflectionMethod = $this->createMethodWithName(strtoupper($method));
$this->assertTrue($this->extension->isMethodSupported($reflectionMethod));
}

public function testIsMethodSupportedShouldReturnFalse(): void
{
$reflectionMethod = new DummyMethodReflection('fooBar');
$reflectionMethod = $this->createMethodWithName('fooBar');
$this->assertFalse($this->extension->isMethodSupported($reflectionMethod));
}

Expand Down
Loading

0 comments on commit 0a20a9f

Please sign in to comment.