PHPackages                             atanamo/php-codeshift - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. atanamo/php-codeshift

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

atanamo/php-codeshift
=====================

A PHP code transformation toolkit based on 'PHP-Parser'

v1.0.5(3y ago)32158.4k↓31.6%11BSD-3-ClausePHPPHP &gt;=7.0

Since Apr 16Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Atanamo/PHP-Codeshift)[ Packagist](https://packagist.org/packages/atanamo/php-codeshift)[ RSS](/packages/atanamo-php-codeshift/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (7)Used By (1)

PHP-Codeshift
=============

[](#php-codeshift)

PHP-Codeshift\* is a lightweight wrapper for the excellent library [PHP-Parser](https://github.com/nikic/PHP-Parser). It mainly provides an easy-to-use API for running own codemod definition files on multiple PHP source files.

\* Yes, the name is totally stolen from project "[jscodeshift](https://github.com/facebook/jscodeshift)" ;-)

Table of contents
=================

[](#table-of-contents)

- [Requirements](#requirements)
- [Features](#features)
- [Install &amp; Run](#install--run)
- [CLI usage](#cli-usage)
- [Writing a codemod](#writing-a-codemod)
    - [Codemod file](#codemod-file)
    - [Ways to transform](#ways-to-transform)
    - [Traversal transformation](#traversal-transformation)
    - [Manual transformation](#manual-transformation)
- [Programmable API](#programmable-api)
    - [CodemodRunner](#codemodrunner)
    - [CodeTransformer](#codetransformer)
    - [AbstractTracer: Custom logging](#abstracttracer-custom-logging)

Requirements
============

[](#requirements)

Due to dependencies to [PHP-Parser](https://github.com/nikic/PHP-Parser) 4.x, following PHP versions are required:

- Running codeshift: PHP 7.0 or higher
- Target code to be transformed: PHP 5.2 or higher

Features
========

[](#features)

- Simple CLI for:
    - Dumping the AST of a file to stout or an output file
    - Transforming a single source file using a codemod
    - Transforming sources in a directory tree using a codemod
- Clear API for writing a codemod
- Clear API for executing codemods programmatically

Install &amp; Run
=================

[](#install--run)

The easiest way to install Codeshift is to add it to your project using [Composer](https://getcomposer.org).

1. Require the library as a dependency using Composer:

    ```
    php composer.phar require atanamo/php-codeshift

    ```
2. Install the library:

    ```
    php composer.phar install

    ```
3. Execute the Codeshift CLI (Print help):

    ```
    vendor/bin/codeshift --help

    ```

CLI usage
=========

[](#cli-usage)

To execute a codemod file on a source directory:

```
vendor/bin/codeshift --mod=/my/codemod.php --src=/my/project/src

```

Use `--out` to not change original source:

```
vendor/bin/codeshift --mod=/my/codemod.php --src=/my/project/src --out=/transformed/src

```

Dump AST to file:

```
vendor/bin/codeshift --ast=/my/script.php --out=/my/script_ast.txt

```

Note: If you are on windows, you may need to use `call` to access the binary:

```
call vendor/bin/codeshift ...

```

Writing a codemod
=================

[](#writing-a-codemod)

Codemod file
------------

[](#codemod-file)

A codemod is a PHP file that defines the transformations to do on your PHP source code / source file.

The only thing needed in the file is to export a class derived from `Codeshift\AbstractCodemod`. This class can define/override one or more of the provided hook methods:

```
