‪TYPO3CMS  ‪main
testMethodPrefixChecker.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 use PhpParser\Node;
6 use PhpParser\NodeTraverser;
7 use PhpParser\NodeVisitorAbstract;
8 use PhpParser\ParserFactory;
9 use Symfony\Component\Console\Output\ConsoleOutput;
10 
11 require_once __DIR__ . '/../../vendor/autoload.php';
12 
21 class NodeVisitor extends NodeVisitorAbstract
22 {
23  public array ‪$matches = [];
24 
25  public function ‪enterNode(Node $node): void
26  {
27  if (($node instanceof Node\Stmt\ClassMethod) && str_starts_with($node->name->name, 'test')) {
28  $this->matches[$node->getLine()] = $node->name->name;
29  }
30  }
31 }
32 
33 ‪$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
34 
35 ‪$finder = new Symfony\Component\Finder\Finder();
36 ‪$finder->files()
37  ->in([
38  __DIR__ . '/../../typo3/sysext/*/Tests/Unit/',
39  __DIR__ . '/../../typo3/sysext/*/Tests/UnitDeprecated/',
40  __DIR__ . '/../../typo3/sysext/*/Tests/Functional/',
41  ])
42  ->name('/Test\.php$/');
43 
44 ‪$output = new ConsoleOutput();
45 
46 ‪$errors = [];
47 foreach (‪$finder as $file) {
48  try {
49  $ast = ‪$parser->parse($file->getContents());
50  } catch (Error $error) {
51  ‪$output->writeln('<error>Parse error: ' . $error->getMessage() . '</error>');
52  exit(1);
53  }
54 
55  $visitor = new NodeVisitor();
56 
57  $traverser = new NodeTraverser();
58  $traverser->addVisitor($visitor);
59 
60  $ast = $traverser->traverse($ast);
61 
62  if (!empty($visitor->matches)) {
63  ‪$errors[$file->getRealPath()] = $visitor->matches;
64  ‪$output->write('<error>F</error>');
65  } else {
66  ‪$output->write('<fg=green>.</>');
67  }
68 }
69 
70 ‪$output->writeln('');
71 
72 if (!empty(‪$errors)) {
73  ‪$output->writeln('');
74 
75  foreach (‪$errors as $file => $matchesPerLine) {
76  ‪$output->writeln('');
77  ‪$output->writeln('<error>Method shoud not start "test". Use @test phpDoc annotation instead. Found in ' . $file . '</error>');
78 
84  foreach ($matchesPerLine as $line => $methodName) {
85  ‪$output->writeln('Method:' . $methodName . ' Line:' . $line);
86  }
87  }
88  exit(1);
89 }
90 
91 exit(0);
‪NodeVisitor\enterNode
‪enterNode(Node $node)
Definition: testMethodPrefixChecker.php:25
‪$finder
‪$finder
Definition: testMethodPrefixChecker.php:35
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪$output
‪$output
Definition: annotationChecker.php:119
‪NodeVisitor\$matches
‪array $matches
Definition: annotationChecker.php:33
‪$parser
‪$parser
Definition: testMethodPrefixChecker.php:33