CI Check - Code sniffer
nikophil opened this issue · 3 comments
this issue refers to the point 3 of issue #10
Run php-cs-fixer on all code blocks
@weaverryan Don't you mean php-cs instead of php-cs-fixer ? i think we're not able to auto-fix the .rst file, but we need to throw an error if the code is not properly formatted...
i've started to play with phpcs
it's actually quite a pain in the ass to use phpcs without the binary and impossible to run the checks just on a piece of code, and not on an actual file.
Here is some code which is working, just have now to add the right ruleset:
$tempFile = tempnam(sys_get_temp_dir(), 'symfony-docs-builder') .'.php';
file_put_contents($tempFile, strpos($code, '<?php') === 0 ? $code : "<?php\n".$code);
if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', false);
}
if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {
define('PHP_CODESNIFFER_VERBOSITY', 2);
}
$config = new PHP_CodeSniffer\Config([], false);
$ruleset = new PHP_CodeSniffer\Ruleset($config);
$file = new PHP_CodeSniffer\Files\File($tempFile, $ruleset, $config);
$file->setContent(file_get_contents($tempFile));
$file->process();
dump($file->getErrors());we cannot use the main Runner because it's only reading conf from stdin. Then it forces us to define some constants...
I'm wondering if it wouldn't be more relevant to use some Process and run the actual phpcs binary...
Another thing: i've put the call of this code in CodeNodeRenderer which is currently the only "easy" place i've found to do that, but that's not good because we don't have access to the environment there, and then we cannot create accurate errors for the user. Maybe we shall do that in some listener... i don't know yet
cheers
I was actually referring to https://github.com/FriendsOfPHP/PHP-CS-Fixer
The main reason is that this is what's used on the symfony repos to check cs and so it has built-in "Symfony" rules. However, it doesn't do JavaScript or Twig or anything else. So, I don't really care what tool we use. I'm also not sure if it's any easier to use standalone - we use in in the tests for MakerBundle, but by executing the binary.
Btw, whatever we use, if it's much easier to write a temporary file, run the executable, then delete the temporary file, no huge deal I think (and we could change it later)
ok that's nice if we have some tool that comes from symfony rules out of the box, that's something less to worry about
Btw, whatever we use, if it's much easier to write a temporary file, run the executable, then delete the temporary file, no huge deal I think (and we could change it later)
it's actually exactly what i have done in my POC ;)
closing this one in favor of https://github.com/weaverryan/docs-builder/issues/7