Skip to content

Commit

Permalink
Merge pull request #50 from reinfi/fix-access-to-unknown-index
Browse files Browse the repository at this point in the history
fix access of undefined array index file
  • Loading branch information
Ocramius authored Dec 15, 2022
2 parents 2e3d39e + 7c1f50d commit 7f34f50
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/EventLogging/EventContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public function getEventTriggerFile()
{
$backtrace = $this->getDebugBacktrace();

if (! isset($backtrace[4]['file'])) {
return '';
}

if (file_exists($backtrace[4]['file'])) {
return basename(dirname($backtrace[4]['file'])) . '/' . basename($backtrace[4]['file']);
}
Expand Down
49 changes: 49 additions & 0 deletions test/EventLogging/EventContextProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace LaminasTest\DeveloperTools\EventLogging;

use Laminas\DeveloperTools\EventLogging\EventContextProvider;
use PHPUnit\Framework\TestCase;

use function array_map;

/** @covers \Laminas\DeveloperTools\EventLogging\EventContextProvider */
class EventContextProviderTest extends TestCase
{
public function testEventTriggerFileDetectsEmptyFileLocationWhenStackFrameIsInPhpItself(): void
{
self::assertSame(
[
'stack frame on this file: EventLogging/EventContextProviderTest.php',
'stack frame in php core: ',
'stack frame on this file: EventLogging/EventContextProviderTest.php',
],
// Important: `array_map()` needed here, as it produces a stack frame without "file" location
array_map(
[$this, 'callEventContextProviderWithStackTraceNestingLevelAndName'],
[
'stack frame on this file',
'stack frame in php core',
'stack frame on this file',
],
[
3,
4,
5,
]
),
'trigger file should be empty because user function calls do not refer to a file'
);
}

private function callEventContextProviderWithStackTraceNestingLevelAndName(string $name, int $level): string
{
if ($level > 0) {
return $this->callEventContextProviderWithStackTraceNestingLevelAndName($name, $level - 1);
}

return $name . ': ' . (new EventContextProvider())->getEventTriggerFile();
}
}

0 comments on commit 7f34f50

Please sign in to comment.