Skip to content

Commit 901830e

Browse files
committed
Merge pull request #12 from dg/comment
documents replaced with comments
2 parents e747943 + 9d3d8d4 commit 901830e

File tree

7 files changed

+148
-58
lines changed

7 files changed

+148
-58
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class ClassType extends Nette\Object
4646
/** @var string[] */
4747
private $traits = [];
4848

49-
/** @var string[] */
50-
private $documents = [];
49+
/** @var string|NULL */
50+
private $comment;
5151

5252
/** @var array name => value */
5353
private $consts = [];
@@ -71,7 +71,7 @@ public static function from($from)
7171
$class->final = $from->isFinal() && $class->type === 'class';
7272
$class->abstract = $from->isAbstract() && $class->type === 'class';
7373
$class->implements = $from->getInterfaceNames();
74-
$class->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : [];
74+
$class->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
7575
$namespace = $from->getNamespaceName();
7676
if ($from->getParentClass()) {
7777
$class->extends = $from->getParentClass()->getName();
@@ -118,8 +118,8 @@ public function __toString()
118118

119119
$properties = [];
120120
foreach ($this->properties as $property) {
121-
$doc = str_replace("\n", "\n * ", implode("\n", $property->getDocuments()));
122-
$properties[] = ($property->getDocuments() ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '')
121+
$doc = str_replace("\n", "\n * ", $property->getComment());
122+
$properties[] = ($doc ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '')
123123
. $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
124124
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
125125
. ";\n";
@@ -139,7 +139,7 @@ public function __toString()
139139
}
140140

141141
return Strings::normalize(
142-
($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '')
142+
($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
143143
. ($this->abstract ? 'abstract ' : '')
144144
. ($this->final ? 'final ' : '')
145145
. $this->type . ' '
@@ -347,36 +347,57 @@ public function addTrait($trait)
347347

348348

349349
/**
350-
* @param string[]
350+
* @param string|NULL
351351
* @return self
352352
*/
353-
public function setDocuments(array $s)
353+
public function setComment($val)
354354
{
355-
$this->documents = $s;
355+
$this->comment = $val ? (string) $val : NULL;
356356
return $this;
357357
}
358358

359359

360360
/**
361-
* @return string[]
361+
* @return string|NULL
362362
*/
363-
public function getDocuments()
363+
public function getComment()
364364
{
365-
return $this->documents;
365+
return $this->comment;
366366
}
367367

368368

369369
/**
370370
* @param string
371371
* @return self
372372
*/
373-
public function addDocument($s)
373+
public function addComment($val)
374374
{
375-
$this->documents[] = (string) $s;
375+
$this->comment .= $this->comment ? "\n$val" : $val;
376376
return $this;
377377
}
378378

379379

380+
/** @deprecated */
381+
public function setDocuments(array $s)
382+
{
383+
return $this->setComment(implode("\n", $s));
384+
}
385+
386+
387+
/** @deprecated */
388+
public function getDocuments()
389+
{
390+
return $this->comment ? [$this->comment] : [];
391+
}
392+
393+
394+
/** @deprecated */
395+
public function addDocument($s)
396+
{
397+
return $this->addComment($s);
398+
}
399+
400+
380401
/**
381402
* @return self
382403
*/

src/PhpGenerator/Method.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class Method extends Nette\Object
4545
/** @var bool */
4646
private $variadic = FALSE;
4747

48-
/** @var string[] */
49-
private $documents = [];
48+
/** @var string|NULL */
49+
private $comment;
5050

5151
/** @var PhpNamespace|NULL */
5252
private $namespace;
@@ -82,7 +82,7 @@ public static function from($from)
8282
}
8383
$method->returnReference = $from->returnsReference();
8484
$method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
85-
$method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : [];
85+
$method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
8686
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
8787
$returnType = $from->getReturnType();
8888
$method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType;
@@ -118,7 +118,7 @@ public function __toString()
118118
? $this->returnType
119119
: $this->namespace->unresolveName($this->returnType);
120120

121-
return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '')
121+
return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
122122
. ($this->abstract ? 'abstract ' : '')
123123
. ($this->final ? 'final ' : '')
124124
. ($this->visibility ? $this->visibility . ' ' : '')
@@ -374,37 +374,60 @@ public function isVariadic()
374374
}
375375

376376

377+
378+
377379
/**
378-
* @param string[]
380+
* @param string|NULL
379381
* @return self
380382
*/
381-
public function setDocuments(array $val)
383+
public function setComment($val)
382384
{
383-
$this->documents = $val;
385+
$this->comment = $val ? (string) $val : NULL;
384386
return $this;
385387
}
386388

387389

388390
/**
389-
* @return string[]
391+
* @return string|NULL
390392
*/
391-
public function getDocuments()
393+
public function getComment()
392394
{
393-
return $this->documents;
395+
return $this->comment;
394396
}
395397

396398

397399
/**
398400
* @param string
399401
* @return self
400402
*/
401-
public function addDocument($val)
403+
public function addComment($val)
402404
{
403-
$this->documents[] = (string) $val;
405+
$this->comment .= $this->comment ? "\n$val" : $val;
404406
return $this;
405407
}
406408

407409

410+
/** @deprecated */
411+
public function setDocuments(array $s)
412+
{
413+
return $this->setComment(implode("\n", $s));
414+
}
415+
416+
417+
/** @deprecated */
418+
public function getDocuments()
419+
{
420+
return $this->comment ? [$this->comment] : [];
421+
}
422+
423+
424+
/** @deprecated */
425+
public function addDocument($s)
426+
{
427+
return $this->addComment($s);
428+
}
429+
430+
408431
/**
409432
* @return self
410433
*/

src/PhpGenerator/PhpFile.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,67 @@
2121
*/
2222
class PhpFile extends Object
2323
{
24-
/** @var string[] */
25-
private $documents = [];
24+
/** @var string|NULL */
25+
private $comment;
2626

2727
/** @var PhpNamespace[] */
2828
private $namespaces = [];
2929

3030

31+
32+
3133
/**
32-
* @return string[]
34+
* @param string|NULL
35+
* @return self
3336
*/
34-
public function getDocuments()
37+
public function setComment($val)
3538
{
36-
return $this->documents;
39+
$this->comment = $val ? (string) $val : NULL;
40+
return $this;
3741
}
3842

3943

4044
/**
41-
* @param string[]
42-
* @return self
45+
* @return string|NULL
4346
*/
44-
public function setDocuments(array $documents)
47+
public function getComment()
4548
{
46-
$this->documents = $documents;
47-
return $this;
49+
return $this->comment;
4850
}
4951

5052

5153
/**
5254
* @param string
5355
* @return self
5456
*/
55-
public function addDocument($document)
57+
public function addComment($val)
5658
{
57-
$this->documents[] = $document;
59+
$this->comment .= $this->comment ? "\n$val" : $val;
5860
return $this;
5961
}
6062

6163

64+
/** @deprecated */
65+
public function setDocuments(array $s)
66+
{
67+
return $this->setComment(implode("\n", $s));
68+
}
69+
70+
71+
/** @deprecated */
72+
public function getDocuments()
73+
{
74+
return $this->comment ? [$this->comment] : [];
75+
}
76+
77+
78+
/** @deprecated */
79+
public function addDocument($s)
80+
{
81+
return $this->addComment($s);
82+
}
83+
84+
6285
/**
6386
* @param string
6487
* @return ClassType
@@ -119,7 +142,7 @@ public function __toString()
119142

120143
return Strings::normalize(
121144
"<?php\n"
122-
. ($this->documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n\n" : '')
145+
. ($this->comment ? "\n" . str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n\n" : '')
123146
. implode("\n\n", $this->namespaces)
124147
) . "\n";
125148
}

src/PhpGenerator/Property.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Property extends Nette\Object
2727
/** @var string public|protected|private */
2828
private $visibility = 'public';
2929

30-
/** @var string[] */
31-
private $documents = [];
30+
/** @var string|NULL */
31+
private $comment;
3232

3333

3434
/**
@@ -42,7 +42,7 @@ public static function from(\ReflectionProperty $from)
4242
$prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL;
4343
$prop->static = $from->isStatic();
4444
$prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public');
45-
$prop->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : [];
45+
$prop->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
4646
return $prop;
4747
}
4848

@@ -129,34 +129,57 @@ public function getVisibility()
129129
}
130130

131131

132+
133+
132134
/**
133-
* @param string[]
135+
* @param string|NULL
134136
* @return self
135137
*/
136-
public function setDocuments(array $s)
138+
public function setComment($val)
137139
{
138-
$this->documents = $s;
140+
$this->comment = $val ? (string) $val : NULL;
139141
return $this;
140142
}
141143

142144

143145
/**
144-
* @return string[]
146+
* @return string|NULL
145147
*/
146-
public function getDocuments()
148+
public function getComment()
147149
{
148-
return $this->documents;
150+
return $this->comment;
149151
}
150152

151153

152154
/**
153155
* @param string
154156
* @return self
155157
*/
156-
public function addDocument($s)
158+
public function addComment($val)
157159
{
158-
$this->documents[] = (string) $s;
160+
$this->comment .= $this->comment ? "\n$val" : $val;
159161
return $this;
160162
}
161163

164+
165+
/** @deprecated */
166+
public function setDocuments(array $s)
167+
{
168+
return $this->setComment(implode("\n", $s));
169+
}
170+
171+
172+
/** @deprecated */
173+
public function getDocuments()
174+
{
175+
return $this->comment ? [$this->comment] : [];
176+
}
177+
178+
179+
/** @deprecated */
180+
public function addDocument($s)
181+
{
182+
return $this->addComment($s);
183+
}
184+
162185
}

tests/PhpGenerator/ClassType.interface.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $interface
1616
->setType('interface')
1717
->addExtend('IOne')
1818
->addExtend('ITwo')
19-
->addDocument('Description of interface');
19+
->addComment('Description of interface');
2020

2121
$interface->addMethod('getForm');
2222

0 commit comments

Comments
 (0)