79370113

Date: 2025-01-20 01:40:17
Score: 3
Natty:
Report link

I asked ChatGPT for advice on this and after initially suggesting a solution using regular expressions, I then asked it to refine the answer using nikic/php-parser and with a little tweaking I got a working response.

I can't post the result on Stack Overflow as it's against the site policy https://stackoverflow.com/help/gen-ai-policy but the short version of this is:

  1. Make a traverser that looks for Node\Stmt\ClassMethod nodes
  2. If the node has statements (i.e. a method body) and that node doesn't include a Node\Stmt\Return_ node then add it to a list
  3. Create a parser on the source to be inspected
  4. Make a traverser, and add the visitor mentioned above to the traverser
  5. Execute and dump results

Here's the working code: https://gist.github.com/gurubobnz/2ae85e5010158896789e75f3ea375803

There will be an issue here in that there is no execution guarantee of the return, e.g. if (false) { return []; } will still be considered a return, but it is good enough for me to make a start on my corpus of source files.

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Blacklisted phrase (1): stackoverflow.com/help
  • Contains signature (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: GuruBob