ryota21silvaの技術ブログ

Funna(ふんな)の技術ブログ

これまで学んだ技術の備忘録。未来の自分が救われることを信じて

PHPのPshSHで対話型デバッグを行う

composerを使ってインストール

$ composer g require psy/psysh:@stable                                                                                                                       

Changed current directory to /Users/funesakisuke/.composer
./composer.json has been created
Running composer update psy/psysh
Loading composer repositories with package information
Updating dependencies
Lock file operations: 14 installs, 0 updates, 0 removals
  - Locking dnoegel/php-xdg-base-dir (v0.1.1)
  - Locking nikic/php-parser (v4.10.4)

Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 14 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-php80 (v1.22.0): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.22.0): Extracting archive
archive
7 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
10 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

REPLとしてpsyshを使う

これでコンソールで色々試せる。 Tabを押せば関数を調べられる。

$ ./vendor/bin/psysh                                                                                                                                         
Psy Shell v0.10.6 (PHP 7.4.14 — cli) by Justin Hileman

>>> set
set_error_handler     set_file_buffer       set_time_limit        setlocale             settype
set_exception_handler set_include_path      setcookie             setrawcookie

$ ./vendor/bin/psyshではなく、psyshで利用できるようにパスを通す

# $HOME/.composer/vendor/binを追加
export PATH=“$HOME/.composer/vendor/bin:$PATH”
$ psysh                                                                                                                                            
Using local PsySH version at ~/workspace/app/cake_3_tutorial/cake_3_tutorial
Psy Shell v0.10.6 (PHP 7.4.14 — cli) by Justin Hileman


>>> function timesFive($x) {
...  $result = $x * 5;
...  return $result;
... }
>>> timesFive(10);
=> 50

cake consoleもある。php -aでいい気もした。

$ bin/cake console          
You can exit with `CTRL-C` or `exit`

Psy Shell v0.10.6 (PHP 7.4.14 — cli) by Justin Hileman
>>>
$ php -a                    
Interactive shell

php >

デバッガーとしてpsyshを使う

コードの中にeval(\Psy\sh());を記述すれば、そこがブレークポイントの位置となりデバッグを行える。

Psy Shell v0.10.6 (PHP 7.4.14 — cli-server) by Justin Hileman
From src/Controller/ArticlesController.php:60:
    58:     $tags = $this->Articles->Tags->find('list');
    59:
  > 60:     eval(\Psy\sh());
    61:
    62:     // ビューコンテキストに tags をセット
Psy Shell v0.10.6 (PHP 7.4.14 — cli-server) by Justin Hileman
From src/Controller/ArticlesController.php:60:
    58:     $tags = $this->Articles->Tags->find('list');
    59:
  > 60:     eval(\Psy\sh());
    61:
    62:     // ビューコンテキストに tags をセット

$tags
=> Cake\ORM\Query {#85
     (help): "This is a Query object, to get the results execute or iterate it.",
     sql: "SELECT tags.id AS `tags__id`, tags.title AS `tags__title` FROM tags tags",
     params: [],
     defaultTypes: [
       "tags__id" => "integer",
       "tags.id" => "integer",
       "id" => "integer",
       "tags__title" => "string",
       "tags.title" => "string",
       "title" => "string",
         ...
       .....
       .......

$article->id
=> 2

$article->title
=> "おはようだこんばんは"

$this->request->is(['post', 'put'])
=> false

コマンドオプション

Commands · bobthecow/psysh Wiki · GitHub

help
  help       Show a list of commands. Type `help [foo]` for information about [foo].      Aliases: ?
  ls         List local, instance or class variables, methods and constants.              Aliases: dir
  dump       Dump an object or primitive.
  doc        Read the documentation for an object, class, constant, method or property.   Aliases: rtfm, man
  show       Show the code for an object, class, constant, method or property.
  wtf        Show the backtrace of the most recent exception.                             Aliases: last-exception, wtf?
  whereami   Show where you are in the code.
  throw-up   Throw an exception or error out of the Psy Shell.
  timeit     Profiles with a timer.
  trace      Show the current call stack.
  buffer     Show (or clear) the contents of the code input buffer.                       Aliases: buf
  clear      Clear the Psy Shell screen.
  edit       Open an external editor. Afterwards, get produced code in input buffer.
  sudo       Evaluate PHP code, bypassing visibility restrictions.
  history    Show the Psy Shell history.                                                  Aliases: hist
  exit       End the current session and return to caller.                                Aliases: quit, q

参考