Skip to content

Commit

Permalink
bug #35597 [PHPunit bridge] Provide current file as file path (greg0ire)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[PHPunit bridge] Provide current file as file path

I failed to apply perfectly this comment:
#33820 (comment)
It should fix one failing test in the bridge.

| Q             | A
| ------------- | ---
| Branch?       |4.4
| Bug fix?      | not for the end user
| New feature?  | no
| Deprecations? | no
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

Commits
-------

d5302cb Provide current file as file path
  • Loading branch information
nicolas-grekas committed Feb 5, 2020
2 parents abeee5f + d5302cb commit b6acfae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
9 changes: 3 additions & 6 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Expand Up @@ -104,16 +104,13 @@ public static function collectDeprecations($outputFile)
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
}

$trace = debug_backtrace();
$filesStack = [];
foreach ($trace as $line) {
if (\in_array($line['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
foreach (debug_backtrace() as $frame) {
if (!isset($frame['file']) || \in_array($frame['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
continue;
}

if (isset($line['file'])) {
$filesStack[] = $line['file'];
}
$filesStack[] = $frame['file'];
}

$deprecations[] = [error_reporting(), $msg, $file, $filesStack];
Expand Down
34 changes: 16 additions & 18 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php
Expand Up @@ -165,24 +165,6 @@ public function isMuted()
return false !== strpos($this->triggeringFile, \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'phpunit'.\DIRECTORY_SEPARATOR);
}

private function getOriginalFilesStack(): array
{
if (null === $this->originalFilesStack) {
$this->originalFilesStack = [];
foreach ($this->trace as $line) {
if (\in_array($line['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
continue;
}
if (!isset($line['file'])) {
continue;
}
$this->originalFilesStack[] = $line['file'];
}
}

return $this->originalFilesStack;
}

/**
* Tells whether both the calling package and the called package are vendor
* packages.
Expand Down Expand Up @@ -224,6 +206,22 @@ public function getType()
return self::TYPE_DIRECT;
}

private function getOriginalFilesStack(): array
{
if (null === $this->originalFilesStack) {
$this->originalFilesStack = [];
foreach ($this->trace as $frame) {
if (!isset($frame['file']) || \in_array($frame['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
continue;
}

$this->originalFilesStack[] = $frame['file'];
}
}

return $this->originalFilesStack;
}

/**
* getPathType() should always be called prior to calling this method.
*
Expand Down
Expand Up @@ -157,7 +157,7 @@ public function providerGetTypeDetectsSelf(): array
}

return [
'not_from_vendors_file' => [Deprecation::TYPE_SELF, '', 'MyClass1', ''],
'not_from_vendors_file' => [Deprecation::TYPE_SELF, '', 'MyClass1', __FILE__],
'nonexistent_file' => [Deprecation::TYPE_UNDETERMINED, '', 'MyClass1', 'dummy_vendor_path'],
'serialized_trace_with_nonexistent_triggering_file' => [
Deprecation::TYPE_UNDETERMINED,
Expand Down

0 comments on commit b6acfae

Please sign in to comment.