Skip to content

Commit 0a20a9f

Browse files
authored
Merge pull request #11 from ondrejmirtes/phpstan-1
PHPStan 1.0 support
2 parents f9fa35c + eaa2359 commit 0a20a9f

13 files changed

+206
-140
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
indent_style = space
9+
indent_size = 4
10+
11+
[*.yml]
12+
indent_size = 2

.github/workflows/phpstan.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: PHPStan
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
phpstan:
11+
name: "PHPStan"
12+
runs-on: "ubuntu-latest"
13+
timeout-minutes: 30
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
php-version:
19+
- "7.4"
20+
- "8.0"
21+
22+
steps:
23+
- name: "Checkout"
24+
uses: "actions/checkout@v2"
25+
26+
- name: "Install PHP"
27+
uses: "shivammathur/setup-php@v2"
28+
with:
29+
coverage: "none"
30+
php-version: "${{ matrix.php-version }}"
31+
extensions: mbstring
32+
33+
- name: "Install dependencies"
34+
run: "composer install --no-interaction --no-progress --no-suggest"
35+
36+
- name: "PHPStan"
37+
run: "vendor/bin/phpstan"

.github/workflows/test.Dockerfile

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Tests with code coverage
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
tests:
11+
name: "Tests"
12+
runs-on: "ubuntu-latest"
13+
timeout-minutes: 30
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
php-version:
19+
- "7.2"
20+
- "7.3"
21+
- "7.4"
22+
- "8.0"
23+
- "8.1"
24+
25+
steps:
26+
- name: "Checkout"
27+
uses: "actions/checkout@v2"
28+
29+
- name: "Install PHP"
30+
uses: "shivammathur/setup-php@v2"
31+
with:
32+
coverage: "pcov"
33+
php-version: "${{ matrix.php-version }}"
34+
extensions: mbstring
35+
36+
- name: "Install dependencies"
37+
run: "composer install --no-interaction --no-progress --no-suggest"
38+
39+
- name: "Tests"
40+
run: |
41+
php -d 'zend.assertions=1' -d 'pcov.enabled=1' -d 'pcov.directory=src' vendor/bin/phpunit --coverage-clover=.clover.xml
42+
43+
- name: "Upload Codecov Report"
44+
uses: "codecov/codecov-action@v1"
45+
with:
46+
token: ${{ secrets.CODECOV_TOKEN }}
47+
file: .clover.xml

.github/workflows/tests.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
tests:
11+
name: "Tests"
12+
runs-on: "ubuntu-latest"
13+
timeout-minutes: 30
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
php-version:
19+
- "7.1"
20+
21+
steps:
22+
- name: "Checkout"
23+
uses: "actions/checkout@v2"
24+
25+
- name: "Install PHP"
26+
uses: "shivammathur/setup-php@v2"
27+
with:
28+
coverage: "none"
29+
php-version: "${{ matrix.php-version }}"
30+
extensions: mbstring
31+
32+
- name: "Install dependencies"
33+
run: "composer install --no-interaction --no-progress --no-suggest"
34+
35+
- name: "Tests"
36+
run: "vendor/bin/phpunit"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
!.gitignore
44
!.gitattributes
55
!.github
6+
!.editorconfig
67

78
#composer
89
composer.lock
910
vendor
11+
12+
#phpstan
13+
phpstan.neon

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"php": "^7.1 | ^8.0",
99
"marc-mabe/php-enum": "^1.1 || ^2.0 || ^3.0 || ^4.0",
10-
"phpstan/phpstan": "^0.12"
10+
"phpstan/phpstan": "^1.0"
1111
},
1212
"require-dev": {
1313
"phpunit/phpunit": "^7.5 | ^8.5 | 9.4"

phpstan.neon.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
includes:
2+
- extension.neon
3+
4+
parameters:
5+
level: 5
6+
paths:
7+
- src
8+
- tests
9+
excludePaths:
10+
- tests/assets
11+
- tests/integration/data

src/EnumDynamicReturnTypeExtension.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\MethodReflection;
1010
use PHPStan\Reflection\ParametersAcceptorSelector;
11-
use PHPStan\Type\ArrayType;
12-
use PHPStan\Type\Constant\ConstantArrayType;
11+
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1312
use PHPStan\Type\ConstantTypeHelper;
1413
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1514
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
@@ -159,10 +158,17 @@ private function detectGetValueReturnType(string $enumeration): Type
159158
* Returns return type of Enum::getValues()
160159
* @phpstan-param class-string<Enum> $enumeration
161160
*/
162-
private function detectGetValuesReturnType(string $enumeration): ArrayType
161+
private function detectGetValuesReturnType(string $enumeration): Type
163162
{
164163
$keyTypes = $this->enumOrdinalTypes($enumeration);
165164
$valueTypes = $this->enumValueTypes($enumeration);
166-
return new ConstantArrayType($keyTypes, $valueTypes, count($keyTypes));
165+
166+
$builder = ConstantArrayTypeBuilder::createEmpty();
167+
foreach ($keyTypes as $i => $keyType) {
168+
$valueType = $valueTypes[$i];
169+
$builder->setOffsetValueType($keyType, $valueType);
170+
}
171+
172+
return $builder->getArray();
167173
}
168174
}

tests/unit/EnumDynamicReturnTypeExtensionTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
use MabeEnum\Enum;
66
use MabeEnumPHPStan\EnumDynamicReturnTypeExtension;
7-
use MabeEnumPHPStan\EnumMethodsClassReflectionExtension;
8-
use PHPStan\Reflection\Dummy\DummyMethodReflection;
9-
use PHPStan\Testing\TestCase;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Testing\PHPStanTestCase;
109

11-
class EnumDynamicReturnTypeExtensionTest extends TestCase
10+
class EnumDynamicReturnTypeExtensionTest extends PHPStanTestCase
1211
{
1312
/**
14-
* @var EnumMethodsClassReflectionExtension
13+
* @var EnumDynamicReturnTypeExtension
1514
*/
1615
protected $extension;
1716

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

27+
private function createMethodWithName(string $name): MethodReflection
28+
{
29+
$method = $this->createMock(MethodReflection::class);
30+
$method->method('getName')->willReturn($name);
31+
32+
return $method;
33+
}
34+
2835
/** @dataProvider staticMethodsProvider */
2936
public function testIsStaticMethodSupportedShouldReturnTrue(string $method): void
3037
{
31-
$reflectionMethod = new DummyMethodReflection($method);
38+
$reflectionMethod = $this->createMethodWithName($method);
3239
$this->assertTrue($this->extension->isStaticMethodSupported($reflectionMethod));
3340

34-
$reflectionMethod = new DummyMethodReflection(strtolower($method));
41+
$reflectionMethod = $this->createMethodWithName(strtolower($method));
3542
$this->assertTrue($this->extension->isStaticMethodSupported($reflectionMethod));
3643

37-
$reflectionMethod = new DummyMethodReflection(strtoupper($method));
44+
$reflectionMethod = $this->createMethodWithName(strtoupper($method));
3845
$this->assertTrue($this->extension->isStaticMethodSupported($reflectionMethod));
3946
}
4047

4148
public function testIsStaticMethodSupportedShouldReturnFalse(): void
4249
{
43-
$reflectionMethod = new DummyMethodReflection('fooBar');
50+
$reflectionMethod = $this->createMethodWithName('fooBar');
4451
$this->assertFalse($this->extension->isStaticMethodSupported($reflectionMethod));
4552
}
4653

4754
/** @dataProvider objectMethodsProvider */
4855
public function testIsMethodSupportedShouldReturnTrue(string $method): void
4956
{
50-
$reflectionMethod = new DummyMethodReflection($method);
57+
$reflectionMethod = $this->createMethodWithName($method);
5158
$this->assertTrue($this->extension->isMethodSupported($reflectionMethod));
5259

53-
$reflectionMethod = new DummyMethodReflection(strtolower($method));
60+
$reflectionMethod = $this->createMethodWithName(strtolower($method));
5461
$this->assertTrue($this->extension->isMethodSupported($reflectionMethod));
5562

56-
$reflectionMethod = new DummyMethodReflection(strtoupper($method));
63+
$reflectionMethod = $this->createMethodWithName(strtoupper($method));
5764
$this->assertTrue($this->extension->isMethodSupported($reflectionMethod));
5865
}
5966

6067
public function testIsMethodSupportedShouldReturnFalse(): void
6168
{
62-
$reflectionMethod = new DummyMethodReflection('fooBar');
69+
$reflectionMethod = $this->createMethodWithName('fooBar');
6370
$this->assertFalse($this->extension->isMethodSupported($reflectionMethod));
6471
}
6572

0 commit comments

Comments
 (0)