ホームページ >バックエンド開発 >PHPチュートリアル >PSYSHでのインタラクティブなPHPデバッグ
午前1時で、Webアプリケーションの配信の締め切りは8時間で、機能していません。 何が起こっているのかを把握しようとすると、バグがどこにあるかを確認するために、どこでもvar_dump()とdie()でコードを入力します…
あなたはイライラしています。返品値または変数割り当てを試すたびに、ソースコードを変更し、アプリケーションを実行し、結果を確認する必要があります。コード。この状況はあなたに馴染みがありますか?
Direct Download(Linux/Mac)
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
さらに、この記事の後半で説明するように、作曲家とのプロジェクトごとに含めることができます。
さあ、Psyshで少し遊んでみましょう。
主な助けはあなたの親友になります。それはあなたにあらゆる種類のコマンドとその説明を与えるものです:
composer g require psy/psysh:~0.1 psysh
wget psysh.org/psysh chmod +x psysh ./psysh基本的に、Replができることは
です
PSYSHをPHPのインタラクティブコンソールと比較すると、PSYSHは割り当てられたらすぐに$ A値を印刷することに注意してください。
より複雑な例は次のとおりです
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >関数()を定義し、それを呼び出しました。 これらの2つのヌルは、関数定義もその実行も値を返していないためです(関数は値をエコーします)。 さらに、関数を定義している間、プロンプトは>>>から....
に変更されました
クラスを定義して、インスタンス化できますか?
composer g require psy/psysh:~0.1 psyshfooをインスタンス化すると、コンストラクターはオブジェクトへの参照を返しました。 これが、psysh printed
クラスFooが定義した方法を忘れた場合、あなたは今、答えを得ました。 Linux OSまたはMacコマンドラインインターフェイスを使用しましたか?その後、LSコマンドに精通している可能性があります。 -laオプションを覚えていますか?
wget psysh.org/psysh chmod +x psysh ./psysh
甘いですね
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
psyshの真の力は、Webアプリケーションと統合されたときに輝いているので、それを構築しましょう。
デモAPPデコレーターのデザインパターンを紹介するための簡単なアプリケーションを実装します。このようなパターンのUMLクラス図は次のとおりです。
UMLやデザインパターンについてあまり知らない場合でも心配しないでください。この記事には、それらを理解することは必要ありません。
また、このプロジェクトでは、一連のテストケースを作成しました。 これらのテストケースは、phpunitによって実行できます。繰り返しますが、この記事を理解するためにユニットテストに精通する必要はありません。
この小さなアプリケーションの完全なソースコードは、https://github.com/sitepoint-examples/psyshにあります。
作曲家のインストールの後、行くのがいいはずです。
ファイルpublic/decorator.phpの次のソースコードをご覧ください。シンプルウィンドウ、装飾されたウィンドウ、およびタイトルを付けて、デコレーターパターンを紹介するオブジェクトを描いています。
PHPのCLI(コマンドラインインターフェイス)を介して、または構成されている場合はWebサーバーを介してコードを実行できます。 PHPの内部Webサーバーも使用できます
cli>>> 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: list, 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? 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. history Show the Psy Shell history. exit End the current session and return to caller. Aliases: quit, qでデバッグ
コマンドラインインターフェイスを介した上記のコードの実行は次のようになります。
>>> help ls Usage: ls [--vars] [-c|--constants] [-f|--functions] [-k|--classes] [-I|--interfaces] [-t|--traits] [-p|--properties] [-m|--methods] [-G|--grep="..."] [-i|--insensitive] [-v|--invert] [-g|--globals] [-n|--internal] [-u|--user] [-C|-- category="..."] [-a|--all] [-l|--long] [target] Aliases: list, dir Arguments: target A target class or object to list. Options: --vars Display variables. --constants (-c) Display defined constants. --functions (-f) Display defined functions. --classes (-k) Display declared classes. --interfaces (-I) Display declared interfaces. --traits (-t) Display declared traits. --properties (-p) Display class or object properties (public properties by default). --methods (-m) Display class or object methods (public methods by default). --grep (-G) Limit to items matching the given pattern (string or regex). --insensitive (-i) Case-insensitive search (requires --grep). --invert (-v) Inverted search (requires --grep). --globals (-g) Include global variables. --internal (-n) Limit to internal functions and classes. --user (-u) Limit to user-defined constants, functions and classes. --category (-C) Limit to constants in a specific category (e.g. "date"). --all (-a) Include private and protected methods and properties. --long (-l) List in long format: includes class names and method signatures. Help: List variables, constants, classes, interfaces, traits, functions, methods, and properties. Called without options, this will return a list of variables currently in scope. If a target object is provided, list properties, constants and methods of that target. If a class, interface or trait name is passed instead, list constants and methods on that class. e.g. >>> ls >>> ls $foo >>> ls -k --grep mongo -i >>> ls -al ReflectionClass >>> ls --constants --category date >>> ls -l --functions --grep /^array_.*/ >>>ファイルを保存した後、次の出力を取得します。
スクリプトの実行が停止され、Psyshがプレイするプロンプトがあります。 psyshell :: debug()のパラメーターとしてget_defined_vars()を渡しているので、シェル内のすべての定義された変数にアクセスできます:
$ウィンドウ変数を調べてみましょう:
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
アプリケーション内にpsyshを置くことについて良いことは、インスタンス化されたオブジェクトのソースコードを調べることができるということです。
composer g require psy/psysh:~0.1 psyshしたがって、$ウィンドウはsimpleWindowのインスタンスであり、ウィンドウインターフェイスを実装しています。ウィンドウインターフェイスのソースコードはどのように見えるのだろうか…
wget psysh.org/psysh chmod +x psysh ./psyshなぜSimpleWindowとDecoratedWindowが同じ出力を持っているのですか? $ decoratedWindowオブジェクトを調べてみましょう
このオブジェクトはsimpleWindowのオブジェクトよりも「重い」ため、ソースコードは長いかもしれません... render()メソッドのソースコードのみを見てみましょう。
./psysh Psy Shell v0.1.11 (PHP 5.5.8 — cli) by Justin Hileman >>>
getWindowReference()メソッドが呼び出され、render()メソッドから結果を返します。 getWindowReference()ソースを確認しましょう
このメソッドは、オブジェクトのWindowReferenceプロパティを返しています。上記のLS -ALコマンドから見たように、ACMEPATTERNSDECORATORSIMPLEWINDOWのインスタンスです。 もちろん、DecoratedWindow :: __ construct()がどのように機能するかを調べただけでしたが、これは確認できる別の方法です。 埋め込まれたサーバーを使用したデバッグ>>> 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: list, 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? 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. history Show the Psy Shell history. exit End the current session and return to caller. Aliases: quit, q
残念ながら、ApacheのようなWebサーバーをデバッグすることはサポートされていません。ただし、PHPの埋め込みサーバーを使用してアプリケーションをデバッグできます。
>>> help ls Usage: ls [--vars] [-c|--constants] [-f|--functions] [-k|--classes] [-I|--interfaces] [-t|--traits] [-p|--properties] [-m|--methods] [-G|--grep="..."] [-i|--insensitive] [-v|--invert] [-g|--globals] [-n|--internal] [-u|--user] [-C|-- category="..."] [-a|--all] [-l|--long] [target] Aliases: list, dir Arguments: target A target class or object to list. Options: --vars Display variables. --constants (-c) Display defined constants. --functions (-f) Display defined functions. --classes (-k) Display declared classes. --interfaces (-I) Display declared interfaces. --traits (-t) Display declared traits. --properties (-p) Display class or object properties (public properties by default). --methods (-m) Display class or object methods (public methods by default). --grep (-G) Limit to items matching the given pattern (string or regex). --insensitive (-i) Case-insensitive search (requires --grep). --invert (-v) Inverted search (requires --grep). --globals (-g) Include global variables. --internal (-n) Limit to internal functions and classes. --user (-u) Limit to user-defined constants, functions and classes. --category (-C) Limit to constants in a specific category (e.g. "date"). --all (-a) Include private and protected methods and properties. --long (-l) List in long format: includes class names and method signatures. Help: List variables, constants, classes, interfaces, traits, functions, methods, and properties. Called without options, this will return a list of variables currently in scope. If a target object is provided, list properties, constants and methods of that target. If a class, interface or trait name is passed instead, list constants and methods on that class. e.g. >>> ls >>> ls $foo >>> ls -k --grep mongo -i >>> ls -al ReflectionClass >>> ls --constants --category date >>> ls -l --functions --grep /^array_.*/ >>>
開発サーバーはポート8080で接続をリッスンしているため、このWebサーバー(https:// localhost:8080/decorator.php)を介してDecorator.phpファイルをリクエストするとすぐに、以下を確認する必要があります。
でやったのと同じように、psyshで遊ぶことができます
<span>>>> $a = 'hello'; </span><span>=> "hello" </span><span>>>></span>単体テストでのデバッグ
優れた開発者として、コードの単体テストを予想どおりに機能していることを証明する必要があります。プロジェクトのファイルには、テストフォルダーがあり、phpunitをインストールしている場合は、その中にテストを実行できます。
>>> function say($a) { ... echo $a; ... } => null >>> say('hello'); hello => null >>>
コードが完璧に実行されているように見えますが、テストは失敗しています。失敗したテストのみを実行することで、さらに調べることができます:
>>> class Foo ... { ... protected $a; ... ... public function setA($a) { ... $this->a = $a; ... } ... ... public function getA() { ... return $this->a; ... } ... } => null >>> $foo = new Foo(); => <Foo #000000001dce50dd000000002dda326e> {} >>> $foo->setA('hello'); => null >>> $foo->getA(); => "hello" >>>
phpunitに不慣れな場合は、コードにあまり注意を払わないでください。 一言で言えば、私はすべてを設定して、titledwindow :: addtitle()メソッドをテストし、空の値を受け取ることを期待しています。
>>> ls $foo Class Methods: getA, setA >>>では、Psyshを使用して何が起こっているのかを確認するにはどうすればよいですか?以前に行ったように、シェル:: debug()メソッドを追加するだけです
私たちはロックする準備ができています!
>>> ls -la $foo Class Properties: $a "hello" Class Methods: getA public function getA() setA public function setA($a)
$ rsには文字列が必要です。私たちが本当に持っているものを見てみましょう。
{ "name": "example/psysh", "authors": [ { "name": "John Doe", "email": "john@doe.tst" } ], "require": { "psy/psysh": "~0.1" }, "autoload": { "psr-4": {"Acme\": "src/"} } }
値、テストが失敗したのも不思議ではありません。TitledWindow:: AddTitle()のソースコードを確認しましょう。 LSコマンドを実行すると、$ TitledWindowオブジェクトを使用してそのオブジェクトのメソッドが利用可能であることがわかります。
php -a Interactive shell php > $a = 'Hello world!'; php > echo $a; Hello world! php >
バグがあります。この方法は、値を返すのではなく、値をエコーすることです。 アプリケーションは正しく機能しているように見えますが、ユニットテストとPSYSHを通じて欠陥を発見し、これを修正できます。
結論はい、PSYSHはPHPでの単体テストに非常に役立ちます。 PSYSHを使用して、テストの実行中の任意の時点で、テスト、変数を検査し、状態をインタラクティブにデバッグできます。これは、テストが失敗している理由を理解するのに特に役立ちます。
以上がPSYSHでのインタラクティブなPHPデバッグの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。