Skip to content

Commit a0516c9

Browse files
authored
Merge pull request #854 from cakephp/deprecation
fix deprecation line php8.0+
2 parents ca05898 + c704370 commit a0516c9

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/Plugin.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ function ($code, $message, $file, $line, $context = null) use (&$previousHandler
100100
if (PHP_VERSION_ID >= 80000) {
101101
$trace = debug_backtrace();
102102
foreach ($trace as $idx => $traceEntry) {
103-
if ($traceEntry['function'] !== 'trigger_error') {
103+
if ($traceEntry['function'] !== 'deprecationWarning') {
104104
continue;
105105
}
106-
$file = $trace[$idx + 2]['file'];
107-
$line = $trace[$idx + 2]['line'];
106+
$offset = 1;
107+
// ['args'][1] refers to index of $stackFrame argument in deprecationWarning()
108+
if (isset($traceEntry['args'][1])) {
109+
$offset = $traceEntry['args'][1];
110+
}
111+
$file = $trace[$idx + $offset]['file'];
112+
$line = $trace[$idx + $offset]['line'];
108113
break;
109114
}
110115
}

tests/TestCase/PluginTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12+
* @link http://cakephp.org CakePHP(tm) Project
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace DebugKit\Test\TestCase;
16+
17+
use Cake\Event\Event;
18+
use Cake\Event\EventManager;
19+
use Cake\TestSuite\TestCase;
20+
use DebugKit\Panel\DeprecationsPanel;
21+
use DebugKit\Plugin;
22+
use DebugKit\ToolbarService;
23+
24+
/**
25+
* Test the Plugin
26+
*/
27+
class PluginTest extends TestCase
28+
{
29+
/**
30+
* Test setDeprecationHandler.
31+
*
32+
* @return void
33+
*/
34+
public function testSetDeprecationHandler()
35+
{
36+
DeprecationsPanel::clearDeprecatedErrors();
37+
$service = new ToolbarService(new EventManager(), []);
38+
$plugin = new Plugin();
39+
$plugin->setDeprecationHandler($service);
40+
$event = new Event('');
41+
$panel = new DeprecationsPanel();
42+
43+
//Without setting the $stackFrame
44+
deprecationWarning('setDeprecationHandler');
45+
//Setting the $stackFrame
46+
deprecationWarning('setDeprecationHandler_2', 2);
47+
//Raw error
48+
$line = __LINE__ + 1;
49+
trigger_error('raw_error', E_USER_DEPRECATED);
50+
51+
$panel->shutdown($event);
52+
$data = $panel->data()['plugins']['DebugKit'];
53+
54+
$this->assertCount(3, $data);
55+
56+
//test first two deprecationWarning()
57+
foreach ([$data[0], $data[1]] as $value) {
58+
$this->assertStringContainsString($value['file'], $value['message']);
59+
$this->assertStringContainsString("line: {$value['line']}", $value['message']);
60+
}
61+
//test raw error
62+
$this->assertSame($line, $data[2]['line']);
63+
}
64+
}

0 commit comments

Comments
 (0)