Skip to content

Commit

Permalink
Allow support for more complex array annotations (#344)
Browse files Browse the repository at this point in the history
* Test that all different array annotations work.

* Adding more complex type

* Fix up template variable parsing.

* Fix tests.

* Fix tests.
  • Loading branch information
dereuromark authored Feb 14, 2024
1 parent 11fd9be commit 5d7912b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Annotator/AbstractAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,12 @@ protected function parseExistingAnnotations(File $file, int $closeTagIndex, arra
$typeString = $this->renderUnionTypes($returnTypes);

$tag = $tokens[$i]['content'];
$content = mb_substr($content, mb_strlen($typeString) + 1);
$variablePos = strpos($content, ' $');
if ($tag === VariableAnnotation::TAG && $variablePos) {
$content = mb_substr($content, $variablePos + 1);
} else {
$content = mb_substr($content, mb_strlen($typeString) + 1);
}

$annotation = AnnotationFactory::createOrFail($tag, $typeString, $content, $classNameIndex);
if ($this->getConfig(static::CONFIG_REMOVE) && $tag === VariableAnnotation::TAG && $this->varInUse($tokens, $closeTagIndex, $content)) {
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Annotator/TemplateAnnotatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,33 @@ public function testAnnotateWithFollowingInline() {
$this->assertTextContains(' -> 1 annotation added.', $output);
}

/**
* Tests that a docblock with arrays in different types, e.g. shape.
*
* @return void
*/
public function testAnnotateWithShapedArray() {
$annotator = $this->_getAnnotatorMock([]);

$expectedContent = str_replace("\r\n", "\n", file_get_contents(TEST_FILES . 'templates/array.php'));
$callback = function($value) use ($expectedContent) {
$value = str_replace(["\r\n", "\r"], "\n", $value);
if ($value !== $expectedContent) {
$this->_displayDiff($expectedContent, $value);
}

return $value === $expectedContent;
};
$annotator->expects($this->once())->method('storeFile')->with($this->anything(), $this->callback($callback));

$path = TEST_ROOT . 'templates/Foos/array.php';
$annotator->annotate($path);

$output = $this->out->output();

$this->assertTextContains(' -> 1 annotation added.', $output);
}

/**
* @param array $params
* @return \IdeHelper\Annotator\TemplateAnnotator|\PHPUnit\Framework\MockObject\MockObject
Expand Down
22 changes: 22 additions & 0 deletions tests/test_app/templates/Foos/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @var \TestApp\View\AppView $this
* @var array $x
* @var array<int> $ints
* @var array{a: int, b: string|null}|null $shaped
*/
foreach ($x as $y) {
echo $y;
}
foreach ($foo as $int) {
echo $int;
}
?>
<div>
<?php foreach ($ints as $int) {
echo $int;
} ?>
<?php foreach ($shaped as $x) {
echo h($x);
} ?>
</div>
23 changes: 23 additions & 0 deletions tests/test_files/templates/array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @var \TestApp\View\AppView $this
* @var array $x
* @var array<int> $ints
* @var array{a: int, b: string|null}|null $shaped
* @var mixed $foo
*/
foreach ($x as $y) {
echo $y;
}
foreach ($foo as $int) {
echo $int;
}
?>
<div>
<?php foreach ($ints as $int) {
echo $int;
} ?>
<?php foreach ($shaped as $x) {
echo h($x);
} ?>
</div>

0 comments on commit 5d7912b

Please sign in to comment.