PHP code example of atanamo / php-codeshift

1. Go to this page and download the library: Download atanamo/php-codeshift library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

atanamo / php-codeshift example snippets




use Codeshift\AbstractCodemod;

class YourAwesomeCodemod extends AbstractCodemod {

    public function init() {
        // Do some own initializations and environment setup here
    }

    public function beforeTraversalTransform(array $statements): array {
        // Do some manual transformations here,
        // they are applied before starting the main code traversal

        return $statements;
    }

    public function afterTraversalTransform(array $statements): array {
        // Do some manual transformations here,
        // they are applied after the main code traversal was done

        return $statements;
    }
}

return 'YourAwesomeCodemod';  // Export name of the codemod class

return '\YourNamespace\YourAwesomeCodemod';

    public function init() {
        // Init the your visitor
        $visitor = new YourAwesomeVisitor();

        // Schedule a traversal run on the code that uses the visitor
        $this->addTraversalTransform($visitor);
    }

$this->addTraversalTransform($visitor1, $visitor2, $visitor3);



use Codeshift\AbstractCodemod;
use PhpParser\{Node, NodeFinder};

class YourManualCodemod extends AbstractCodemod {

    public function beforeTraversalTransform(array $statements): array {
        $nodeFinder = new NodeFinder();
        $functionNode = $nodeFinder->findFirstInstanceOf($statements, Node\Stmt\Function_::class);

        if ($functionNode != null) {
            $functionNode->name = new Node\Identifier('foobar');
        }

        return $statements;
    }
}

return 'YourManualCodemod';



$codemodPaths = [
    'path/to/my/codemod_1.php',
    'path/to/my/codemod_2.php',
    'path/to/my/codemod_3.php',
    'path/to/my/codemod_4.php'
];

$srcPath = 'my/project/src';
$outPath = 'my/project/transformed_src';
$ignorePaths = [
    'some/special/file_to_skip.php'
    'some/very/special/dir_to_skip'
];

// Execute the codemods
try {
    $runner = new \Codeshift\CodemodRunner();
    $runner->addCodemods($codemodPaths);
    $runner->execute($srcPath, $outPath, $ignorePaths);
}
catch (\Exception $ex) {
    echo "Error while executing codemods!\n";
    echo $ex->getMessage();
}



$codemod = new MySpecialCodemod();

$transformer = new \Codeshift\CodeTransformer();
$transformer->setCodemod($codemod);

// Store some PHP code to string
$codeString = getSomeCodeInput();

// Execute the codemod
try {
    echo $transformer->runOnCode($codeString);
}
catch (\Exception $ex) {
    echo "Error while executing code transformation!\n";
    echo $ex->getMessage();
}



class MySpecialTracer extends AbstractTracer {
    public function writeLine($text='') {
        echo '<p>', $text, '</p>';
    }
}

$runner = new \Codeshift\CodemodRunner('path/to/my/codemod.php', new MySpecialTracer());
$runner->executeSecured('my/project/src');
text
    php composer.phar 
text
    php composer.phar install