Browse Source

Fix dynamic property on PHP 8.2

Emit a deprecation for users on PHP versions that are lower that this
behavior will be going away. I'm also ok with just silently not adding
that property in PHP 8.2

Fixes #16963
Mark Story 3 years ago
parent
commit
f376b31ce7

+ 8 - 2
src/Database/Log/LoggingStatement.php

@@ -75,8 +75,14 @@ class LoggingStatement extends StatementDecorator
             $result = parent::execute($params);
             $this->loggedQuery->took = (int)round((microtime(true) - $this->startTime) * 1000, 0);
         } catch (Exception $e) {
-            /** @psalm-suppress UndefinedPropertyAssignment */
-            $e->queryString = $this->queryString;
+            if (version_compare(PHP_VERSION, '8.2.0', '<')) {
+                deprecationWarning(
+                    '4.4.12 - Having queryString set on exceptions is deprecated.' .
+                    'If you are not using this attribute there is no action to take.'
+                );
+                /** @psalm-suppress UndefinedPropertyAssignment */
+                $e->queryString = $this->queryString;
+            }
             $this->loggedQuery->error = $e;
             $this->_log();
             throw $e;

+ 8 - 6
tests/TestCase/Database/Log/LoggingStatementTest.php

@@ -155,12 +155,14 @@ class LoggingStatementTest extends TestCase
             ->method('__get')
             ->willReturn('SELECT bar FROM foo');
         $st->setLogger(new QueryLogger(['connection' => 'test']));
-        try {
-            $st->execute();
-        } catch (MyPDOException $e) {
-            $this->assertSame('This is bad', $e->getMessage());
-            $this->assertSame($st->queryString, $e->queryString);
-        }
+        $this->deprecated(function () use ($st) {
+            try {
+                $st->execute();
+            } catch (MyPDOException $e) {
+                $this->assertSame('This is bad', $e->getMessage());
+                $this->assertSame($st->queryString, $e->queryString);
+            }
+        });
 
         $messages = Log::engine('queries')->read();
         $this->assertCount(1, $messages);