funivan/php-tokenizer

Wrapper around token_get_all. Easy to extract and modify php code.

0.3.0 2020-01-20 09:23 UTC

README

GitHub tag Software License Total Downloads

Wrapper around token_get_all. Easy to extract and modify tokens

Install

Via Composer

composer require funivan/php-tokenizer

Usage

Reformat our code like PhpStorm. Lets create rule: place single spaces after while

  use Funivan\PhpTokenizer\Collection;
  use Funivan\PhpTokenizer\Pattern\PatternMatcher;
  use Funivan\PhpTokenizer\QuerySequence\QuerySequence;


  $source = "<?php while(){}"; // while (){}
  
  $collection = Collection::createFromString($source);
  
  (new PatternMatcher($collection))->apply(function (QuerySequence $checker) {

    $while = $checker->strict('while');
    $space = $checker->possible(T_WHITESPACE);

    if ($checker->isValid()) {
      $space->remove();
      $while->appendToValue(" ");
    }

  });

  echo (string) $collection;
   

Documentation

Documentation

Testing

./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.