2

I am writing a PHP command line app which generates some PHP code and wants to pass it thought the code style fixer before storing it to a file.

Is it possible to use php-cs-fixer as a library and call it from the code ?

If no, is there any other solution to ensure that the generated code respects the PSR-2 standard ?

Thanks,

webNeat
  • 2,768
  • 1
  • 20
  • 22
  • Yes, it is. `magento/magento2` wants it to happen, see https://github.com/magento/magento2/pull/10770, but I really wouldn't recommend it. If you do, however, take a look at the comment in this PR by Dariusz Ruminski, the maintainer of `friendsofphp/php-cs-fixer`: https://github.com/magento/magento2/pull/10770#issuecomment-327829883 – localheinz Sep 19 '17 at 11:58

1 Answers1

1

I think the easiest way is to:

  1. Put your generated code into a temporary file.
  2. Copy php-cs-fixer binary contents except the last line (second link here).
  3. Application::setAutoExit(false).
  4. call Application:run() with ArrayInput and BufferedOutput and check return value (should be zero).

Or just shell_exec a whole command with its arguments.

Tns
  • 390
  • 1
  • 7