Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: static-analysis

on:
push:
pull_request:

jobs:
static_analysis:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do this:

  1. move this job into an existing YML file instead of adding a new one
  2. for an existing YML file:
    1. rename into ci.yml
    2. replace name: tests with name: ci
    3. replace name: PHP ${{ matrix.php }} into name: Tests ${{ matrix.php }}

This way we'll have all CI runs in one place. Right now I have to jump through the hooks to see static analysis and tests of the same PR on the https://github.com/hamcrest/hamcrest-php/actions page.

Example result: https://github.com/minkphp/Mink/actions/runs/13197030721.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


runs-on: ubuntu-latest

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
extensions: curl
tools: composer:v2
coverage: none

- name: Install PHP dependencies
run: composer update --prefer-dist --no-interaction --no-progress

- name: Run PHPStan
run: vendor/bin/phpstan analyze
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
tests/.phpunit.result.cache
tests/phpunit.xml
vendor
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

"require-dev": {
"phpunit/php-file-iterator": "^1.4 || ^2.0 || ^3.0",
"phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0"
"phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0"
},

"replace": {
Expand All @@ -33,7 +35,7 @@

"extra": {
"branch-alias": {
"dev-master": "2.1-dev"
"dev-master": "3.0-dev"
}
}
}
18 changes: 17 additions & 1 deletion generator/FactoryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function generateDeclaration($name, FactoryMethod $method)
$code = $this->indent . $this->getDeclarationModifiers()
. 'function ' . $name . '('
. $this->generateDeclarationArguments($method)
. ')' . "\n" . $this->indent . '{' . "\n";
. '): ' . $this->generateReturnType($method) . "\n" . $this->indent . '{' . "\n";
return $code;
}

Expand All @@ -83,6 +83,22 @@ public function generateDeclarationArguments(FactoryMethod $method)
}
}

public function generateReturnType(FactoryMethod $method): string
{
$call = $method->getCalls()[0];
if (!$call instanceof FactoryCall) {
throw new Exception('The first call in the FactoryMethod cannot be used to determine the return type. Method: '.$method->getName());
}

$returnType = $call->getMethod()->getReturnType();

if (!$returnType) {
Comment thread
pscheit marked this conversation as resolved.
throw new \Exception('The first calls FactoryMethod cannot be used to determine the return type. Method: '.$method->getName());
}

return sprintf('\\%s', $returnType);
}

public function generateImport(FactoryMethod $method)
{
return $this->indent . self::INDENT . "require_once '" . $method->getClass()->getFile() . "';" . "\n";
Expand Down
15 changes: 15 additions & 0 deletions generator/FactoryMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ public function getFullName()
return $this->getClassName() . '::' . $this->getName();
}

public function getReturnType(): ?string
Comment thread
pscheit marked this conversation as resolved.
{
if (!$this->reflector->hasReturnType()) {
return null;
}

$returnType = $this->reflector->getReturnType()->getName();

if ($returnType === 'self') {
return $this->reflector->getDeclaringClass()->getName();
}

return $returnType;
}

public function getCommentText()
{
return implode("\n", $this->comment);
Expand Down
2 changes: 1 addition & 1 deletion generator/parts/functions_header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (!function_exists('assertThat')) {
* assertThat("some error", $a > $b);
* </pre>
*/
function assertThat()
function assertThat(): void
{
$args = func_get_args();
call_user_func_array(
Expand Down
Loading