From 5942e47ec4ae147fb15dfc1173743d79180080b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Wed, 13 Jul 2022 00:37:53 +0200
Subject: [PATCH] [BUGFIX] Enhance annotation integrity test script
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Docblock annotations with more then one space between a wildcard
character `*` and the annotation start character `@` was not found.
Additionally, annotation identifiers did not regonize hyphens as
valid identifier character, thus cutting of e.g. phpstan annoations.

This patch now properly finds annotions with multi-space separations
and annotation identifiers containing hyphens. Additionally, two
phpstan specific annotations are allowed.

> https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases

Summarized contained tasks:

> find multi-space separated annotations
  e.g. `*  @<AnnotationIdentifier>`
> properly extract full annotation identifier containing hyphens
  e.g. `* @annoation-identifier-with-hyphen`
> add `@phpstan-type` and `@phpstan-import-type` as allowed
  annotation, but only on class level, and not on class-property
  or class-method level

Resolves: #97917
Resolves: #97918
Resolves: #97919
Releases: main, 11.5, 10.4
Change-Id: Iab69ffd87d407088f1237168e19b67a8dc846c8f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75138
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
---
 Build/Scripts/annotationChecker.php               | 15 ++++++++++++---
 .../DatabaseCheck/Platform/SqlSrv.php             | 12 ++++++------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/Build/Scripts/annotationChecker.php b/Build/Scripts/annotationChecker.php
index 4d2f8a7b4737..12d6d6d3f54a 100755
--- a/Build/Scripts/annotationChecker.php
+++ b/Build/Scripts/annotationChecker.php
@@ -46,7 +46,7 @@ class NodeVisitor extends NodeVisitorAbstract
                     return;
                 }
 
-                // These annotations are OK to have, everything else is denied
+                // These annotations are OK to have on class, class property and class method level, everything else is denied
                 $negativeLookaheadMatches = [
                     // Annotation tags
                     'Annotation', 'Attribute', 'Attributes', 'Required', 'Target',
@@ -76,14 +76,23 @@ class NodeVisitor extends NodeVisitorAbstract
                     // static code analysis
                     'template', 'implements', 'extends'
                 ];
+                // allow annotation only on class level
+                if (get_class($node) === Node\Stmt\Class_::class) {
+                    $negativeLookaheadMatches = array_merge(
+                        $negativeLookaheadMatches,
+                        [
+                            // PHPStan
+                            'phpstan-type', 'phpstan-import-type',
+                        ]
+                    );
+                }
 
                 $matches = [];
                 preg_match_all(
-                    '/\*\s@(?!' . implode('|', $negativeLookaheadMatches) . ')(?<annotations>[a-zA-Z0-9\\\\]+)/',
+                    '/\*(\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;
diff --git a/typo3/sysext/install/Classes/SystemEnvironment/DatabaseCheck/Platform/SqlSrv.php b/typo3/sysext/install/Classes/SystemEnvironment/DatabaseCheck/Platform/SqlSrv.php
index 3f0e971e01ea..6cc0de84f6b9 100644
--- a/typo3/sysext/install/Classes/SystemEnvironment/DatabaseCheck/Platform/SqlSrv.php
+++ b/typo3/sysext/install/Classes/SystemEnvironment/DatabaseCheck/Platform/SqlSrv.php
@@ -155,14 +155,14 @@ class SqlSrv extends AbstractPlatform
      * Examples:
      *
      * valid:
-     *      _foo
-     *      @foo
-     *      #foo
-     *      _floo1äea
-     *      @foo111111111kemcie_l#@
+     * >    _foo
+     * >    @foo
+     * >    #foo
+     * >    _floo1äea
+     * >    @foo111111111kemcie_l#@
      *
      * not valid:
-     *      @@thisShouldNotBeValid
+     * >    @@thisShouldNotBeValid
      *
      *
      * @param string $databaseName
-- 
GitLab