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);
}
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';
$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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.