Skip to content

Commit 059a93b

Browse files
committed
Method: body is always string, even for interfaces (BC break)
1 parent fe54e51 commit 059a93b

File tree

5 files changed

+5
-33
lines changed

5 files changed

+5
-33
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,6 @@ public function removeTrait(string $name): self
390390
public function addMember($member): self
391391
{
392392
if ($member instanceof Method) {
393-
if ($this->isInterface()) {
394-
$member->setBody(null);
395-
}
396-
397393
$this->methods[strtolower($member->getName())] = $member;
398394

399395
} elseif ($member instanceof Property) {
@@ -601,9 +597,7 @@ public function getMethod(string $name): Method
601597
public function addMethod(string $name): Method
602598
{
603599
$method = new Method($name);
604-
if ($this->isInterface()) {
605-
$method->setBody(null);
606-
} else {
600+
if (!$this->isInterface()) {
607601
$method->setPublic();
608602
}
609603

src/PhpGenerator/Factory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
149149
$method->setVisibility($isInterface ? null : $this->getVisibility($from));
150150
$method->setFinal($from->isFinal());
151151
$method->setAbstract($from->isAbstract() && !$isInterface);
152-
$method->setBody($from->isAbstract() ? null : '');
153152
$method->setReturnReference($from->returnsReference());
154153
$method->setVariadic($from->isVariadic());
155154
$method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));

src/PhpGenerator/Method.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ final class Method
2626
use Traits\CommentAware;
2727
use Traits\AttributeAware;
2828

29-
/** @var string|null */
30-
private $body = '';
31-
3229
/** @var bool */
3330
private $static = false;
3431

@@ -54,22 +51,6 @@ public function __toString(): string
5451
}
5552

5653

57-
/** @return static */
58-
public function setBody(?string $code, ?array $args = null): self
59-
{
60-
$this->body = $args === null || $code === null
61-
? $code
62-
: (new Dumper)->format($code, ...$args);
63-
return $this;
64-
}
65-
66-
67-
public function getBody(): ?string
68-
{
69-
return $this->body;
70-
}
71-
72-
7354
/** @return static */
7455
public function setStatic(bool $state = true): self
7556
{

src/PhpGenerator/Printer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ public function printArrowFunction(Closure $closure, ?PhpNamespace $namespace =
112112
}
113113

114114

115-
public function printMethod(Method $method, ?PhpNamespace $namespace = null): string
115+
public function printMethod(Method $method, ?PhpNamespace $namespace = null, bool $isInterface = false): string
116116
{
117117
$this->namespace = $this->resolveTypes ? $namespace : null;
118118
$method->validate();
119-
$line = ($method->isAbstract() ? 'abstract ' : '')
119+
$line = ($method->isAbstract() && !$isInterface ? 'abstract ' : '')
120120
. ($method->isFinal() ? 'final ' : '')
121121
. ($method->getVisibility() ? $method->getVisibility() . ' ' : '')
122122
. ($method->isStatic() ? 'static ' : '')
@@ -132,7 +132,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null): st
132132
. $line
133133
. $params
134134
. $returnType
135-
. ($method->isAbstract() || $method->getBody() === null
135+
. ($method->isAbstract() || $isInterface
136136
? ";\n"
137137
: (strpos($params, "\n") === false ? "\n" : ' ')
138138
. "{\n"
@@ -206,7 +206,7 @@ public function printClass(ClassType $class, ?PhpNamespace $namespace = null): s
206206

207207
$methods = [];
208208
foreach ($class->getMethods() as $method) {
209-
$methods[] = $this->printMethod($method, $namespace);
209+
$methods[] = $this->printMethod($method, $namespace, $class->isInterface());
210210
}
211211

212212
$members = array_filter([

tests/PhpGenerator/ClassType.addMember.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,3 @@ Assert::same('', $method->getBody());
3232
$class = (new ClassType('Example'))
3333
->setType('interface')
3434
->addMember($method = new Nette\PhpGenerator\Method('getHandle'));
35-
36-
Assert::null($method->getBody());

0 commit comments

Comments
 (0)