Skip to content

Commit e7e9667

Browse files
committed
Fix println to be callable without parameters
1 parent 64f9d24 commit e7e9667

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Packages/java/io/PrintStream.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,22 @@ class PrintStream
1111
{
1212
private $sequence = '';
1313

14-
public function println($arg)
14+
public function println(...$arguments)
1515
{
16+
$length = count($arguments);
17+
if ($length > 1) {
18+
throw new \InvalidArgumentException(
19+
"The argument array should have 0 or 1 element, but has {$length} elements."
20+
);
21+
}
22+
23+
if ($length === 0) {
24+
echo "\n";
25+
return;
26+
}
27+
28+
$arg = $arguments[0];
29+
1630
if ($arg instanceof Utf8Info) {
1731
echo $arg->getString() . "\n";
1832
return;

tests/Packages/JavaIoPrintStreamClassTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ private function call($method, ...$arguments)
2323
return ob_get_clean();
2424
}
2525

26+
public function testPrintlnWithoutParams()
27+
{
28+
$result = $this->call(explode('::', __METHOD__)[1]);
29+
$this->assertEquals("\n", $result);
30+
}
31+
2632
public function testPrintlnWithStringParams()
2733
{
2834
$result = $this->call(explode('::', __METHOD__)[1]);

tests/fixtures/java/JavaIoPrintStreamClassTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
class JavaIoPrintStreamClassTest
22
{
3+
public static void testPrintlnWithoutParams()
4+
{
5+
System.out.println();
6+
}
7+
38
public static void testPrintlnWithStringParams()
49
{
510
String x = "Hello World";
@@ -83,4 +88,4 @@ public static void testPrintWithBooleanParams_false()
8388
boolean x = false;
8489
System.out.print(x);
8590
}
86-
}
91+
}

0 commit comments

Comments
 (0)