Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
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