Skip to content

Commit

Permalink
Use triggering class to generate baseline for deprecation messages fr…
Browse files Browse the repository at this point in the history
…om DebugClassLoader
  • Loading branch information
leongersen authored and nicolas-grekas committed Jul 7, 2023
1 parent 1572c5b commit 6ffaf5e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
4 changes: 3 additions & 1 deletion DeprecationErrorHandler/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ public function isBaselineDeprecation(Deprecation $deprecation)
return false;
}

if ($deprecation->originatesFromAnObject()) {
if ($deprecation->originatesFromDebugClassLoader()) {
$location = $deprecation->triggeringClass();
} elseif ($deprecation->originatesFromAnObject()) {
$location = $deprecation->originatingClass().'::'.$deprecation->originatingMethod();
} else {
$location = 'procedural code';
Expand Down
25 changes: 25 additions & 0 deletions DeprecationErrorHandler/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Deprecation
private $originClass;
private $originMethod;
private $triggeringFile;
private $triggeringClass;

/** @var string[] Absolute paths to vendor directories */
private static $vendors;
Expand All @@ -61,6 +62,10 @@ class Deprecation
*/
public function __construct($message, array $trace, $file, $languageDeprecation = false)
{
if (isset($trace[2]['class']) && \in_array($trace[2]['class'], [DebugClassLoader::class, LegacyDebugClassLoader::class], true)) {
$this->triggeringClass = $trace[2]['args'][0];
}

if (isset($trace[2]['function']) && 'trigger_deprecation' === $trace[2]['function']) {
$file = $trace[2]['file'];
array_splice($trace, 1, 1);
Expand Down Expand Up @@ -158,6 +163,26 @@ private function lineShouldBeSkipped(array $line)
return 'ReflectionMethod' === $class || 0 === strpos($class, 'PHPUnit\\');
}

/**
* @return bool
*/
public function originatesFromDebugClassLoader()
{
return isset($this->triggeringClass);
}

/**
* @return string
*/
public function triggeringClass()
{
if (null === $this->triggeringClass) {
throw new \LogicException('Check with originatesFromDebugClassLoader() before calling this method.');
}

return $this->triggeringClass;
}

/**
* @return bool
*/
Expand Down
50 changes: 44 additions & 6 deletions Tests/DeprecationErrorHandler/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\DeprecationGroup;
use Symfony\Component\ErrorHandler\DebugClassLoader;

class ConfigurationTest extends TestCase
{
Expand Down Expand Up @@ -356,7 +357,7 @@ public function testBaselineGenerationEmptyFile()
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, '')));
$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected_baseline = [
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Test message 1',
Expand All @@ -368,7 +369,7 @@ public function testBaselineGenerationEmptyFile()
'count' => 1,
],
];
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineGenerationNoFile()
Expand All @@ -383,7 +384,7 @@ public function testBaselineGenerationNoFile()
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, '')));
$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected_baseline = [
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Test message 1',
Expand All @@ -395,7 +396,7 @@ public function testBaselineGenerationNoFile()
'count' => 2,
],
];
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testExistingBaseline()
Expand Down Expand Up @@ -447,7 +448,7 @@ public function testExistingBaselineAndGeneration()
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 3', $trace, '')));
$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected_baseline = [
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Test message 2',
Expand All @@ -459,7 +460,44 @@ public function testExistingBaselineAndGeneration()
'count' => 1,
],
];
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader()
{
$filename = $this->createFile();
$configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile='.urlencode($filename));

$trace = debug_backtrace();
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Regular deprecation', $trace, '')));

$trace[2] = [
'class' => DebugClassLoader::class,
'function' => 'testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader',
'args' => [self::class]
];

$deprecation = new Deprecation('Deprecation by debug class loader', $trace, '');

$this->assertTrue($deprecation->originatesFromDebugClassLoader());

$this->assertTrue($configuration->isBaselineDeprecation($deprecation));

$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Regular deprecation',
'count' => 1,
],
[
'location' => self::class,
'message' => 'Deprecation by debug class loader',
'count' => 1,
],
];
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineArgumentException()
Expand Down

0 comments on commit 6ffaf5e

Please sign in to comment.