‪TYPO3CMS  ‪main
testClassFinalChecker.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 PhpParser\PhpVersion;
10 use Symfony\Component\Console\Output\ConsoleOutput;
11 
12 require_once __DIR__ . '/../../vendor/autoload.php';
13 
17 class NodeVisitor extends NodeVisitorAbstract
18 {
19  public array ‪$matches = [];
20 
21  public function ‪enterNode(Node $node): void
22  {
23  if (($node instanceof Node\Stmt\Class_) && !$node->isFinal() && !$node->isAnonymous() && !$node->isAbstract()) {
24  $this->matches[$node->getLine()] = $node->name;
25  }
26  }
27 }
28 
29 ‪$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2));
30 
31 ‪$finder = new Symfony\Component\Finder\Finder();
32 ‪$finder->files()
33  ->in([
34  __DIR__ . '/../../typo3/sysext/*/Tests/Unit/',
35  __DIR__ . '/../../typo3/sysext/*/Tests/UnitDeprecated/',
36  __DIR__ . '/../../typo3/sysext/*/Tests/Functional/',
37  __DIR__ . '/../../typo3/sysext/*/Tests/FunctionalDeprecated/',
38  ])
39  ->name('/Test\.php$/');
40 
41 ‪$output = new ConsoleOutput();
42 
44 foreach (‪$finder as $file) {
45  try {
46  $ast = ‪$parser->parse($file->getContents());
47  } catch (Error $error) {
48  ‪$output->writeln('<error>Parse error: ' . $error->getMessage() . '</error>');
49  exit(1);
50  }
51 
52  $visitor = new NodeVisitor();
53 
54  $traverser = new NodeTraverser();
55  $traverser->addVisitor($visitor);
56 
57  $ast = $traverser->traverse($ast);
58 
59  if (!empty($visitor->matches)) {
60  ‪$errors[$file->getRealPath()] = $visitor->matches;
61  ‪$output->write('<error>F</error>');
62  } else {
63  ‪$output->write('<fg=green>.</>');
64  }
65 }
66 
67 ‪$output->writeln('');
68 
69 if (!empty(‪$errors)) {
70  ‪$output->writeln('');
71 
72  foreach (‪$errors as $file => $matchesPerLine) {
73  ‪$output->writeln('');
74  ‪$output->writeln('<error>Test class should be marked as final. Found in ' . $file . '</error>');
75 
81  foreach ($matchesPerLine as $line => $methodName) {
82  ‪$output->writeln('Method:' . $methodName . ' Line:' . $line);
83  }
84  }
85  exit(1);
86 }
87 
88 exit(0);
‪$errors
‪$errors
Definition: testClassFinalChecker.php:43
‪NodeVisitor\enterNode
‪enterNode(Node $node)
Definition: testClassFinalChecker.php:21
‪$output
‪$output
Definition: testClassFinalChecker.php:41
‪$parser
‪$parser
Definition: testClassFinalChecker.php:29
‪NodeVisitor\$matches
‪array $matches
Definition: annotationChecker.php:32
‪$finder
‪$finder
Definition: testClassFinalChecker.php:31