Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecations triggered from code called by native code #53

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/Doctrine/Deprecations/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function triggerIfCalledFromOutside(string $package, string $link,
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

// first check that the caller is not from a tests folder, in which case we always let deprecations pass
if (strpos($backtrace[1]['file'], DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR) === false) {
if (isset($backtrace[1]['file'], $backtrace[0]['file']) && strpos($backtrace[1]['file'], DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR) === false) {
$path = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package . DIRECTORY_SEPARATOR;

if (strpos($backtrace[0]['file'], $path) === false) {
Expand Down Expand Up @@ -173,16 +173,16 @@ public static function triggerIfCalledFromOutside(string $package, string $link,
}

/**
* @param array<mixed> $backtrace
* @param list<array{function: string, line?: int, file?: string, class?: class-string, type?: string, args?: mixed[], object?: object}> $backtrace
*/
private static function delegateTriggerToBackend(string $message, array $backtrace, string $link, string $package): void
{
$type = self::$type ?? self::getTypeFromEnv();

if (($type & self::TYPE_PSR_LOGGER) > 0) {
$context = [
'file' => $backtrace[0]['file'],
'line' => $backtrace[0]['line'],
'file' => $backtrace[0]['file'] ?? null,
'line' => $backtrace[0]['line'] ?? null,
'package' => $package,
'link' => $link,
];
Expand All @@ -198,10 +198,10 @@ private static function delegateTriggerToBackend(string $message, array $backtra

$message .= sprintf(
' (%s:%d called by %s:%d, %s, package %s)',
self::basename($backtrace[0]['file']),
$backtrace[0]['line'],
self::basename($backtrace[1]['file']),
$backtrace[1]['line'],
self::basename($backtrace[0]['file'] ?? 'native code'),
$backtrace[0]['line'] ?? 0,
self::basename($backtrace[1]['file'] ?? 'native code'),
$backtrace[1]['line'] ?? 0,
$link,
$package
);
Expand Down
13 changes: 13 additions & 0 deletions test_fixtures/src/ConstructorDeprecation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace DeprecationTests;

use Doctrine\Deprecations\Deprecation;

class ConstructorDeprecation
{
public function __construct()
{
Deprecation::trigger('doctrine/bar', 'https://github.com/doctrine/deprecations/issues/44', 'This constructor is deprecated.');
}
}
16 changes: 16 additions & 0 deletions tests/Doctrine/Deprecations/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Doctrine\Deprecations;

use DeprecationTests\ConstructorDeprecation;
use DeprecationTests\Foo;
use DeprecationTests\RootDeprecation;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\Foo\Baz;
use PHPUnit\Framework\Error\Deprecated;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use ReflectionProperty;
use Throwable;

Expand Down Expand Up @@ -298,4 +300,18 @@ public function testDeprecationTriggerByEnv(): void
Deprecation::trigger('Foo', __METHOD__, 'message');
$this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount());
}

public function testDeprecationTriggeredFromNativeCode(): void
{
$ref = new ReflectionClass(ConstructorDeprecation::class);

Deprecation::enableWithTriggerError();
$this->expectErrorHandler(
'This constructor is deprecated. (ConstructorDeprecation.php:%d called by native code:0, https://github.com/doctrine/deprecations/issues/44, package doctrine/bar)',
'https://github.com/doctrine/deprecations/issues/44'
);

$ref->newInstance();
$this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount());
}
}