Skip to content

Fix issue with modified backtrace #250

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

Merged
merged 1 commit into from
Aug 15, 2020
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
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<rule ref="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix">
<exclude-pattern>*/src/*/Abstract*.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod">
Copy link
Contributor

@williamdes williamdes Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be moved to the source file, but it depends on your code policy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My proposed change would avoid this error

<exclude-pattern>*/src/DocBlock/Tags/InvalidTag.php</exclude-pattern>
</rule>
</ruleset>
51 changes: 31 additions & 20 deletions src/DocBlock/Tags/InvalidTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
use Exception;
use phpDocumentor\Reflection\DocBlock\Tag;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use Throwable;
use function array_map;
use function array_walk_recursive;
use function get_class;
use function get_resource_type;
use function is_array;
use function is_object;
use function is_resource;
use function sprintf;
Expand Down Expand Up @@ -80,29 +81,12 @@ private function flattenExceptionBacktrace(Throwable $exception) : void
$traceProperty = (new ReflectionClass(Exception::class))->getProperty('trace');
$traceProperty->setAccessible(true);

$flatten =
/** @param mixed $value */
static function (&$value) : void {
if ($value instanceof Closure) {
$closureReflection = new ReflectionFunction($value);
$value = sprintf(
'(Closure at %s:%s)',
$closureReflection->getFileName(),
$closureReflection->getStartLine()
);
} elseif (is_object($value)) {
$value = sprintf('object(%s)', get_class($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
}
};

do {
$trace = $exception->getTrace();
if (isset($trace[0]['args'])) {
$trace = array_map(
static function (array $call) use ($flatten) : array {
array_walk_recursive($call['args'], $flatten);
function (array $call) : array {
$call['args'] = array_map([$this, 'flattenArguments'], $call['args']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give the php version requirements I would recommend (not tested):
https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/composer.json#L17

Suggested change
$call['args'] = array_map([$this, 'flattenArguments'], $call['args']);
$call['args'] = array_map(function($value) { return $this->flattenArguments($value); }, $call['args']);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 for changing this. The array-callable syntax is much shorter and clearer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, for me this syntax is "compilation" compatible and explicit for any code searcher or analyser :)
Don't you agree?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that would remove the need of ref="SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod">

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both can be formally detected. I guess it just hasn't been implemented by that code style package. psalm itself certainly recognises uses like that, in it's core stuff.


return $call;
},
Expand All @@ -117,6 +101,33 @@ static function (array $call) use ($flatten) : array {
$traceProperty->setAccessible(false);
}

/**
* @param mixed $value
*
* @return mixed
*
* @throws ReflectionException
*/
private function flattenArguments($value)
{
if ($value instanceof Closure) {
$closureReflection = new ReflectionFunction($value);
$value = sprintf(
'(Closure at %s:%s)',
$closureReflection->getFileName(),
$closureReflection->getStartLine()
);
} elseif (is_object($value)) {
$value = sprintf('object(%s)', get_class($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
} elseif (is_array($value)) {
$value = array_map([$this, 'flattenArguments'], $value);
}

return $value;
}

public function render(?Formatter $formatter = null) : string
{
if ($formatter === null) {
Expand Down
75 changes: 75 additions & 0 deletions tests/integration/ModifyBackTraceSafeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection;


use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class ModifyBackTraceSafeTest extends TestCase
{
public function testBackTraceModificationDoesNotImpactFunctionArguments()
{
$traverser = new Traverser();
$node1 = new Node();
$node1->children[] = new Node();
$node1->children[] = new Node();

$traverser->traverse([new Node(), $node1]);
}
}


class Node {
public $children = [];
}

class Traverser
{
public function traverse(array $nodes)
{
$this->traverseArray($nodes);
}

public function traverseArray(array $nodes): array
{
$doNodes = [];

foreach ($nodes as &$node) {
$node = $this->callback($node);
$node = $this->traverseNode($node);

$doNodes[] = $node;
}

return $doNodes;
}

public function callback(Node $class) : Node
{
$docblock = <<<DOCBLOCK
/**
* @see sql.php
*/
DOCBLOCK;

$factor = DocBlockFactory::createInstance();

$factor->create($docblock);

return $class;
}

private function traverseNode(Node $node) : Node
{
if ($node->children) {
$this->traverseArray($node->children);
}

return $node;
}
}