Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: BaseConnection::getConnectDuration() number_format(): Passing null to parameter
### PHP Version

8.1

### CodeIgniter4 Version

4.1.6

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Linux

### Which server did you use?

apache

### Database

MariaDB 10.4.22

### What happened?

 number_format(): Passing null to parameter #1 ($num) of type float is deprecated

SYSTEMPATH/Database/BaseConnection.php at line 933

926      * Returns the number of seconds with microseconds that it took
927      * to connect to the database.
928      *
929      * Used by the Debug Toolbar's timeline.
930      */
931     public function getConnectDuration(int $decimals = 6): string
932     {
933         return number_format($this->connectDuration, $decimals);
934     }
935 
936     /**
937      * Protect Identifiers
938      *
939      * This function is used extensively by the Query Builder class, and by
940      * a couple functions in this class.

{PHP internal code}   —  CodeIgniter\Debug\Exceptions->errorHandler ( arguments )

SYSTEMPATH/Database/BaseConnection.php : 933   —   number_format()

SYSTEMPATH/Debug/Toolbar/Collectors/Database.php : 112   —  CodeIgniter\Database\BaseConnection->getConnectDuration () 

### Steps to Reproduce

I installed codeigniter 4.1.5 with composer in XAMPP 8.0.13 in this versión it's works, but I update XAMPP TO php 8.1.1, then, i update codeigniter via composer, and when setup my project, not working, directly throws this error

### Expected Output

Start login view for my project, before that only pre configure the database

### Anything else?

_No response_


Discussion:
@jesusferm Thank you for reporting.
I've confirmed this error when only connecting db but not running any queries.

I sent a PR to fix.
Can you check the fix of `system/Database/BaseConnection.php`?
https://github.com/codeigniter4/CodeIgniter4/pull/5536/files#diff-a0dd1b2b2b4cedd9717224db1b29e49a84991d770ef1b4a70c97721b1293e9a5
It's work.

I have a question, i modified directly file inside package was installed from composer, this modified not affected for new updates from composer?

@kenjis thanks
@jesusferm Thanks!

>  i modified directly file inside package was installed from composer, this modified not affected for new updates from composer?

Not affected. The modification would just be removed when updating CI4.




Patch:
diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php
index 5f678e5114d7..53e5dad1b351 100644
--- a/system/Database/BaseConnection.php
+++ b/system/Database/BaseConnection.php
@@ -258,14 +258,14 @@ abstract class BaseConnection implements ConnectionInterface
      *
      * @var float
      */
-    protected $connectTime;
+    protected $connectTime = 0.0;
 
     /**
      * How long it took to establish connection.
      *
      * @var float
      */
-    protected $connectDuration;
+    protected $connectDuration = 0.0;
 
     /**
      * If true, no queries will actually be


Test patch:
diff --git a/tests/system/Database/BaseConnectionTest.php b/tests/system/Database/BaseConnectionTest.php
index 7d2352a0e9b2..9bb8ddee1499 100644
--- a/tests/system/Database/BaseConnectionTest.php
+++ b/tests/system/Database/BaseConnectionTest.php
@@ -123,6 +123,16 @@ public function testStoresConnectionTimings()
         $this->assertGreaterThan(0.0, $db->getConnectDuration());
     }
 
+    /**
+     * @see https://github.com/codeigniter4/CodeIgniter4/issues/5535
+     */
+    public function testStoresConnectionTimingsNotConnected()
+    {
+        $db = new MockConnection($this->options);
+
+        $this->assertSame('0.000000', $db->getConnectDuration());
+    }
+
     public function testMagicIssetTrue()
     {
         $db = new MockConnection($this->options);