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
Add return shape for Throwable::getTrace #7798
Conversation
I found these snippets: https://psalm.dev/r/b35a8df2f3<?php
/**
* @psalm-type Backtrace list<array{file:string,line:int,function:string,class?:string,type?:'::'|'->',args:array<mixed>}>
*/
/**
* From:
*
* class cls
* {
* public static function meth(int $a, string $b) : void
* {
* throw new Exception;
* }
* }
*
* @return Backtrace
*/
function traceWhenInsideStaticMethod()
{
return array (
0 =>
array (
'file' => '/in/9n6qW',
'line' => 12,
'function' => 'meth',
'class' => 'cls',
'type' => '::',
'args' =>
array (
0 => 1,
1 => 'hello',
),
),
);
}
/**
* From:
*
* class cls
* {
* public function meth2(int $a, string $b) : void
* {
* throw new Exception;
* }
* }
*
* @return Backtrace
*/
function traceWhenInsideObjectMethod()
{
return array (
0 =>
array (
'file' => '/in/iQoni',
'line' => 24,
'function' => 'meth2',
'class' => 'cls',
'type' => '->',
'args' =>
array (
0 => 1,
1 => 'hello',
),
),
);
}
/**
* From:
*
* function func()
* {
* throw new Exception;
* }
*
* @return Backtrace
*/
function traceWhenInsideFunction()
{
return array (
0 =>
array (
'file' => '/in/2cEkb',
'line' => 22,
'function' => 'func',
'args' =>
array (
),
),
);
}
/**
* From:
*
* (function() { throw new Exception;})();
*
* @return Backtrace
*/
/**
* From:
*
* (function() { throw new Exception;})();
*
* @return Backtrace
*/
function traceWhenInsideAnonymousFuncion()
{
return array (
0 =>
array (
'file' => '/in/QlGeB',
'line' => 38,
'function' => '{closure}',
'args' =>
array (
),
),
);
}
/**
* From:
*
* throw new Exception;
*
* @return Backtrace
*/
function traceWhenInGlobalScope()
{
return array (
);
}
|
2a4e044
to
defdb77
Compare
Updated to allow optional |
I found these snippets: https://psalm.dev/r/11247acbc9<?php
/**
* @psalm-type Backtrace list<array{file:string,line:int,function?:string,class?:string,type?:'::'|'->',args?:array<mixed>}>
*/
/**
* From:
*
* class cls
* {
* public static function meth(int $a, string $b) : void
* {
* throw new Exception;
* }
* }
*
* @return Backtrace
*/
function traceWhenInsideStaticMethod()
{
return array (
0 =>
array (
'file' => '/in/9n6qW',
'line' => 12,
'function' => 'meth',
'class' => 'cls',
'type' => '::',
'args' =>
array (
0 => 1,
1 => 'hello',
),
),
);
}
/**
* From:
*
* class cls
* {
* public function meth2(int $a, string $b) : void
* {
* throw new Exception;
* }
* }
*
* @return Backtrace
*/
function traceWhenInsideObjectMethod()
{
return array (
0 =>
array (
'file' => '/in/iQoni',
'line' => 24,
'function' => 'meth2',
'class' => 'cls',
'type' => '->',
'args' =>
array (
0 => 1,
1 => 'hello',
),
),
);
}
/**
* From:
*
* function func()
* {
* throw new Exception;
* }
*
* @return Backtrace
*/
function traceWhenInsideFunction()
{
return array (
0 =>
array (
'file' => '/in/2cEkb',
'line' => 22,
'function' => 'func',
'args' =>
array (
),
),
);
}
/**
* From:
*
* (function() { throw new Exception;})();
*
* @return Backtrace
*/
/**
* From:
*
* (function() { throw new Exception;})();
*
* @return Backtrace
*/
function traceWhenInsideAnonymousFuncion()
{
return array (
0 =>
array (
'file' => '/in/QlGeB',
'line' => 38,
'function' => '{closure}',
'args' =>
array (
),
),
);
}
/**
* From:
*
* throw new Exception;
*
* @return Backtrace
*/
function traceWhenInGlobalScope()
{
return array (
);
}
/**
* From:
*
* throw new Exception;
*
* @return Backtrace
*/
function traceWhenInInclude()
{
return array (
0 => array (
'file' => '/Users/ciaranmcnulty/Code/composer-scratch-test/test.php',
'line' => 3,
'function' => 'include',
),
);
}
|
Seems great! You'll have to change Callmap_Historical too for tests to pass |
@orklah should I update /stubs too? I'm not sure the connection between the two things |
1ab93f4
to
b661d8f
Compare
Yes, stubs should be updated too. CallMap is there because it's much faster to parse for simple functions, but stubs exist to add annotations that don't work in the CallMap, so the stubs override the CallMap. It looks like CoreImmutableClasses.phpstub is the only stub that needs changed? |
Note: you made a PR on master, it means it will be released as a part of Psalm 5 in the near future. If it's important to you to get this quickly, you may want to rebase your PR onto 4.x so it can be released faster |
This is based on the documentation for debug-backtrace: https://php.net/debug_backtrace Experimentation that shows the `object` field is not populated for exceptions: https://3v4l.org/iQoni And validation on psalm.dev: https://psalm.dev/r/b35a8df2f3
16030b2
to
ca185d7
Compare
I rebased onto 4.x I'm not sure what's happened there with Appveyor? |
Closed/reopened to try and nudge appveyor :/ |
Appveyor was removed from our CI ages ago, but sometimes on rebase it pops again, not sure why. As long as the rest of the CI runs, it's good |
Thanks! |
@@ -346,7 +346,7 @@ | |||
'ArgumentCountError::getLine' => ['int'], | |||
'ArgumentCountError::getMessage' => ['string'], | |||
'ArgumentCountError::getPrevious' => ['?Throwable'], | |||
'ArgumentCountError::getTrace' => ['list<array<string,mixed>>'], | |||
'ArgumentCountError::getTrace' => ['list<array{file:string,line:int,function:string,class?:string,type?:\'::\'|\'->\',args?:array<mixed>}>'], |
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.
per phpstan/phpstan-src#914 (comment) file
is optional
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.
and class
can be narrowed to class-string
.
let me steal this more precise type
-union-type into phpstan ;)
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.
line
is optional as well apparently.
This is based on the documentation for debug-backtrace:
https://php.net/debug_backtrace
Experimentation that shows the
object
field is not populated for exceptions:https://3v4l.org/iQoni
And validation on psalm.dev:
https://psalm.dev/r/b35a8df2f3
Should I also update the stubs?
What tests should I add?