From e52782b56f5f4a2cee4394a50724d45ecd4ec931 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Fri, 17 Mar 2023 22:47:09 +0100
Subject: [PATCH] [TASK] Use BackedEnum for LikeWildcard
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

PHP 8 introduced native Enumerations,
which can now be used instead of our own
implementation from TYPO3 v7.

Enums are effectively singletons making
it much faster than previously when
used many times.

Since LikeWildcard in indexed search is
internal and only used internally within
indexed search, the usage is rather small.

Resolves: #100213
Releases: main
Change-Id: I4d3f60c7d4b91e921271487c8688036939403805
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78162
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: core-ci <typo3@b13.com>
---
 .../Repository/IndexSearchRepository.php      |  6 ++--
 .../Classes/Utility/LikeWildcard.php          | 29 +++++++++----------
 .../Functional/Utility/LikeWildcardTest.php   |  6 +---
 3 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/typo3/sysext/indexed_search/Classes/Domain/Repository/IndexSearchRepository.php b/typo3/sysext/indexed_search/Classes/Domain/Repository/IndexSearchRepository.php
index f81a3e9dcc29..241aee91fef3 100644
--- a/typo3/sysext/indexed_search/Classes/Domain/Repository/IndexSearchRepository.php
+++ b/typo3/sysext/indexed_search/Classes/Domain/Repository/IndexSearchRepository.php
@@ -612,11 +612,9 @@ class IndexSearchRepository
      * Search for a word
      *
      * @param string $sWord the search word
-     * @param int $wildcard Bit-field of Utility\LikeWildcard
      */
-    protected function searchWord(string $sWord, int $wildcard): Result
+    protected function searchWord(string $sWord, LikeWildcard $likeWildcard): Result
     {
-        $likeWildcard = LikeWildcard::cast($wildcard);
         $wSel = $likeWildcard->getLikeQueryPart(
             'index_words',
             'IW.baseword',
@@ -649,7 +647,7 @@ class IndexSearchRepository
     protected function searchSentence(string $sWord): Result
     {
         $this->wSelClauses[] = '1=1';
-        $likeWildcard = LikeWildcard::cast(LikeWildcard::BOTH);
+        $likeWildcard = LikeWildcard::BOTH;
         $likePart = $likeWildcard->getLikeQueryPart(
             'index_fulltext',
             'IFT.fulltextdata',
diff --git a/typo3/sysext/indexed_search/Classes/Utility/LikeWildcard.php b/typo3/sysext/indexed_search/Classes/Utility/LikeWildcard.php
index 84d5249dd079..34f186d15cbb 100644
--- a/typo3/sysext/indexed_search/Classes/Utility/LikeWildcard.php
+++ b/typo3/sysext/indexed_search/Classes/Utility/LikeWildcard.php
@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of the TYPO3 CMS project.
  *
@@ -16,28 +18,25 @@
 namespace TYPO3\CMS\IndexedSearch\Utility;
 
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Type\Enumeration;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Enumeration object for LikeWildcard
  * @internal
  */
-final class LikeWildcard extends Enumeration
+enum LikeWildcard: int
 {
-    public const __default = self::BOTH;
-
-    /** @var int Do not use any wildcard */
-    public const NONE = 0;
+    /** Do not use any wildcard */
+    case NONE = 0;
 
-    /** @var int Use wildcard on left side */
-    public const LEFT = 1;
+    /** Use wildcard on left side */
+    case LEFT = 1;
 
-    /** @var int Use wildcard on right side */
-    public const RIGHT = 2;
+    /** Use wildcard on right side */
+    case RIGHT = 2;
 
-    /** @var int Use wildcard on both sides */
-    public const BOTH = 3;
+    /** Use wildcard on both sides */
+    case BOTH = 3;
 
     /**
      * Returns a LIKE clause for sql queries.
@@ -47,14 +46,14 @@ final class LikeWildcard extends Enumeration
      * @param string $likeValue The value for the LIKE clause operation.
      * @return string
      */
-    public function getLikeQueryPart($tableName, $fieldName, $likeValue)
+    public function getLikeQueryPart(string $tableName, string $fieldName, string $likeValue): string
     {
         $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
             ->getQueryBuilderForTable($tableName);
 
-        $string = ($this->value & self::LEFT ? '%' : '')
+        $string = ($this->value & self::LEFT->value ? '%' : '')
             . $queryBuilder->escapeLikeWildcards($likeValue)
-            . ($this->value & self::RIGHT ? '%' : '');
+            . ($this->value & self::RIGHT->value ? '%' : '');
 
         return $queryBuilder->expr()->like($fieldName, $queryBuilder->quote($string));
     }
diff --git a/typo3/sysext/indexed_search/Tests/Functional/Utility/LikeWildcardTest.php b/typo3/sysext/indexed_search/Tests/Functional/Utility/LikeWildcardTest.php
index 3ef9ed7cd18f..15f1b9ac03f1 100644
--- a/typo3/sysext/indexed_search/Tests/Functional/Utility/LikeWildcardTest.php
+++ b/typo3/sysext/indexed_search/Tests/Functional/Utility/LikeWildcardTest.php
@@ -24,19 +24,15 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\IndexedSearch\Utility\LikeWildcard;
 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
 
-/**
- * This class contains unit tests for the LikeQueryUtility
- */
 class LikeWildcardTest extends FunctionalTestCase
 {
     /**
      * @test
      * @dataProvider getLikeQueryPartDataProvider
      */
-    public function getLikeQueryPart(string $tableName, string $fieldName, string $likeValue, int $wildcard, string $expected): void
+    public function getLikeQueryPart(string $tableName, string $fieldName, string $likeValue, LikeWildcard $subject, string $expected): void
     {
         $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
-        $subject = LikeWildcard::cast($wildcard);
         if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
             $expected = str_replace('LIKE', 'ILIKE', $expected);
         }
-- 
GitLab