Skip to content
Closed
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
1 change: 0 additions & 1 deletion src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use RuntimeException;

Expand Down
55 changes: 55 additions & 0 deletions src/DocBlock/Tags/Factory/TokenIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\TokenIterator as PhpStanTokenIterator;
use ReflectionObject;
use ReflectionProperty;

if (InstalledVersions::satisfies(
new VersionParser(),
'phpstan/phpdoc-parser',
'<=1.29.0'
)
) {
final class TokenIterator extends PhpStanTokenIterator
{
private ReflectionProperty $indexProperty;
private ReflectionProperty $tokensProperty;

/** {@inheritDoc} */
public function __construct(array $tokens, int $index = 0)
{
parent::__construct($tokens, $index);

$r = new ReflectionObject($this);
$this->indexProperty = $r->getParentClass()->getProperty('index');
$this->indexProperty->setAccessible(true);
$this->tokensProperty = $r->getParentClass()->getProperty('tokens');
$this->tokensProperty->setAccessible(true);
}

public function getSkippedHorizontalWhiteSpaceIfAny(): string
{
$index = $this->indexProperty->getValue($this);
$tokens = $this->tokensProperty->getValue($this);

if (
$index > 0 && (
$tokens[$index - 1][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS ||
$tokens[$index - 1][Lexer::TYPE_OFFSET] === Lexer::TOKEN_PHPDOC_EOL)
) {
return $tokens[$index - 1][Lexer::VALUE_OFFSET];
}

return '';
}
}
} else {
class_alias(PhpStanTokenIterator::class, '\phpDocumentor\Reflection\DocBlock\Tags\Factory\TokenIterator');
}
71 changes: 70 additions & 1 deletion tests/integration/InterpretingDocBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ public function testRegressionWordpressDocblocks(): void
false,
new Description(
'{' . "\n" .
'Optional. Array or string of arguments for installing a package. Default empty array.' . "\n" .
"\n" .
' Optional. Array or string of arguments for installing a package. Default empty array.' . "\n" .
"\n" .
' @type string $source Required path to the package source. Default empty.' . "\n" .
' @type string $destination Required path to a folder to install the package in.' . "\n" .
Expand All @@ -364,4 +365,72 @@ public function testRegressionWordpressDocblocks(): void
$docblock
);
}

public function testIndentationIsKept(): void
{
$docComment = <<<DOC
/**
* Registers the script module if no script module with that script module
* identifier has already been registered.
*
* @since 6.5.0
*
* @param array \$deps {
* Optional. List of dependencies.
*
* @type string|array ...$0 {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
*
* @type string \$id The script module identifier.
* @type string \$import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
*/
DOC;

$factory = DocBlockFactory::createInstance();
$docblock = $factory->create($docComment);

self::assertEquals(
new DocBlock(
'Registers the script module if no script module with that script module
identifier has already been registered.',
new Description(
''
),
[
new Since('6.5.0', new Description('')),
new Param(
'deps',
new Array_(new Mixed_()),
false,
new Description("{

Optional. List of dependencies.

@type string|array ...$0 {
An array of script module identifiers of the dependencies of this script
module. The dependencies can be strings or arrays. If they are arrays,
they need an `id` key with the script module identifier, and can contain
an `import` key with either `static` or `dynamic`. By default,
dependencies that don't contain an `import` key are considered static.

@type string \$id The script module identifier.
@type string \$import Optional. Import type. May be either `static` or
`dynamic`. Defaults to `static`.
}
}"
)
),
],
new Context('\\')
),
$docblock
);
}
}