Skip to content

Commit 44cd0f3

Browse files
committed
Allow skipping of class annotations.
1 parent 97c2972 commit 44cd0f3

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

docs/Annotations.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ verify the changes. This feature is still in a very alpha phase.
334334
Note that you can prevent removal (just as updating) by adding a comment to your annotation.
335335
That will skip any attempt to remove it.
336336

337+
## Skipping annotations for a class
338+
Sometimes you are extending another class, in that case you can use `@inherit` tag as class doc block to skip annotating here.
339+
340+
In this case this table extend the Images table, but use the same `protected $_entityClass = Image::class;`, so skipping:
341+
```php
342+
/**
343+
* @inheritdoc
344+
*/
345+
class CustomImagesTable extends ImagesTable ...
346+
```
347+
337348
## Continuous integration support
338349
The tool can also be run like the coding standards check in your CI.
339350
This way no annotation can be forgotten, when making PRs for your project.

src/Annotator/AbstractAnnotator.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ protected function _annotate($path, $content, array $annotations) {
225225

226226
$closeTagIndex = $file->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $classIndex - 1, $prevCode);
227227
$this->_resetCounter();
228+
if ($closeTagIndex && $this->shouldSkip($file, $closeTagIndex)) {
229+
return false;
230+
}
228231
if ($closeTagIndex && !$this->isInlineDocBlock($file, $closeTagIndex)) {
229232
$newContent = $this->_appendToExistingDocBlock($file, $closeTagIndex, $annotations);
230233
} else {
@@ -550,6 +553,27 @@ protected function isInlineDocBlock(File $file, $docBlockCloseIndex) {
550553
return $tokens[$docBlockCloseIndex]['line'] === $tokens[$docBlockOpenIndex]['line'];
551554
}
552555

556+
/**
557+
* @param \PHP_CodeSniffer\Files\File $file
558+
* @param int $docBlockCloseIndex
559+
* @return bool
560+
*/
561+
protected function shouldSkip(File $file, $docBlockCloseIndex) {
562+
$tokens = $file->getTokens();
563+
$docBlockOpenIndex = $tokens[$docBlockCloseIndex]['comment_opener'];
564+
565+
for ($i = $docBlockOpenIndex + 1; $i < $docBlockCloseIndex; $i++) {
566+
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
567+
continue;
568+
}
569+
if ($tokens[$i]['content'] === '@inheritdoc') {
570+
return true;
571+
}
572+
}
573+
574+
return false;
575+
}
576+
553577
/**
554578
* @param array $usedModels
555579
* @param string $content

tests/TestCase/Annotator/ModelAnnotatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ public function testAnnotateExistingReplace() {
167167
$this->assertTextContains(' -> 1 annotation updated', $output);
168168
}
169169

170+
/**
171+
* @return void
172+
*/
173+
public function testAnnotateSkip() {
174+
$annotator = $this->_getAnnotatorMock([]);
175+
176+
$expectedContent = str_replace("\r\n", "\n", file_get_contents(TEST_FILES . 'Model/Table/SkipMeTable.php'));
177+
$callback = function($value) use ($expectedContent) {
178+
$value = str_replace(["\r\n", "\r"], "\n", $value);
179+
if ($value !== $expectedContent) {
180+
$this->_displayDiff($expectedContent, $value);
181+
}
182+
return $value === $expectedContent;
183+
};
184+
$annotator->expects($this->never())->method('_storeFile')->with($this->anything(), $this->callback($callback));
185+
186+
$path = APP . 'Model/Table/SkipMeTable.php.php';
187+
$annotator->annotate($path);
188+
189+
$output = (string)$this->out->output();
190+
$this->assertSame('', $output);
191+
}
192+
170193
/**
171194
* @return void
172195
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace App\Model\Table;
3+
4+
use Cake\ORM\Table;
5+
6+
/**
7+
* @inheritDoc
8+
*/
9+
class SkipMeTable extends Table {
10+
11+
/**
12+
* @param array $config
13+
* @return void
14+
*/
15+
public function initialize(array $config) {
16+
parent::initialize($config);
17+
18+
$this->table('wheels');
19+
$this->belongsTo('Cars');
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace App\Model\Table;
3+
4+
use Cake\ORM\Table;
5+
6+
/**
7+
* @inheritDoc
8+
*/
9+
class SkipMeTable extends Table {
10+
11+
/**
12+
* @param array $config
13+
* @return void
14+
*/
15+
public function initialize(array $config) {
16+
parent::initialize($config);
17+
18+
$this->table('wheels');
19+
$this->belongsTo('Cars');
20+
}
21+
22+
}

0 commit comments

Comments
 (0)