Skip to content
This repository was archived by the owner on Dec 29, 2020. It is now read-only.

Commit feda240

Browse files
committed
Merge pull request #7 from mfn/preserve-new-line
converter: preserve new lines when inserting return type declarations
2 parents 3d8879b + ffb2d25 commit feda240

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Converter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,15 @@ public function convert(Project $project, File $file): string
105105
$return = $this->getReturn($project, $objectType, $namespace, $object, $function);
106106

107107
if ($return && $return[0] && !$return[1]) {
108+
if ($endsInNewLine = $this->endsInNewLine($output)) {
109+
$output = substr($output, 0, -strlen($endsInNewLine));
110+
}
111+
108112
$output .= sprintf(': %s', $return[0]);
113+
114+
if ($endsInNewLine) {
115+
$output .= $endsInNewLine;
116+
}
109117
}
110118

111119
$function = null;
@@ -475,4 +483,26 @@ private function getType(Tag $tag): array
475483

476484
return [$type->__toString(), false];
477485
}
486+
487+
/**
488+
* Determines if the string ends in a new line and returns it.
489+
*
490+
* Will also include additional space/tab character if present, i.e. the
491+
* indentation into the next line.
492+
*
493+
* Returns an empty string if no match was found.
494+
*
495+
* @param string $output
496+
* @return string
497+
*/
498+
private function endsInNewLine(string $output): string
499+
{
500+
$result = preg_match('/((?:\r\n|\r|\n)[ \t]*)$/', $output, $matches);
501+
502+
if (0 === $result) {
503+
return '';
504+
}
505+
506+
return $matches[1];
507+
}
478508
}

tests/test.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ function foo()
5757
*
5858
* @return float
5959
*/
60-
function bar(\DateTime $a = null, array $b, int $c = null, string $d, bool $e, callable $f = null)
61-
: float{
60+
function bar(\DateTime $a = null, array $b, int $c = null, string $d, bool $e, callable $f = null): float
61+
{
6262
return 0.0;
6363
}
6464
@@ -98,8 +98,8 @@ function bat(int $a): string
9898
*
9999
* @return int
100100
*/
101-
function test(string $a = null)
102-
: int{
101+
function test(string $a = null): int
102+
{
103103
}
104104

105105
PHP
@@ -178,8 +178,8 @@ trait BazTrait
178178
*
179179
* @return \DateTime
180180
*/
181-
protected function inTrait(int $a)
182-
: \DateTime{
181+
protected function inTrait(int $a): \DateTime
182+
{
183183
}
184184
}
185185

@@ -210,8 +210,8 @@ public function test(float $a)
210210
/**
211211
* {@inheritdoc}
212212
*/
213-
public function baz(array $a, int $b)
214-
: float{
213+
public function baz(array $a, int $b): float
214+
{
215215
}
216216
}
217217

0 commit comments

Comments
 (0)