Skip to content

Commit 6c5938e

Browse files
committed
fix: log the vendor spl_object_hash() deprecation on PHP 8.6 instead of throwing
1 parent de59b12 commit 6c5938e

3 files changed

Lines changed: 67 additions & 18 deletions

File tree

‎docs/changelogs/v4.8.0.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ Release Date: Unreleased
375375
## Changes
376376

377377
- **Config:** Added the `md` key for `Config\Mimes::$mimes` for Markdown files.
378+
- **Debug:** The PHP 8.4 `session.sid_length` and `session.sid_bits_per_character` deprecations are now logged at `Config\Exceptions::$deprecationLogLevel` with a backtrace, instead of at the `warning` level without one.
378379
- **Router:** Reduced route registration overhead in `RouteCollection::create()` by skipping placeholder replacement for route keys without `:` and calling `helper('array')` only once per collection.
379380

380381
## Deprecations
@@ -394,6 +395,7 @@ Release Date: Unreleased
394395
## Bugs Fixed
395396

396397
- **Config:** Fixed a bug where instantiating an anonymous `BaseConfig` subclass threw a `ValueError` from `getenv()` on PHP 8.6.
398+
- **Debug:** Fixed a bug where the `spl_object_hash()` deprecation raised by `mikey179/vfsstream` and `kint-php/kint` on PHP 8.6 was thrown as an exception. It is now logged as a deprecation.
397399
- **Testing:** Fixed a bug where `ReflectionHelper::getPrivateMethodInvoker()` passed an object when invoking a static method, which is deprecated in PHP 8.6.
398400

399401
See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.

‎system/Debug/Exceptions.php‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use CodeIgniter\HTTP\ResponseInterface;
2222
use Config\Exceptions as ExceptionsConfig;
2323
use ErrorException;
24-
use Psr\Log\LogLevel;
2524
use Throwable;
2625

2726
/**
@@ -141,8 +140,8 @@ public function resetForWorkerMode(): void
141140
public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null)
142141
{
143142
if ($this->isDeprecationError($severity)) {
144-
if ($this->isSessionSidDeprecationError($message, $file, $line)) {
145-
return true;
143+
if ($this->isSessionSidDeprecationError($message) || $this->isSplObjectHashDeprecationError($message, $file)) {
144+
return $this->handleDeprecationError($message, $file, $line);
146145
}
147146

148147
if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) {
@@ -188,25 +187,28 @@ public function shutdownHandler()
188187
}
189188

190189
/**
191-
* Handles session.sid_length and session.sid_bits_per_character deprecations in PHP 8.4.
190+
* Detects the `session.sid_length` and `session.sid_bits_per_character` deprecations in PHP 8.4.
192191
*/
193-
private function isSessionSidDeprecationError(string $message, ?string $file = null, ?int $line = null): bool
192+
private function isSessionSidDeprecationError(string $message): bool
194193
{
195-
if (PHP_VERSION_ID >= 80400 && str_contains($message, 'session.sid_')) {
196-
log_message(
197-
LogLevel::WARNING,
198-
'[DEPRECATED] {message} in {errFile} on line {errLine}.',
199-
[
200-
'message' => $message,
201-
'errFile' => clean_path($file ?? ''),
202-
'errLine' => $line ?? 0,
203-
],
204-
);
205-
206-
return true;
194+
return PHP_VERSION_ID >= 80400 && str_contains($message, 'session.sid_');
195+
}
196+
197+
/**
198+
* Detects the `spl_object_hash()` deprecation in PHP 8.6 raised by `mikey179/vfsstream` and `kint-php/kint`.
199+
*/
200+
private function isSplObjectHashDeprecationError(string $message, ?string $file = null): bool
201+
{
202+
if (! str_contains($message, 'spl_object_hash()')) {
203+
return false;
207204
}
208205

209-
return false;
206+
$file = str_replace('\\', '/', $file ?? '');
207+
208+
return str_contains($file, '/mikey179/vfsstream/')
209+
// @todo 4.8.0 Remove these checks once Kint v7 is released
210+
|| str_contains($file, '/kint-php/kint/')
211+
|| str_contains($file, '/system/ThirdParty/Kint/');
210212
}
211213

212214
/**

‎tests/system/Debug/ExceptionsTest.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Config\Exceptions as ExceptionsConfig;
2222
use ErrorException;
2323
use Override;
24+
use PHPUnit\Framework\Attributes\DataProvider;
2425
use PHPUnit\Framework\Attributes\Group;
2526

2627
/**
@@ -78,6 +79,50 @@ public function testDeprecationsOnPhp81DoNotThrow(): void
7879
}
7980
}
8081

82+
#[DataProvider('provideVendorSplObjectHashDeprecationDoesNotThrow')]
83+
public function testVendorSplObjectHashDeprecationDoesNotThrow(string $file): void
84+
{
85+
$config = new ExceptionsConfig();
86+
87+
$config->logDeprecations = false;
88+
$config->deprecationLogLevel = 'error';
89+
90+
$this->exception = new Exceptions($config);
91+
92+
$message = 'Function spl_object_hash() is deprecated since 8.6, consider using spl_object_id() instead';
93+
94+
$this->assertTrue($this->exception->errorHandler(E_DEPRECATED, $message, $file, 374));
95+
$this->assertLogContains('error', '[DEPRECATED] ' . $message);
96+
}
97+
98+
/**
99+
* @return iterable<string, array{string}>
100+
*/
101+
public static function provideVendorSplObjectHashDeprecationDoesNotThrow(): iterable
102+
{
103+
yield 'vfsStream' => [VENDORPATH . 'mikey179/vfsstream/src/main/php/org/bovigo/vfs/vfsStreamFile.php'];
104+
105+
yield 'Kint via Composer' => [VENDORPATH . 'kint-php/kint/src/Parser/Parser.php'];
106+
107+
yield 'Kint via bundle' => [SYSTEMPATH . 'ThirdParty/Kint/Parser/Parser.php'];
108+
}
109+
110+
public function testSplObjectHashDeprecationOutsideVendorStillThrows(): void
111+
{
112+
$config = new ExceptionsConfig();
113+
114+
$config->logDeprecations = false;
115+
116+
$this->exception = new Exceptions($config);
117+
118+
$message = 'Function spl_object_hash() is deprecated since 8.6, consider using spl_object_id() instead';
119+
120+
$this->expectException(ErrorException::class);
121+
$this->expectExceptionMessage($message);
122+
123+
$this->exception->errorHandler(E_DEPRECATED, $message, APPPATH . 'Foo.php', 1);
124+
}
125+
81126
public function testSuppressedDeprecationsAreLogged(): void
82127
{
83128
$config = new ExceptionsConfig();

0 commit comments

Comments
 (0)