Download the PHP package atanamo/php-codeshift without Composer

On this page you can find all versions of the php package atanamo/php-codeshift. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-codeshift

PHP-Codeshift

PHP-Codeshift* is a lightweight wrapper for the excellent library 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" ;-)

Table of contents

Requirements

Due to dependencies to PHP-Parser 4.x, following PHP versions are required:

Features

Install & Run

The easiest way to install Codeshift is to add it to your project using Composer.

  1. Require the library as a dependency using Composer:

  2. Install the library:

  3. Execute the Codeshift CLI (Print help):

CLI usage

To execute a codemod file on a source directory:

Use --out to not change original source:

Dump AST to file:

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

Writing a codemod

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:

Please see source file <codeshift>/src/AbstractCodemod.php for detail documentation of the AbstractCodemod class.

Also see <codeshift>/samples/foobar_codemod.php for an example codemod.

If you declare the codemod class in an own namespace, do not forget to export/return it correctly:

Ways to transform

Basically, you can differ two ways of how to transform your code:

  1. Traversal transformation using so-called "Node visitors"
  2. Manual transformation using hard-core API

For traversal transformation you almost always only need the init hook of a codemod. The before and after hooks are intended for the more specific tasks of a manual transformation. But of course you can mix them as you need.

For the transformations themselves you will have to rely heavily on the API of PHP-Parser.

Traversal transformation

Traversal transformation means to transform the AST while traversing it. The transformation itself is defined by one or more "node visitors". The visitors are applied to each node of the AST.

Please see the excellent documentation of PHP-Parser for this: PHP-Parser/doc/component/Walking_the_AST

While PHP-Parser refers to the class PhpParser\NodeTraverser for applying a visitor, PHP-Codeshift provides some convenient mechanism for this.

Normally you only have to add your visitor in the init hook of your codemod:

Each call to addTraversalTransform() will create one PhpParser\NodeTraverser internally, which is then used to do one traversal run on the AST.

If you want to use multiple visitors on one single run, just pass them all:

You may define the visitor classes in the codemod file or require them from other files.

Note that each of your visitors must implement the interface PhpParser\NodeVisitor. Therefor it's recommended to simply extend the abstract class PhpParser\NodeVisitorAbstract. Do not forget to use the correct namespace.

Manual transformation

Manual transformation includes everything that differs from traversal transformation.

You can use the hook methods beforeTraversalTransform and/or afterTraversalTransform to manipulate the passed statement nodes as you like.

In that case, the AST as a whole is represented by the statement nodes. Therefor, to simplify finding specific nodes of the AST, the PHP-Parser library provides the PhpParser\NodeFinder class.

The documentation of PHP-Parser contains some examples of how to use the NodeFinder: PHP-Parser/doc/component/Walking_the_AST#simple-node-finding

The following is an example in the context of a codemod. It simply searches for the first function definition in the AST and renames the found function to "foobar":

Programmable API

While in most cases it will be sufficient to use the CLI, the library offers a few useful classes for manual execution of codemods.

Mainly these are:

CodemodRunner

The CodemodRunner is the most convienent way to execute one or more codemods by using their file names.

The following example shows how to use the CodemodRunner for executing 4 different codemods sequentially by an automated routine:

Please see the class documentation for further methods and details.

CodeTransformer

The CodeTransformer provides some more basic routines for executing a codemod. It is used by the CodemodRunner internally.

The following example uses the CodeTransformer to transform code from a string that is fetched by a custom function.

Please see the class documentation for further methods and details.

AbstractTracer: Custom logging

If you use the API classes, they generate some logs to STDOUT by default. For instance, the execution of a codemod is logged and each transformation of a file.

These logs are generated by the SimpleOutputTracer, which is the default implementation for the AbstractTracer class.

You can implement your own "Tracer" by deriving the AbstractTracer and providing it to the CodemodRunner or CodeTransformer.

This allows you to log processing steps to e.g. a special stream. You may also implement a kind of adapter by this, which provides information for continuous-integration frameworks or similar.

The following example defines an own "Tracer" and passes it to the CodemodRunner:

The custom tracer MySpecialTracer simply writes all output as HTML paragraphs. Also note that the example uses executeSecured here, to even log exceptions by the custom tracer.

From a practical point of view, it might be more useful to override other methods with custom implementations. Here are a few examples:

Please see the class documentation for further methods and details.


All versions of php-codeshift with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0
nikic/php-parser Version ^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package atanamo/php-codeshift contains the following files

Loading the files please wait ....