Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 24 additions & 4 deletions .github/workflows/tests.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
name: tests
name: ci

on:
push:
pull_request:

jobs:
tests:
static_analysis:
name: Static analysis
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

tests:
name: Tests ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -18,8 +40,6 @@ jobs:
- '8.3'
- '8.4'

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
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) {
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
{
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