Browse Source

Replace switch() to match()

Visman 1 year ago
parent
commit
e045a4c481

+ 17 - 25
app/Models/Attachment/Attachments.php

@@ -182,31 +182,23 @@ class Attachments extends Model
                 ]);
                 ]);
             }
             }
 
 
-            switch ($this->c->DB->getType()) {
-                case 'mysql':
-                    $query = "INSERT IGNORE INTO {$table} (id, pid)
-                        VALUES (?i:id, ?i:pid)";
-
-                    break;
-                case 'sqlite':
-                case 'pgsql':
-                    $query = "INSERT INTO {$table} (id, pid)
-                        VALUES (?i:id, ?i:pid)
-                        ON CONFLICT(id, pid) DO NOTHING";
-
-                    break;
-                default:
-                    $query = "INSERT INTO {$table} (id, pid)
-                        SELECT tmp.*
-                        FROM (SELECT ?i:id AS f1, ?i:pid AS f2) AS tmp
-                        WHERE NOT EXISTS (
-                            SELECT 1
-                            FROM {$table}
-                            WHERE id=?i:id AND pid=?i:pid
-                        )";
-
-                    break;
-            }
+            $query = match ($this->c->DB->getType()) {
+                'mysql' => "INSERT IGNORE INTO {$table} (id, pid)
+                    VALUES (?i:id, ?i:pid)",
+
+                'sqlite', 'pgsql' => "INSERT INTO {$table} (id, pid)
+                    VALUES (?i:id, ?i:pid)
+                    ON CONFLICT(id, pid) DO NOTHING",
+
+                default => "INSERT INTO {$table} (id, pid)
+                    SELECT tmp.*
+                    FROM (SELECT ?i:id AS f1, ?i:pid AS f2) AS tmp
+                    WHERE NOT EXISTS (
+                        SELECT 1
+                        FROM {$table}
+                        WHERE id=?i:id AND pid=?i:pid
+                    )",
+            };
 
 
             foreach ($ids as $id) {
             foreach ($ids as $id) {
                 $vars = [
                 $vars = [

+ 8 - 20
app/Models/Forum/Forum.php

@@ -446,26 +446,14 @@ class Forum extends DataModel
      */
      */
     protected function createIdsList(int $rows = null, int $offset = null): void
     protected function createIdsList(int $rows = null, int $offset = null): void
     {
     {
-        switch ($this->sort_by) {
-            case 1:
-                $sortBy = 't.posted DESC';
-                break;
-            case 2:
-                $sortBy = 't.subject ASC';
-                break;
-            case 4:
-                $sortBy = 't.last_post ASC';
-                break;
-            case 5:
-                $sortBy = 't.posted ASC';
-                break;
-            case 6:
-                $sortBy = 't.subject DESC';
-                break;
-            default:
-                $sortBy = 't.last_post DESC';
-                break;
-        }
+        $sortBy = match ($this->sort_by) {
+            1       => 't.posted DESC',
+            2       => 't.subject',
+            4       => 't.last_post',
+            5       => 't.posted',
+            6       => 't.subject DESC',
+            default => 't.last_post DESC',
+        };
 
 
         $vars = [
         $vars = [
             ':fid'    => $this->id,
             ':fid'    => $this->id,

+ 17 - 25
app/Models/Online/Online.php

@@ -259,31 +259,23 @@ class Online extends Model
                     WHERE user_id=?i:id';
                     WHERE user_id=?i:id';
             }
             }
         } else {
         } else {
-            switch ($this->c->DB->getType()) {
-                case 'mysql':
-                    $query = 'INSERT IGNORE INTO ::online (user_id, ident, logged, o_position, o_name)
-                        VALUES (?i:id, ?s:ident, ?i:logged, ?s:pos, ?s:name)';
-
-                    break;
-                case 'sqlite':
-                case 'pgsql':
-                    $query = 'INSERT INTO ::online (user_id, ident, logged, o_position, o_name)
-                        VALUES (?i:id, ?s:ident, ?i:logged, ?s:pos, ?s:name)
-                        ON CONFLICT(user_id, ident) DO NOTHING';
-
-                    break;
-                default:
-                    $query = 'INSERT INTO ::online (user_id, ident, logged, o_position, o_name)
-                        SELECT tmp.*
-                        FROM (SELECT ?i:id AS f1, ?s:ident AS f2, ?i:logged AS f3, ?s:pos AS f4, ?s:name AS f5) AS tmp
-                        WHERE NOT EXISTS (
-                            SELECT 1
-                            FROM ::online
-                            WHERE user_id=?i:id AND ident=?s:ident
-                        )';
-
-                    break;
-            }
+            $query = match ($this->c->DB->getType()) {
+                'mysql' => 'INSERT IGNORE INTO ::online (user_id, ident, logged, o_position, o_name)
+                    VALUES (?i:id, ?s:ident, ?i:logged, ?s:pos, ?s:name)',
+
+                'sqlite', 'pgsql' => 'INSERT INTO ::online (user_id, ident, logged, o_position, o_name)
+                    VALUES (?i:id, ?s:ident, ?i:logged, ?s:pos, ?s:name)
+                    ON CONFLICT(user_id, ident) DO NOTHING',
+
+                default => 'INSERT INTO ::online (user_id, ident, logged, o_position, o_name)
+                    SELECT tmp.*
+                    FROM (SELECT ?i:id AS f1, ?s:ident AS f2, ?i:logged AS f3, ?s:pos AS f4, ?s:name AS f5) AS tmp
+                    WHERE NOT EXISTS (
+                        SELECT 1
+                        FROM ::online
+                        WHERE user_id=?i:id AND ident=?s:ident
+                    )',
+            };
         }
         }
 
 
         $this->c->DB->exec($query, $vars);
         $this->c->DB->exec($query, $vars);

+ 10 - 10
app/Models/Pages/PM/AbstractPM.php

@@ -156,11 +156,11 @@ abstract class AbstractPM extends Page
                     $name = \substr($pms->second, 1, -1);
                     $name = \substr($pms->second, 1, -1);
                 }
                 }
 
 
-                switch ($pms->area) {
-                    case Cnst::ACTION_NEW:     $m = ['New messages with %s', $name]; break;
-                    case Cnst::ACTION_CURRENT: $m = ['My talks with %s', $name]; break;
-                    case Cnst::ACTION_ARCHIVE: $m = ['Archive messages with %s', $name]; break;
-                }
+                $m = match ($pms->area) {
+                    Cnst::ACTION_NEW     => ['New messages with %s', $name],
+                    Cnst::ACTION_CURRENT => ['My talks with %s', $name],
+                    Cnst::ACTION_ARCHIVE => ['Archive messages with %s', $name],
+                };
             } else {
             } else {
                 if ($this->targetUser instanceof User) {
                 if ($this->targetUser instanceof User) {
                     $crumbs[] = [
                     $crumbs[] = [
@@ -177,11 +177,11 @@ abstract class AbstractPM extends Page
                     ];
                     ];
                 }
                 }
 
 
-                switch ($pms->area) {
-                    case Cnst::ACTION_NEW:     $m = 'New messages'; break;
-                    case Cnst::ACTION_CURRENT: $m = 'My talks'; break;
-                    case Cnst::ACTION_ARCHIVE: $m = 'Archive messages'; break;
-                }
+                $m = match ($pms->area) {
+                    Cnst::ACTION_NEW     => 'New messages',
+                    Cnst::ACTION_CURRENT => 'My talks',
+                    Cnst::ACTION_ARCHIVE => 'Archive messages',
+                };
             }
             }
 
 
             $crumbs[] = [
             $crumbs[] = [

+ 5 - 14
app/Models/Pages/RegLogTrait.php

@@ -30,20 +30,11 @@ trait RegLogTrait
 
 
         $this->c->Lang->load('admin_providers');
         $this->c->Lang->load('admin_providers');
 
 
-        switch ($type) {
-            case 'reg':
-                $message = 'Sign up with %s';
-
-                break;
-            case 'add':
-                $message = 'From %s';
-
-                break;
-            default:
-                $message = 'Sign in with %s';
-
-                break;
-        }
+        $message = match ($type) {
+            'reg'   => 'Sign up with %s',
+            'add'   => 'From %s',
+            default => 'Sign in with %s',
+        };
 
 
         $btns = [];
         $btns = [];
 
 

+ 13 - 18
app/Models/Post/Delete.php

@@ -161,24 +161,19 @@ class Delete extends Action
 
 
             $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
             $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
 
 
-            switch ($this->c->DB->getType()) {
-                case 'mysql':
-                    $query = 'DELETE p
-                        FROM ::posts AS p, ::topics AS t
-                        WHERE t.forum_id IN (?ai:forums) AND p.topic_id=t.id';
-
-                    break;
-                default:
-                    $query = 'DELETE
-                        FROM ::posts
-                        WHERE topic_id IN (
-                            SELECT id
-                            FROM ::topics
-                            WHERE forum_id IN (?ai:forums)
-                        )';
-
-                break;
-            }
+            $query = match ($this->c->DB->getType()) {
+                'mysql' => 'DELETE p
+                    FROM ::posts AS p, ::topics AS t
+                    WHERE t.forum_id IN (?ai:forums) AND p.topic_id=t.id',
+
+                default => 'DELETE
+                    FROM ::posts
+                    WHERE topic_id IN (
+                        SELECT id
+                        FROM ::topics
+                        WHERE forum_id IN (?ai:forums)
+                    )',
+            };
 
 
             $this->c->DB->exec($query, $vars);
             $this->c->DB->exec($query, $vars);
         }
         }

+ 40 - 58
app/Models/Search/Delete.php

@@ -87,76 +87,58 @@ class Delete extends Method
             $vars = [
             $vars = [
                 ':users' => $uids,
                 ':users' => $uids,
             ];
             ];
-
-            switch ($this->c->DB->getType()) {
-                case 'mysql':
-                    $query = 'DELETE sm
-                        FROM ::search_matches AS sm, ::posts AS p
-                        WHERE p.poster_id IN (?ai:users) AND sm.post_id=p.id';
-
-                    break;
-                default:
-                    $query = 'DELETE
-                        FROM ::search_matches
-                        WHERE post_id IN (
-                            SELECT p.id
-                            FROM ::posts AS p
-                            WHERE p.poster_id IN (?ai:users)
-                        )';
-
-                    break;
-            }
+            $query = match ($this->c->DB->getType()) {
+                'mysql' => 'DELETE sm
+                    FROM ::search_matches AS sm, ::posts AS p
+                    WHERE p.poster_id IN (?ai:users) AND sm.post_id=p.id',
+
+                default => 'DELETE
+                    FROM ::search_matches
+                    WHERE post_id IN (
+                        SELECT p.id
+                        FROM ::posts AS p
+                        WHERE p.poster_id IN (?ai:users)
+                    )',
+            };
         }
         }
 
 
         if ($fids) {
         if ($fids) {
             $vars = [
             $vars = [
                 ':forums' => $fids,
                 ':forums' => $fids,
             ];
             ];
-
-            switch ($this->c->DB->getType()) {
-                case 'mysql':
-                    $query = 'DELETE sm
-                        FROM ::search_matches AS sm, ::posts AS p, ::topics AS t
-                        WHERE t.forum_id IN (?ai:forums) AND p.topic_id=t.id AND sm.post_id=p.id';
-
-                    break;
-                default:
-                    $query = 'DELETE
-                        FROM ::search_matches
-                        WHERE post_id IN (
-                            SELECT p.id
-                            FROM ::posts AS p
-                            INNER JOIN ::topics AS t ON t.id=p.topic_id
-                            WHERE t.forum_id IN (?ai:forums)
-                        )';
-
-                    break;
-            }
+            $query = match ($this->c->DB->getType()) {
+                'mysql' => 'DELETE sm
+                    FROM ::search_matches AS sm, ::posts AS p, ::topics AS t
+                    WHERE t.forum_id IN (?ai:forums) AND p.topic_id=t.id AND sm.post_id=p.id',
+
+                default => 'DELETE
+                    FROM ::search_matches
+                    WHERE post_id IN (
+                        SELECT p.id
+                        FROM ::posts AS p
+                        INNER JOIN ::topics AS t ON t.id=p.topic_id
+                        WHERE t.forum_id IN (?ai:forums)
+                    )',
+            };
         }
         }
 
 
         if ($tids) {
         if ($tids) {
             $vars = [
             $vars = [
                 ':topics' => $tids,
                 ':topics' => $tids,
             ];
             ];
-
-            switch ($this->c->DB->getType()) {
-                case 'mysql':
-                    $query = 'DELETE sm
-                        FROM ::search_matches AS sm, ::posts AS p
-                        WHERE p.topic_id IN (?ai:topics) AND sm.post_id=p.id';
-
-                    break;
-                default:
-                    $query = 'DELETE
-                        FROM ::search_matches
-                        WHERE post_id IN (
-                            SELECT p.id
-                            FROM ::posts AS p
-                            WHERE p.topic_id IN (?ai:topics)
-                        )';
-
-                    break;
-            }
+            $query = match ($this->c->DB->getType()) {
+                'mysql' => 'DELETE sm
+                    FROM ::search_matches AS sm, ::posts AS p
+                    WHERE p.topic_id IN (?ai:topics) AND sm.post_id=p.id',
+
+                default => 'DELETE
+                    FROM ::search_matches
+                    WHERE post_id IN (
+                        SELECT p.id
+                        FROM ::posts AS p
+                        WHERE p.topic_id IN (?ai:topics)
+                    )',
+            };
         }
         }
 
 
         if ($pids) {
         if ($pids) {