Bytekit is a PHP extension that provides userspace access to the opcodes generated by PHP's compiler.
The bytekit-cli project develops bytekit
, a command-line tool that leverages Bytekit to perform common code analysis tasks.
bytekit
should be installed using the PEAR Installer, the backbone of the PHP Extension and Application Repository that provides a distribution system for PHP packages.
Depending on your OS distribution and/or your PHP environment, you may need to install PEAR or update your existing PEAR installation before you can proceed with the following instructions. sudo pear upgrade PEAR
usually suffices to upgrade an existing PEAR installation. The PEAR Manual explains how to perform a fresh installation of PEAR.
The following two commands (which you may have to run as root
) are all that is required to install bytekit
using the PEAR Installer:
pear config-set auto_discover 1
pear install pear.phpunit.de/hphpa
After the installation you can find the bytekit
source files inside your local PEAR directory; the path is usually /usr/lib/php/Bytekit
.
Disassemble a source file:
➜ ~ bytekit /tmp/test.php
bytekit-cli 1.1.2 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 opcodes in a source tree and write a report in PMD-XML format:
➜ ~ bytekit --rule=DirectOutput \
--rule=DisallowedOpcodes:EVAL
--xml pmd-bytekit.xml
/usr/local/src/phpunit/trunk
bytekit-cli 1.1.2 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.2">
<file name="/usr/local/src/phpunit/trunk/PHPUnit/TextUI/Command.php">
<violation rule="Disallowed opcode "EVAL"" 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 "EVAL"" line="223" class="PHPUnit_Extensions_PhptTestCase" method="run"/>
</file>
<file name="/usr/local/src/phpunit/trunk/PHPUnit/Framework/TestCase.php">
<violation rule="Disallowed opcode "EVAL"" line="1158" class="PHPUnit_Framework_TestCase" method="getMockFromWsdl"/>
<violation rule="Disallowed opcode "EVAL"" line="1059" class="PHPUnit_Framework_TestCase" method="getMock"/>
</file>
</pmd>