Skip to content

Commit 3be0caa

Browse files
committed
Fixed RuntimeMethod
1 parent d83da52 commit 3be0caa

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/ReflectionApi/RuntimeMethod.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ public function addArgument(RuntimeArgument $argument): self
4545
return $this;
4646
}
4747

48-
public function setBody(string ...$expression): self
48+
public function setBody(string ...$lines): self
4949
{
5050
if ($this->abstract) {
5151
throw ReflectionApiException::forAbstractMethodWithBody($this->name);
5252
}
5353

54-
$this->body = $expression;
54+
$this->body = $lines;
5555

5656
return $this;
5757
}
5858

59-
public function addExpression(string $expression): self
59+
public function addLine(string $line): self
6060
{
6161
if ($this->abstract) {
6262
throw ReflectionApiException::forAbstractMethodWithBody($this->name);
6363
}
6464

65-
$this->body[] = $expression;
65+
$this->body[] = $line;
6666

6767
return $this;
6868
}
@@ -106,7 +106,7 @@ public function generateCode(): string
106106
$code .= "\n{";
107107

108108
if ($this->body) {
109-
$code .= "\n\t" . implode(";\n\t", $this->body) . ';';
109+
$code .= "\n\t" . implode("\n\t", $this->body);
110110
}
111111

112112
$code .= "\n}";

tests/Unit/Utils/ReflectionApi/RuntimeMethodTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function testSetBody(): void
4545
{
4646
$method = new RuntimeMethod('doSomething');
4747
$method->setBody(
48-
'$result = $a + $b',
49-
'return $result'
48+
'$result = $a + $b;',
49+
'return $result;'
5050
);
5151

5252
self::assertSame(
@@ -72,6 +72,11 @@ public function testCreatePrivateMethod(): void
7272

7373
public function testAddExpression(): void
7474
{
75+
$method = new RuntimeMethod('doSomething');
76+
$method->addLine('if ($something) {');
77+
$method->addLine("\techo 'test';");
78+
$method->addLine('}');
7579

80+
self::assertSame("public function doSomething()\n{\n\tif (\$something) {\n\t\techo 'test';\n\t}\n}", $method->generateCode());
7681
}
7782
}

0 commit comments

Comments
 (0)