Skip to content

Commit

Permalink
Fix delare / strict type issue (#270)
Browse files Browse the repository at this point in the history
* Test case for strict type issue

* Add fix for declare strict
  • Loading branch information
dereuromark authored Feb 10, 2022
1 parent b30bb22 commit f02bdb3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Annotator/TemplateAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ protected function annotateContent(string $path, string $content, array $annotat
}

$needsPhpTag = $phpOpenTagIndex === null || $this->needsPhpTag($file, $phpOpenTagIndex);

$phpOpenTagIndex = $this->checkforDeclareStatement($file, $phpOpenTagIndex);

$docBlockCloseTagIndex = null;
if ($needsPhpTag) {
$phpOpenTagIndex = null;
} else {
}
if ($phpOpenTagIndex !== null) {
$docBlockCloseTagIndex = $this->findExistingDocBlock($file, $phpOpenTagIndex);
}

Expand Down Expand Up @@ -179,6 +183,10 @@ protected function needsPhpTag(File $file, int $phpOpenTagIndex): bool {
}

$nextIndex = $file->findNext(T_WHITESPACE, $phpOpenTagIndex + 1, null, true);
if ($tokens[$nextIndex]['code'] === T_DECLARE) {
return false;
}

if ($tokens[$nextIndex]['line'] === $tokens[$phpOpenTagIndex]['line']) {
return true;
}
Expand Down Expand Up @@ -504,4 +512,30 @@ protected function getVariableAnnotation(array $variable) {
return $annotation;
}

/**
* @param \PHP_CodeSniffer\Files\File $file
* @param int|null $phpOpenTagIndex
*
* @return int|null
*/
protected function checkforDeclareStatement(File $file, ?int $phpOpenTagIndex): ?int {
if ($phpOpenTagIndex === null) {
return $phpOpenTagIndex;
}

$nextIndex = $file->findNext(T_DECLARE, $phpOpenTagIndex, $phpOpenTagIndex + 2);
if (!$nextIndex) {
return $phpOpenTagIndex;
}

$tokens = $file->getTokens();

$lastIndexOfRow = $tokens[$nextIndex]['parenthesis_closer'];
while (!empty($tokens[$lastIndexOfRow + 1]) && $tokens[$lastIndexOfRow + 1]['line'] === $tokens[$lastIndexOfRow]['line']) {
$lastIndexOfRow++;
}

return $lastIndexOfRow;
}

}
27 changes: 27 additions & 0 deletions tests/TestCase/Annotator/TemplateAnnotatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,33 @@ public function testAnnotateExistingOutdated() {
$this->assertTextContains(' -> 2 annotations updated, 1 annotation removed, 1 annotation skipped.', $output);
}

/**
* Tests merging with existing PHP tag and doc block - PHP strict_types mode.
*
* @return void
*/
public function testAnnotateExistingStrict() {
$annotator = $this->_getAnnotatorMock([]);

$expectedContent = str_replace("\r\n", "\n", file_get_contents(TEST_FILES . 'templates/existing_strict.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/existing_strict.php';
$annotator->annotate($path);

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

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

/**
* Tests with empty template
*
Expand Down
12 changes: 12 additions & 0 deletions tests/test_app/templates/Foos/existing_strict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);
/**
* @license MIT
*/

/**
* @var \TestApp\View\AppView $this
*/
?>
<div>
<?php echo h($wheel->id); ?>
</div>
13 changes: 13 additions & 0 deletions tests/test_files/templates/existing_strict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);
/**
* @license MIT
*/

/**
* @var \TestApp\View\AppView $this
* @var \TestApp\Model\Entity\Wheel $wheel
*/
?>
<div>
<?php echo h($wheel->id); ?>
</div>

0 comments on commit f02bdb3

Please sign in to comment.