Upgrade to Pro — share decks privately, control downloads, hide ads and more …

(x)debug Silverstripe

(x)debug Silverstripe

xdebug talk by Lukas Erni and Werner M. Krauß at SilverStripe EU Conference in Ljubljana, Slovenia 2024 www.stripecon.eu

wernerkrauss

October 08, 2024
Tweet

More Decks by wernerkrauss

Other Decks in Technology

Transcript

  1. Werner M. Krauß wmk Bad Ischl, Austria netwerkstatt.at 2 kids,

    at least 5 guitars, pilgrim Lukas Erni lerni Ruswil, Switzerland kraftausdruck.ch 2 kids, Beekeeping
  2. What has changed since 2016? Things getting slower with containered

    development environment but gaining speed with Xdebug 3.x, PHP 7.x & 8.x and much more predictable & streamlined development environment. With DDEV/containers, setting-up Xdebug has become a breeze.
  3. History of bugs • the class of insects originated on

    Earth about 480 million years ago • so… • bugs have been there all the time before computers • and annoyed engineers • fun fact: bed bugs are the horror of all pilgrims
  4. 19th Century Hardware Engineering „The first step [in all of

    my inventions] is an intuition, and comes with a burst, then difficulties arise – this thing gives out and [it is] then that ‘Bugs’ – as such little faults and difficulties are called – show themselves […].“ Thomas Edison, 1878
  5. The first Bug • 09.09.1947, Harvard Faculty at the Computation

    Laboratory • Operators traced an error in the Mark II to a moth trapped in a relay, coining the term bug. • This bug was carefully removed and taped to the log book. • source: Wikipedia
  6. Debug History in PHP / Silverstripe CMS • echo($var); •

    print_r($array); • die("I'm here"); Not in Live mode • debug::show(...); • debug::message(...); Also in Live mode • debug::dump(...); In Templates • $Foo.Debug()
  7. Debugging in PHP Pros: • easy to use • immediate

    output Cons: • debugging in code tends to end up in git • not the best tool for the job
  8. Silverstripe Debug Parameters How to get more informations from Silverstripe

    CMS • ?isDev=1 Put the site into development mode, enabling debugging messages to the browser on a live server. For security, you'll be asked to log in with an administrator log-in. Will persist for the current browser session. • ?isTest=1 See above. • ?debug=1 Show a collection of debugging information about the director / controller operation • ?debug_request=1 Show all steps of the request from initial HTTPRequest to Controller to Template Rendering
  9. Silverstripe Debug Parameters #2 • ?showqueries=1 List all SQL queries

    executed • ?showtemplate=1 Show the compiled version of all the templates used, including line numbers. Good when you have a syntax error in a template. Cannot be used on a Live site without isDev when logged in as Admin. https://docs.silverstripe.org/en/5/developer_guides/debugging/ https://docs.silverstripe.org/en/5/developer_guides/debugging/url_variable_tools/
  10. You can disable that (for security reasons) --- Only: environment:

    'live' --- SilverStripe\Dev\DevelopmentAdmin: deny_non_cli: true
  11. Debugbar shows you: Timeline execution time overview Database Queries, Long

    running queries System logs and messages Shows anything processed by a logger -> no need to check log Session Cookies Parameter Requirements Middleware Template SiteConfig Config System Cache Mails Headers CMS & PHP Version, Time & Memory Usage
  12. DDEV • for local development • based on docker •

    has everything you need and a lot extensions for special requirements • apache-fpm/nginx • mariadb • all major PHP-Versions • project-type=silverstripe (thanks to firesphere) • plugins for PHPStorm and VSCode • See https://ddev.com/
  13. Why should I? • find errors / bugs more easier

    • know the tools for craftsmanship 🛠 • no debugging information gets committed to git • easy setup with ddev and PHPStorm / VScode • actually it works out of the box 🦾 • ddev xdebug on/off ◦ switch it off for a faster dev experience when not debugging • you’ll become more sexy ❤
  14. What happened until now • Xdebug is a PHP extension

    written by Derick Rethans - he works on it since 2002! • It uses the DBGp debugging protocol • It is a powerful tool for debugging and profiling PHP code.
  15. Xdebug still worth a talk? Just click • DDEV makes

    setup much easier! Tweak a few things in your boilerplate, to make it always available. https://github.com/lerni/ootstra/tree/master/.vscode VSCode Extensions → .vscode/extensions.json • DDEV Manager - mainly automatic ‘ddev xdebug true/false’ • PHP Debug Adapter .vscode/tasks.json & .vscode/launch.json • "hostname": "0.0.0.0" for CLI debugging • "pathMappings": {“/var/www/html": "${workspaceFolder}"}
  16. Fake Client IDE/Editor and listen to Xdebug ddev xdebug on

    $ nc -l 0.0.0.0 9003 - and fetch an url with the browser or curl
  17. Why is XDebug better than var_dump() and die()? • Breakpoint

    • Conditional breakpoint • List of all available variables in current scope • Watch • Frames (stack of called functions)
  18. Methods Step Over => goto next line Step Into =>

    go inside a called function or method Force Step Into Step Out => leave the current method Run to Cursor
  19. More Methods Resume Program => goto next breakpoint Evaluate Expression

    Quick Evaluate Expression => without dialog Toggle Breakpoint View Breakpoints
  20. Example: Debugging unit tests • good for more complicated tasks

    that are not easy to reach on the site ◦ e.g. shop checkout functionality • fixtures maybe a bit hard to setup • when a test works you’re done • best done via CLI
  21. PHPStan Pros: • Totally annoying • can check your code

    for bugs before they reach production • works better with well typed classes • forces you to think about types Cons: • Did I say it’s totally annoying… at least in the beginning
  22. PHPStan: installation • Of course using composer as a dev

    requirement • There’s a package to make PHPStan understand Silverstripe, e.g. DataObject::get() and its magic properties. composer require --dev syntro/silverstripe-phpstan ^5
  23. SSShell (Silverstripe’s shell, not SSS hell!) • SSShell is a

    REPL for SilverStripe running on Psy Shell 🚀 • PsySH is a runtime developer console, interactive debugger and REPL for PHP. • REPL = Read-eval-print loop You can • view classes/objects and static properties • run methods on objects • run sake commands and flush
  24. Why should I use SSShell • good for tinkering around

    in Silverstripe • The interactive debugger saves lives! Stop die()ing all the time. • an alternative for executing simple one time tasks More informations: • https://github.com/pstaender/ssshell • https://psysh.org/