Skip to content

Commit 4a0b4a0

Browse files
authored
Add native typhints to all hamcrest classes (+ install phpstan) (#88)
1 parent 2f2740a commit 4a0b4a0

File tree

80 files changed

+944
-488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+944
-488
lines changed
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1-
name: tests
1+
name: ci
22

33
on:
44
push:
55
pull_request:
66

77
jobs:
8-
tests:
8+
static_analysis:
9+
name: Static analysis
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
915

16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '8.0'
20+
extensions: curl
21+
tools: composer:v2
22+
coverage: none
23+
24+
- name: Install PHP dependencies
25+
run: composer update --prefer-dist --no-interaction --no-progress
26+
27+
- name: Run PHPStan
28+
run: vendor/bin/phpstan analyze
29+
30+
tests:
31+
name: Tests ${{ matrix.php }}
1032
runs-on: ubuntu-latest
1133
strategy:
1234
matrix:
@@ -18,8 +40,6 @@ jobs:
1840
- '8.3'
1941
- '8.4'
2042

21-
name: PHP ${{ matrix.php }}
22-
2343
steps:
2444
- name: Checkout code
2545
uses: actions/checkout@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
composer.lock
22
tests/.phpunit.result.cache
3+
tests/phpunit.xml
34
vendor

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
"require-dev": {
2424
"phpunit/php-file-iterator": "^1.4 || ^2.0 || ^3.0",
25-
"phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0"
25+
"phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0",
26+
"phpstan/phpstan": "^2.1",
27+
"phpstan/phpstan-phpunit": "^2.0"
2628
},
2729

2830
"replace": {
@@ -33,7 +35,7 @@
3335

3436
"extra": {
3537
"branch-alias": {
36-
"dev-master": "2.1-dev"
38+
"dev-master": "3.0-dev"
3739
}
3840
}
3941
}

generator/FactoryFile.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function generateDeclaration($name, FactoryMethod $method)
6565
$code = $this->indent . $this->getDeclarationModifiers()
6666
. 'function ' . $name . '('
6767
. $this->generateDeclarationArguments($method)
68-
. ')' . "\n" . $this->indent . '{' . "\n";
68+
. '): ' . $this->generateReturnType($method) . "\n" . $this->indent . '{' . "\n";
6969
return $code;
7070
}
7171

@@ -83,6 +83,22 @@ public function generateDeclarationArguments(FactoryMethod $method)
8383
}
8484
}
8585

86+
public function generateReturnType(FactoryMethod $method): string
87+
{
88+
$call = $method->getCalls()[0];
89+
if (!$call instanceof FactoryCall) {
90+
throw new Exception('The first call in the FactoryMethod cannot be used to determine the return type. Method: '.$method->getName());
91+
}
92+
93+
$returnType = $call->getMethod()->getReturnType();
94+
95+
if (!$returnType) {
96+
throw new \Exception('The first calls FactoryMethod cannot be used to determine the return type. Method: '.$method->getName());
97+
}
98+
99+
return sprintf('\\%s', $returnType);
100+
}
101+
86102
public function generateImport(FactoryMethod $method)
87103
{
88104
return $this->indent . self::INDENT . "require_once '" . $method->getClass()->getFile() . "';" . "\n";

generator/FactoryMethod.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ public function getFullName()
214214
return $this->getClassName() . '::' . $this->getName();
215215
}
216216

217+
public function getReturnType(): ?string
218+
{
219+
if (!$this->reflector->hasReturnType()) {
220+
return null;
221+
}
222+
223+
$returnType = $this->reflector->getReturnType()->getName();
224+
225+
if ($returnType === 'self') {
226+
return $this->reflector->getDeclaringClass()->getName();
227+
}
228+
229+
return $returnType;
230+
}
231+
217232
public function getCommentText()
218233
{
219234
return implode("\n", $this->comment);

generator/parts/functions_header.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (!function_exists('assertThat')) {
1313
* assertThat("some error", $a > $b);
1414
* </pre>
1515
*/
16-
function assertThat()
16+
function assertThat(): void
1717
{
1818
$args = func_get_args();
1919
call_user_func_array(

0 commit comments

Comments
 (0)