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