-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
@@ -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']); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Give the php version requirements I would recommend (not tested):
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👎 for changing this. The array-callable syntax is much shorter and clearer? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And that would remove the need of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
}, | ||||||
|
@@ -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) { | ||||||
|
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; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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