Skip to content

Commit

Permalink
Enhance wrapped lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Jan 29, 2020
1 parent 93a4910 commit 7f5622f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 25 deletions.
93 changes: 73 additions & 20 deletions src/Base/Generator/PhpDoc/PhpDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public function addLine(string $line, string $type = ''): void
if (!isset($this->lines[$type])) {
$this->lines[$type] = [];
}
foreach (explode("\n", $line) as $l) {
$this->lines[$type][] = $l;
}

$this->lines[$type][] = $line;
}

/**
Expand Down Expand Up @@ -124,7 +123,9 @@ public function buildLines(): array
{
$phpdocLines = [];
$previousType = null;

$this->orderLines();

foreach ($this->lines as $type => $lines) {
if ($previousType === null) {
$previousType = $type;
Expand All @@ -133,31 +134,83 @@ public function buildLines(): array
$phpdocLines[] = '';
$previousType = $type;
}
$this->buildTypedLines($type, $lines, $phpdocLines);
$phpdocLines = array_merge(
$phpdocLines,
$this->buildTypedLines($type, $lines)
);
}
return $phpdocLines;
}

/**
* {@inheritDoc}
*/
public function buildTypedLines(string $type, array $lines, array &$phpdocLines): void
public function buildTypedLines(string $type, array $lines): array
{
$phpdocLines = [];
$linePrefix = $this->buildTypedLinePrefix($type);

foreach ($lines as $line) {
$lineSuffix = ($type === static::TYPE_DESCRIPTION ? '' : '@' . $type . ' ');
foreach (static::wrapLines($lineSuffix . $line, $this->wrapOn) as $i => $l) {
$phpdocLines[] = ($i > 0 ? str_repeat(' ', strlen($lineSuffix)) : '') . $l;
$phpdocLines = array_merge(
$phpdocLines,
$this->buildLinesFromSingleLine($linePrefix, $line)
);
}

return $phpdocLines;
}

/**
* {@inheritDoc}
*/
public function buildTypedLinePrefix(string $type): string
{
if ($type === static::TYPE_DESCRIPTION) {
return '';
}

return sprintf('@%s ', $type);
}

/**
* {@inheritDoc}
*/
public function buildLinesFromSingleLine(string $linePrefix, string $line): array
{
$lines = [];
$linePrefixLength = strlen($linePrefix);
$blankSubLinePrefix = str_repeat(' ', $linePrefixLength);
$explodedLines = explode("\n", $line);

foreach ($explodedLines as $i=>$explodedLine) {
$wrapOn = $this->wrapOn;
if ($i === 0) {
$wrapOn -= $linePrefixLength;
}

$lines = array_merge(
$lines,
$this->wrapLines($explodedLine, $wrapOn)
);
}

foreach ($lines as $i=>$line) {
$subLinePrefix = $i === 0 ? $linePrefix : $blankSubLinePrefix;
$lines[$i] = $subLinePrefix . $line;
}

return $lines;
}

/**
* {@inheritDoc}
*/
public static function wrapLines(string $line, int $wrapOn = 100): array
public function wrapLines(string $line, ?int $wrapOn = null): array
{
$wrapOn = $wrapOn ?? $this->wrapOn;
$lines = [];
$currentLine = '';

foreach (explode(' ', $line) as $word) {
if (iconv_strlen($currentLine . ' ' . $word) > $wrapOn) {
$lines[] = $currentLine;
Expand Down Expand Up @@ -199,29 +252,29 @@ public static function getPossibleTypesFromTypeNames(array $types = []): string
/**
* {@inheritDoc}
*/
public function setWrapOn(int $wrapOn): void
public function orderLines(): void
{
$this->wrapOn = $wrapOn;
uksort($this->lines, function ($k1, $k2) {
$o1 = array_search($k1, static::LINE_TYPE_ORDER);
$o2 = array_search($k2, static::LINE_TYPE_ORDER);

return $o1 - $o2;
});
}

/**
* {@inheritDoc}
*/
public function getWrapOn(): int
public function setWrapOn(int $wrapOn): void
{
return $this->wrapOn;
$this->wrapOn = $wrapOn;
}

/**
* {@inheritDoc}
*/
public function orderLines(): void
public function getWrapOn(): int
{
uksort($this->lines, function ($k1, $k2) {
$o1 = array_search($k1, static::LINE_TYPE_ORDER);
$o2 = array_search($k2, static::LINE_TYPE_ORDER);

return $o1 - $o2;
});
return $this->wrapOn;
}
}
26 changes: 21 additions & 5 deletions src/Base/Generator/PhpDoc/PhpDocGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,35 @@ public static function getPossibleTypesFromTypeNames(array $types = []): string;
public function buildLines(): array;

/**
* @param $type
* @param string $type
* @param string[] $lines
* @param string[] $phpdocLines
*
* @return string[]
*/
public function buildTypedLines(string $type, array $lines): array;

/**
* @param string $type
*
* @return string
*/
public function buildTypedLines(string $type, array $lines, array &$phpdocLines): void;
public function buildTypedLinePrefix(string $type): string;

/**
* @param string $linePrefix
* @param string $line
* @param int $wrapOn
*
* @return string[]
*/
public static function wrapLines(string $line, int $wrapOn = 110): array;
public function buildLinesFromSingleLine(string $linePrefix, string $line): array;

/**
* @param string $line
* @param int|null $wrapOn
*
* @return string[]
*/
public function wrapLines(string $line, ?int $wrapOn = null): array;

/**
* @return bool
Expand Down

0 comments on commit 7f5622f

Please sign in to comment.