Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions src/PhpGenerator/ClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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();
Expand Down Expand Up @@ -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";
Expand All @@ -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 . ' '
Expand Down Expand Up @@ -347,36 +347,57 @@ 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;
}


/**
* @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
*/
Expand Down
47 changes: 35 additions & 12 deletions src/PhpGenerator/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 . ' ' : '')
Expand Down Expand Up @@ -374,37 +374,60 @@ 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;
}


/**
* @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
*/
Expand Down
49 changes: 36 additions & 13 deletions src/PhpGenerator/PhpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,67 @@
*/
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;
}


/**
* @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
Expand Down Expand Up @@ -119,7 +142,7 @@ public function __toString()

return Strings::normalize(
"<?php\n"
. ($this->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";
}
Expand Down
45 changes: 34 additions & 11 deletions src/PhpGenerator/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -129,34 +129,57 @@ 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;
}


/**
* @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);
}

}
2 changes: 1 addition & 1 deletion tests/PhpGenerator/ClassType.interface.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $interface
->setType('interface')
->addExtend('IOne')
->addExtend('ITwo')
->addDocument('Description of interface');
->addComment('Description of interface');

$interface->addMethod('getForm');

Expand Down
Loading