/
annotationChecker.php
executable file
·156 lines (134 loc) · 5.56 KB
/
annotationChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env php
<?php
declare(strict_types = 1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use PhpParser\Comment\Doc;
use PhpParser\Error;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use Symfony\Component\Console\Output\ConsoleOutput;
require_once __DIR__ . '/../../vendor/autoload.php';
/**
* Class NodeVisitor
*/
class NodeVisitor extends NodeVisitorAbstract
{
/**
* @var array
*/
public $matches = [];
public function enterNode(Node $node)
{
switch (get_class($node)) {
case Node\Stmt\Class_::class:
case Node\Stmt\Property::class:
case Node\Stmt\ClassMethod::class:
/** Node\Stmt\ClassMethod $node */
if (!($docComment = $node->getDocComment()) instanceof Doc) {
return;
}
// These annotations are OK to have, everything else is denied
$negativeLookaheadMatches = [
// Annotation tags
'Annotation', 'Attribute', 'Attributes', 'Required', 'Target',
// Widely used tags (but not existent in phpdoc)
'fix', 'fixme', 'override',
// PHPDocumentor 1 tags
'abstract', 'access', 'code', 'deprec', 'endcode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc', 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar', 'staticVar', 'throw',
// PHPDocumentor 2 tags
'api', 'author', 'category', 'copyright', 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal', 'license', 'link', 'method', 'package', 'param', 'property', 'property-read', 'property-write', 'return', 'see', 'since', 'source', 'subpackage', 'throws', 'todo', 'TODO', 'usedby', 'uses', 'var', 'version',
// PHPUnit tags
'codeCoverageIgnore', 'codeCoverageIgnoreStart', 'codeCoverageIgnoreEnd', 'test', 'covers', 'dataProvider', 'group', 'skip', 'depends', 'expectedException', 'before',
// codeception tags
'env',
// PHPCheckStyle
'SuppressWarnings', 'noinspection',
// Extbase related (deprecated)
'transient', 'cli', 'flushesCaches',
// Extbase related
'TYPO3\\\\CMS\\\\Extbase\\\\Annotation\\\\Inject', 'Extbase\\\\Inject', 'Inject',
'TYPO3\\\\CMS\\\\Extbase\\\\Annotation\\\\Validate', 'Extbase\\\\Validate', 'Validate',
'Transient', 'Extbase\\\\ORM\\\\Lazy', 'IgnoreValidation', 'Enum',
'TYPO3\\\\CMS\\\\Extbase\\\\Annotation\\\\ORM\\\\Cascade', 'Extbase\\\\ORM\\\\Cascade', 'Cascade',
// Extension scanner
'extensionScannerIgnoreFile', 'extensionScannerIgnoreLine'
];
$matches = [];
preg_match_all(
'/\*\s@(?!' . implode('|', $negativeLookaheadMatches) . ')(?<annotations>[a-zA-Z0-9\\\\]+)/',
$docComment->getText(),
$matches
);
if (!empty($matches['annotations'])) {
$this->matches[$node->getLine()] = array_map(function ($value) {
return '@' . $value;
}, $matches['annotations']);
}
break;
default:
break;
}
}
}
$parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7);
$finder = new Symfony\Component\Finder\Finder();
$finder->files()
->in(__DIR__ . '/../../typo3/')
->name('/\.php$/')
// black list some deprecated unit test fixture files that test old deprecations
->notPath('/.*sysext\/extbase\/Tests\/UnitDeprecated\/Reflection\/Fixture/')
// black list some unit test fixture files from extension scanner that test matchers of old annotations
->notName('MethodAnnotationMatcherFixture.php')
->notName('PropertyAnnotationMatcherFixture.php')
;
$output = new ConsoleOutput();
$errors = [];
foreach ($finder as $file) {
try {
$ast = $parser->parse($file->getContents());
} catch (Error $error) {
$output->writeln('<error>Parse error: ' . $error->getMessage() . '</error>');
exit(1);
}
$visitor = new NodeVisitor();
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
$ast = $traverser->traverse($ast);
if (!empty($visitor->matches)) {
$errors[$file->getRealPath()] = $visitor->matches;
$output->write('<error>F</error>');
} else {
$output->write('<fg=green>.</>');
}
}
$output->writeln('');
if (!empty($errors)) {
$output->writeln('');
foreach ($errors as $file => $matchesPerLine) {
$output->writeln('');
$output->writeln('<error>' . $file . '</error>');
/**
* @var array $matchesPerLine
* @var int $line
* @var array $matches
*/
foreach ($matchesPerLine as $line => $matches) {
$output->writeln($line . ': ' . implode(', ', $matches));
}
}
exit(1);
}
exit(0);