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

Symfony Debug & VarDumper - Your daily Must-Have

Symfony Debug & VarDumper - Your daily Must-Have

Nicolas Grekas

May 12, 2016
Tweet

More Decks by Nicolas Grekas

Other Decks in Programming

Transcript

  1. #DrupalCon @nicolasgrekas ✤ Fixing trivial errors ✤ Fixing wrong behaviors

    ✤ Starts with understanding what’s going on Debugging & Dumping
  2. #DrupalCon @nicolasgrekas ✤ Warnings, Notices, Deprecations ✤ User errors, Recoverable

    errors ✤ Parse errors, Undefined function/class, Type errors ✤ OOM ✤ etc. Dealing with errors in PHP
  3. #DrupalCon @nicolasgrekas ✤ Warnings, Notices, Deprecations ✤ User errors, Recoverable

    errors ✤ $callback = function ($type, $msg, $file, $line, $ctx) {…}; ✤ error_reporting() & “@” silencing operator set_error_handler($callback);
  4. #DrupalCon @nicolasgrekas ✤ Parse errors, Undefined function/class, Type errors ✤

    ~OOM ✤ $callback = function () {$e = error_get_last();…}; register_shutdown_function($callback);
  5. #DrupalCon @nicolasgrekas ✤ try {…} catch (\Exception $e) {…} ✤

    set_exception_handler($callback); ✤ $callback = function ($exception) {…}; Exceptions
  6. #DrupalCon @nicolasgrekas ✤ try {…} catch (\Error $e) {…} ✤

    set_exception_handler($callback); ✤ $callback = function ($exception) {…}; Error/Throwable since PHP 7!
  7. #DrupalCon @nicolasgrekas ✤ Typos ✤ Character case mistakes ✤ Decorate

    using spl_autoload_functions(); ✤ Add suggestions for typos using levenshtein Class loading issues
  8. #DrupalCon @nicolasgrekas Drupal defaults ✤ Notices/warnings are logged ✤ Uncaught

    \Exceptions trigger the kernel.exception event ✤ PHP 7 \Errors bubble up to the root of the call stack ✤ Uncaught exceptions and fatal errors are logged by the native PHP handler (depending on your ini settings)
  9. #DrupalCon @nicolasgrekas Symfony defaults ✤ Notices/warnings are converted to exceptions

    ✤ Uncaught \Exceptions trigger the kernel.exception event ✤ PHP 7 \Errors bubble up to the root of the call stack ✤ Uncaught exceptions and fatal errors are reinjected into the kernel and end up triggering the kernel.exception event ✤ All types/levels are logged, even when silenced
  10. #DrupalCon @nicolasgrekas ErrorHandler::register() ✤ Converts PHP errors to exceptions ✤

    Adds scope (local vars) & stack trace info ✤ Un-silence configured error levels ✤ Turns fatal errors into exceptions
  11. #DrupalCon @nicolasgrekas Mission statement ✤ Easy to read HTML or

    CLI output ✤ Accurate state representation ✤ PHP proof ✤ Extensible and reusable
  12. #DrupalCon @nicolasgrekas Internal mechanism – #1 ✤ VarCloner takes any

    $vars ✤ Turns it into a Data object ✤ Using Caster callables ✤ And enforcing state extraction limits
  13. #DrupalCon @nicolasgrekas Internal mechanism – #2 ✤ The resulting Data

    object ✤ Contains a simple PHP array ✤ That holds full precision ✤ Over the state of the initial $var
  14. #DrupalCon @nicolasgrekas Internal mechanism – #3 ✤ A Dumper object

    (CliDumper/HtmlDumper/YourDumper) ✤ Turns the Data object into a string representation ✤ That is written to a PHP stream or line callback
  15. #DrupalCon @nicolasgrekas ✤ Then just “dump($var)”! ✤ composer global require

    symfony/var-dumper ✤ auto_prepend_file=$HOME/.composer/vendor/autoload.php composer require symfony/var-dumper