Skip to content

Commit ab798cb

Browse files
committed
added Helpers::formatDocComment() & unformatDocComment()
1 parent 959eab1 commit ab798cb

File tree

6 files changed

+54
-8
lines changed

6 files changed

+54
-8
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function from($from)
8080
$class->final = $from->isFinal() && $class->type === 'class';
8181
$class->abstract = $from->isAbstract() && $class->type === 'class';
8282
$class->implements = $from->getInterfaceNames();
83-
$class->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
83+
$class->comment = Helpers::unformatDocComment($from->getDocComment());
8484
if ($from->getParentClass()) {
8585
$class->extends = $from->getParentClass()->getName();
8686
$class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames());
@@ -118,8 +118,7 @@ public function __toString()
118118

119119
$properties = [];
120120
foreach ($this->properties as $property) {
121-
$doc = str_replace("\n", "\n * ", $property->getComment());
122-
$properties[] = ($doc ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '')
121+
$properties[] = Helpers::formatDocComment($property->getComment())
123122
. $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
124123
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
125124
. ";\n";
@@ -130,7 +129,7 @@ public function __toString()
130129
};
131130

132131
return Strings::normalize(
133-
($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
132+
Helpers::formatDocComment($this->comment . "\n")
134133
. ($this->abstract ? 'abstract ' : '')
135134
. ($this->final ? 'final ' : '')
136135
. $this->type . ' '

src/PhpGenerator/Helpers.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,30 @@ public static function formatMember($name)
206206
}
207207

208208

209+
/**
210+
* @return string
211+
*/
212+
public static function formatDocComment($content)
213+
{
214+
if (($s = trim($content)) === '') {
215+
return '';
216+
} elseif (strpos($content, "\n") === FALSE) {
217+
return "/** $s */\n";
218+
} else {
219+
return str_replace("\n", "\n * ", "/**\n$s") . "\n */\n";
220+
}
221+
}
222+
223+
224+
/**
225+
* @return string
226+
*/
227+
public static function unformatDocComment($comment)
228+
{
229+
return preg_replace('#^\s*\* ?#m', '', trim(trim(trim($comment), '/*')));
230+
}
231+
232+
209233
/**
210234
* @return bool
211235
*/

src/PhpGenerator/Method.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function from($from)
8989
}
9090
$method->returnReference = $from->returnsReference();
9191
$method->variadic = $from->isVariadic();
92-
$method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
92+
$method->comment = Helpers::unformatDocComment($from->getDocComment());
9393
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
9494
$method->returnType = (string) $from->getReturnType();
9595
$method->returnNullable = $from->getReturnType()->allowsNull();
@@ -127,7 +127,7 @@ public function __toString()
127127
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
128128
}
129129

130-
return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
130+
return Helpers::formatDocComment($this->comment . "\n")
131131
. ($this->abstract ? 'abstract ' : '')
132132
. ($this->final ? 'final ' : '')
133133
. ($this->visibility ? $this->visibility . ' ' : '')

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function __toString()
145145

146146
return Strings::normalize(
147147
"<?php\n"
148-
. ($this->comment ? "\n" . str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n\n" : '')
148+
. ($this->comment ? "\n" . Helpers::formatDocComment($this->comment . "\n") . "\n" : '')
149149
. implode("\n\n", $this->namespaces)
150150
) . "\n";
151151
}

src/PhpGenerator/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function from(\ReflectionProperty $from)
4343
$prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL;
4444
$prop->static = $from->isStatic();
4545
$prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public');
46-
$prop->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
46+
$prop->comment = Helpers::unformatDocComment($from->getDocComment());
4747
return $prop;
4848
}
4949

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Helpers::formatDocComment() & unformatDocComment()
5+
*/
6+
7+
use Nette\PhpGenerator\Helpers;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
Assert::same('', Helpers::formatDocComment(' '));
15+
Assert::same("/** @var string */\n", Helpers::formatDocComment('@var string'));
16+
Assert::same("/**\n * @var string\n */\n", Helpers::formatDocComment("@var string\n"));
17+
Assert::same("/**\n * A\n * B\n * C\n */\n", Helpers::formatDocComment("A\nB\nC\n"));
18+
19+
Assert::same('', Helpers::unformatDocComment(''));
20+
Assert::same('', Helpers::unformatDocComment("/** */\n\r\t"));
21+
Assert::same('@var string', Helpers::unformatDocComment(" /** @var string */ "));
22+
Assert::same('@var string', Helpers::unformatDocComment("/**\n * @var string\n */"));
23+
Assert::same("A\nB\nC", Helpers::unformatDocComment("/**\n * A\n * B\n * C\n */\n"));

0 commit comments

Comments
 (0)