diff --git a/src/PhpGenerator/ClassType.php b/src/PhpGenerator/ClassType.php index 98b2cf99..64c72adb 100644 --- a/src/PhpGenerator/ClassType.php +++ b/src/PhpGenerator/ClassType.php @@ -46,8 +46,8 @@ class ClassType extends Nette\Object /** @var string[] */ private $traits = []; - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @var array name => value */ private $consts = []; @@ -71,7 +71,7 @@ public static function from($from) $class->final = $from->isFinal() && $class->type === 'class'; $class->abstract = $from->isAbstract() && $class->type === 'class'; $class->implements = $from->getInterfaceNames(); - $class->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + $class->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; $namespace = $from->getNamespaceName(); if ($from->getParentClass()) { $class->extends = $from->getParentClass()->getName(); @@ -118,8 +118,8 @@ public function __toString() $properties = []; foreach ($this->properties as $property) { - $doc = str_replace("\n", "\n * ", implode("\n", $property->getDocuments())); - $properties[] = ($property->getDocuments() ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '') + $doc = str_replace("\n", "\n * ", $property->getComment()); + $properties[] = ($doc ? (strpos($doc, "\n") === FALSE ? "/** $doc */\n" : "/**\n * $doc\n */\n") : '') . $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName() . ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value)) . ";\n"; @@ -139,7 +139,7 @@ public function __toString() } return Strings::normalize( - ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') + ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . $this->type . ' ' @@ -347,22 +347,22 @@ public function addTrait($trait) /** - * @param string[] + * @param string|NULL * @return self */ - public function setDocuments(array $s) + public function setComment($val) { - $this->documents = $s; + $this->comment = $val ? (string) $val : NULL; return $this; } /** - * @return string[] + * @return string|NULL */ - public function getDocuments() + public function getComment() { - return $this->documents; + return $this->comment; } @@ -370,13 +370,34 @@ public function getDocuments() * @param string * @return self */ - public function addDocument($s) + public function addComment($val) { - $this->documents[] = (string) $s; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); + } + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + + /** * @return self */ diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 8325a1d9..99283bb1 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -45,8 +45,8 @@ class Method extends Nette\Object /** @var bool */ private $variadic = FALSE; - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @var PhpNamespace|NULL */ private $namespace; @@ -82,7 +82,7 @@ public static function from($from) } $method->returnReference = $from->returnsReference(); $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); - $method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + $method->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) { $returnType = $from->getReturnType(); $method->returnType = $returnType->isBuiltin() ? (string) $returnType : '\\' . $returnType; @@ -118,7 +118,7 @@ public function __toString() ? $this->returnType : $this->namespace->unresolveName($this->returnType); - return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '') + return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '') . ($this->abstract ? 'abstract ' : '') . ($this->final ? 'final ' : '') . ($this->visibility ? $this->visibility . ' ' : '') @@ -374,23 +374,25 @@ public function isVariadic() } + + /** - * @param string[] + * @param string|NULL * @return self */ - public function setDocuments(array $val) + public function setComment($val) { - $this->documents = $val; + $this->comment = $val ? (string) $val : NULL; return $this; } /** - * @return string[] + * @return string|NULL */ - public function getDocuments() + public function getComment() { - return $this->documents; + return $this->comment; } @@ -398,13 +400,34 @@ public function getDocuments() * @param string * @return self */ - public function addDocument($val) + public function addComment($val) { - $this->documents[] = (string) $val; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); + } + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + + /** * @return self */ diff --git a/src/PhpGenerator/PhpFile.php b/src/PhpGenerator/PhpFile.php index 02a342f2..d0bfd8f1 100644 --- a/src/PhpGenerator/PhpFile.php +++ b/src/PhpGenerator/PhpFile.php @@ -21,30 +21,32 @@ */ class PhpFile extends Object { - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @var PhpNamespace[] */ private $namespaces = []; + + /** - * @return string[] + * @param string|NULL + * @return self */ - public function getDocuments() + public function setComment($val) { - return $this->documents; + $this->comment = $val ? (string) $val : NULL; + return $this; } /** - * @param string[] - * @return self + * @return string|NULL */ - public function setDocuments(array $documents) + public function getComment() { - $this->documents = $documents; - return $this; + return $this->comment; } @@ -52,13 +54,34 @@ public function setDocuments(array $documents) * @param string * @return self */ - public function addDocument($document) + public function addComment($val) { - $this->documents[] = $document; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); + } + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + + /** * @param string * @return ClassType @@ -119,7 +142,7 @@ public function __toString() return Strings::normalize( "documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n\n" : '') + . ($this->comment ? "\n" . str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n\n" : '') . implode("\n\n", $this->namespaces) ) . "\n"; } diff --git a/src/PhpGenerator/Property.php b/src/PhpGenerator/Property.php index 2f833b87..5bd427a7 100644 --- a/src/PhpGenerator/Property.php +++ b/src/PhpGenerator/Property.php @@ -27,8 +27,8 @@ class Property extends Nette\Object /** @var string public|protected|private */ private $visibility = 'public'; - /** @var string[] */ - private $documents = []; + /** @var string|NULL */ + private $comment; /** @@ -42,7 +42,7 @@ public static function from(\ReflectionProperty $from) $prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL; $prop->static = $from->isStatic(); $prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public'); - $prop->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : []; + $prop->comment = $from->getDocComment() ? preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL; return $prop; } @@ -129,23 +129,25 @@ public function getVisibility() } + + /** - * @param string[] + * @param string|NULL * @return self */ - public function setDocuments(array $s) + public function setComment($val) { - $this->documents = $s; + $this->comment = $val ? (string) $val : NULL; return $this; } /** - * @return string[] + * @return string|NULL */ - public function getDocuments() + public function getComment() { - return $this->documents; + return $this->comment; } @@ -153,10 +155,31 @@ public function getDocuments() * @param string * @return self */ - public function addDocument($s) + public function addComment($val) { - $this->documents[] = (string) $s; + $this->comment .= $this->comment ? "\n$val" : $val; return $this; } + + /** @deprecated */ + public function setDocuments(array $s) + { + return $this->setComment(implode("\n", $s)); +} + + + /** @deprecated */ + public function getDocuments() + { + return $this->comment ? [$this->comment] : []; + } + + + /** @deprecated */ + public function addDocument($s) + { + return $this->addComment($s); + } + } diff --git a/tests/PhpGenerator/ClassType.interface.phpt b/tests/PhpGenerator/ClassType.interface.phpt index 75dd4905..f69b634f 100644 --- a/tests/PhpGenerator/ClassType.interface.phpt +++ b/tests/PhpGenerator/ClassType.interface.phpt @@ -16,7 +16,7 @@ $interface ->setType('interface') ->addExtend('IOne') ->addExtend('ITwo') - ->addDocument('Description of interface'); + ->addComment('Description of interface'); $interface->addMethod('getForm'); diff --git a/tests/PhpGenerator/ClassType.phpt b/tests/PhpGenerator/ClassType.phpt index bc6575ee..6ac11b5e 100644 --- a/tests/PhpGenerator/ClassType.phpt +++ b/tests/PhpGenerator/ClassType.phpt @@ -20,8 +20,8 @@ $class ->addImplement('IExample') ->addImplement('IOne') ->addTrait('ObjectTrait') - ->addDocument("Description of class.\nThis is example\n") - ->addDocument('@property-read Nette\Forms\Form $form'); + ->addComment("Description of class.\nThis is example\n") + ->addComment('@property-read Nette\Forms\Form $form'); $class ->addConst('ROLE', 'admin') @@ -29,7 +29,7 @@ $class $class->addProperty('handle') ->setVisibility('private') - ->addDocument('@var resource orignal file handle'); + ->addComment('@var resource orignal file handle'); $class->addProperty('order') ->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST')); @@ -40,8 +40,8 @@ $p = $class->addProperty('sections', ['first' => TRUE]) Assert::same($p, $class->getProperty('sections')); $m = $class->addMethod('getHandle') - ->addDocument('Returns file handle.') - ->addDocument('@return resource') + ->addComment('Returns file handle.') + ->addComment('@return resource') ->setFinal(TRUE) ->setBody('return $this->?;', ['handle']); diff --git a/tests/PhpGenerator/PhpFile.phpt b/tests/PhpGenerator/PhpFile.phpt index ef95b967..d7fe0607 100644 --- a/tests/PhpGenerator/PhpFile.phpt +++ b/tests/PhpGenerator/PhpFile.phpt @@ -13,8 +13,8 @@ require __DIR__ . '/../bootstrap.php'; $file = new PhpFile; -$file->addDocument('This file is auto-generated. DO NOT EDIT!'); -$file->addDocument('Hey there, I\'m here to document things.'); +$file->addComment('This file is auto-generated. DO NOT EDIT!'); +$file->addComment('Hey there, I\'m here to document things.'); $namespaceFoo = $file->addNamespace('Foo');