sebastianbergmann / bytekit-cli

A command-line tool built on the PHP Bytekit extension.

This URL has Read+Write access

Fri Nov 06 02:45:07 -0800 2009
commit  35ed76548d57383cf2394cfdb1f2d3c61e1e33c0
tree    2b4e511a7503e5824c518458877d454f4e5834c9
parent  e14aa1ec690649e72a733d532ff0b42b4ace4879
name age message
directory Bytekit/ Loading commit data...
file README.markdown
file bytekit.bat Sat Jun 06 23:13:16 -0700 2009 Initial import. [sebastianbergmann]
file bytekit.php Mon Jun 08 00:06:03 -0700 2009 Consistently use require_once. [sebastianbergmann]
directory examples/ Tue Jun 30 02:13:47 -0700 2009 Add dead code example. [sebastianbergmann]
file package.xml
README.markdown

bytekit-cli

Bytekit is a PHP extension that provides userspace access to the opcodes generated by PHP's compiler.

bytekit-cli is a command-line tool that leverages Bytekit to perform common code analysis tasks.

Installation

bytekit-cli should be installed using the PEAR Installer. This installer is the backbone of PEAR, which provides a distribution system for PHP packages, and is shipped with every release of PHP since version 4.3.0.

The PEAR channel (pear.phpunit.de) that is used to distribute bytekit-cli needs to be registered with the local PEAR environment:

sb@ubuntu ~ % pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded

This has to be done only once. Now the PEAR Installer can be used to install packages from the PHPUnit channel:

sb@ubuntu ~ % pear install phpunit/bytekit
downloading bytekit-1.1.0.tgz ...
Starting to download bytekit-1.1.0.tgz (10,295 bytes)
.....done: 10,295 bytes
downloading File_Iterator-1.0.0.tgz ...
Starting to download File_Iterator-1.0.0.tgz (2,353 bytes)
...done: 2,353 bytes
install ok: channel://pear.phpunit.de/File_Iterator-1.0.0
install ok: channel://pear.phpunit.de/bytekit-1.1.0

After the installation you can find the bytekit-cli source files inside your local PEAR directory; the path is usually /usr/lib/php/Bytekit.

Usage Examples

Disassemble a source file:

sb@ubuntu bytekit-cli % bytekit /tmp/test.php
bytekit-cli 1.1.0 by Sebastian Bergmann.

Filename:           /tmp/test.php
Function:           main
Number of oplines:  13
Compiled variables: !0 = $a, !1 = $b, !2 = $c

  line  #     opcode                           result  operands
  -----------------------------------------------------------------------------
  2     0     EXT_STMT
        1     ASSIGN                                   !0, 1
  3     2     EXT_STMT
        3     ASSIGN                                   !1, 2
  4     4     EXT_STMT
        5     ADD                              ~2      !0, !1
        6     PRINT                            ~3      ~2
        7     FREE                                     ~3
  5     8     EXT_STMT
        9     JMPZ                                     !0, ->12

        10    ASSIGN                                   !2, 3
        11    JMP                                      ->12

  6     12    RETURN                                   1

Scan for unwanted mnemonics in a source tree and write a report in PMD-XML format:

sb@ubuntu ~ % bytekit --rule=DirectOutput \
                      --rule=DisallowedOpcodes:EVAL
                      --xml pmd-bytekit.xml
                      /usr/local/src/phpunit/trunk
bytekit-cli 1.1.0 by Sebastian Bergmann.

  - Disallowed opcode "EVAL"
    in /usr/local/src/phpunit/trunk/PHPUnit/TextUI/Command.php:177

  - Direct output of variable $buffer
    in /usr/local/src/phpunit/trunk/PHPUnit/TextUI/TestRunner.php:468

  - Direct output of variable $buffer
    in /usr/local/src/phpunit/trunk/PHPUnit/Util/Printer.php:173

  - Direct output of variable $message
    in /usr/local/src/phpunit/trunk/PHPUnit/Extensions/Database/UI/Mediums/Text.php:130

  - Disallowed opcode "EVAL"
    in /usr/local/src/phpunit/trunk/PHPUnit/Extensions/PhptTestCase.php:223

  - Disallowed opcode "EVAL"
    in /usr/local/src/phpunit/trunk/PHPUnit/Framework/TestCase.php:1158

  - Disallowed opcode "EVAL"
    in /usr/local/src/phpunit/trunk/PHPUnit/Framework/TestCase.php:1059

The report looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<pmd version="bytekit-cli 1.1.0">
  <file name="/usr/local/src/phpunit/trunk/PHPUnit/TextUI/Command.php">
    <violation rule="Disallowed opcode &quot;EVAL&quot;" line="177" class="PHPUnit_TextUI_Command" method="run"/>
  </file>
  <file name="/usr/local/src/phpunit/trunk/PHPUnit/TextUI/TestRunner.php">
    <violation rule="Direct output of variable $buffer" line="468" class="PHPUnit_TextUI_TestRunner" method="write"/>
  </file>
  <file name="/usr/local/src/phpunit/trunk/PHPUnit/Util/Printer.php">
    <violation rule="Direct output of variable $buffer" line="173" class="PHPUnit_Util_Printer" method="write"/>
  </file>
  <file name="/usr/local/src/phpunit/trunk/PHPUnit/Extensions/Database/UI/Mediums/Text.php">
    <violation rule="Direct output of variable $message" line="130" class="PHPUnit_Extensions_Database_UI_Mediums_Text" method="output"/>
  </file>
  <file name="/usr/local/src/phpunit/trunk/PHPUnit/Extensions/PhptTestCase.php">
    <violation rule="Disallowed opcode &quot;EVAL&quot;" line="223" class="PHPUnit_Extensions_PhptTestCase" method="run"/>
  </file>
  <file name="/usr/local/src/phpunit/trunk/PHPUnit/Framework/TestCase.php">
    <violation rule="Disallowed opcode &quot;EVAL&quot;" line="1158" class="PHPUnit_Framework_TestCase" method="getMockFromWsdl"/>
    <violation rule="Disallowed opcode &quot;EVAL&quot;" line="1059" class="PHPUnit_Framework_TestCase" method="getMock"/>
  </file>
</pmd>