-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What to replace PHP_CodeSniffer_CLI() with? #2035
Comments
Here is an example of a custom runner: https://gist.github.com/gsherwood/aafd2c16631a8a872f0c4a23916962ac It processes a PHP string using a dummy file object and grabs the report output itself, but you could also just call $runner->run() after setting up the config object to have it process files and reports as normal. |
Thanks, I'll try that. |
Ok, so I tried with this: class Checks {
...
public function run_sniffer {
...
$ignored = array(
'.*/node_modules/.*',
'.*/vendor/.*',
'.*/assets/build/.*',
'.*/build/.*',
'.*/bin/.*',
);
// Set up a PHPCS Runner and Config.
$runner = new Runner();
// Set default standard.
\PHP_CodeSniffer\Config::setConfigData( 'default_standard', 'WordPress-Theme', true );
// Ignoring warnings when generating the exit code.
\PHP_CodeSniffer\Config::setConfigData( 'ignore_warnings_on_exit', true, true );
// Show only errors?
\PHP_CodeSniffer\Config::setConfigData( 'show_warnings', $show_warnings, true );
// Set minimum supported PHP version.
\PHP_CodeSniffer\Config::setConfigData( 'testVersion', $minimum_php_version . '-', true );
// Set text domains.
\PHP_CodeSniffer\Config::setConfigData( 'text_domain', implode( ',', $args['text_domains'] ), true );
// Path to WordPress Theme coding standard.
\PHP_CodeSniffer\Config::setConfigData( 'installed_paths', WP_PLUGIN_DIR . '/' . $this->plugin_name . '/vendor/wp-coding-standards/wpcs/', true );
$runner->config = new Config();
$runner->init();
$runner->config->files = implode( ',', $all_files );
$runner->config->standards = $standards_array;
$runner->config->annotations = $ignore_annotations;
$runner->config->verbosity = 1;
$runner->config->parallel = true;
$runner->config->colors = false;
$runner->config->showProgress = true;
$runner->config->reportWidth = 110;
$runner->config->ignored = $ignored;
$report = $runner->run();
error_log( print_r( $report, true ) );
}
} I'm calling the But I'm getting errors
Some of the variables are set up on the start via I must be doing something wrong with the config. Also, should I use |
Sorry, I didn't realise you were running this outside the CLI environment. In that case, you need to pass something to the Config class constructor for the first argument. That first argument is where you would pass in a fake But you could also just use it to pass in a single command line arg and set the others the way you are already doing. So something like |
Awesome will test that. Thanks! |
Ok, this fixed the
When runing the sniffer, which is odd. I'll try to test a bit more, to see if the issue isn't with the |
I don't know why any code would be trying to include test files. That only happens during a unit test run, and you can't kick one of those off without using phpunit. Do you have something that might be including all files inside the PHPCS directory and catching test files as well? |
Not sure. I'm autoloading files using a classmap, and maybe I loaded something I shouldn't have. I added use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Config; In the file I'm using the I'll try to modify it and see if I'll get the error. |
PHPCS has it's own autoloader, so you'll probably want to include that. See the original gist I linked for an example of that. |
Yeah, I wanted to automate it, by not having to manually use |
Ok, the only way I can get it to run is by setting $_SERVER['argv'] = array( '-v' );
$runner->config = new Config();
$runner->init(); And that returns 3 when I do $report = $runner->runPHPCS();
error_log( print_r( $report, true ) ); I'll try to set manually through the |
@dingo-d Any luck? Do you still need help or should I close this? |
It looks like my config isn't being passed when I just do the So I tried some hacks like this $class = new \ReflectionClass( $runner );
$run = $class->getMethod( 'run' );
$run->setAccessible( true );
$run->invoke( $runner );
$report = $runner->run();
$output = ob_get_clean(); And with that I can get some of the config in the PHP_CodeSniffer, but I doubt I should be using this. At first I thought that the files aren't passed down correctly (path issue), but that wasn't it. Any idea why the config isn't working? The PR I'm working on is this: WPTT/theme-sniffer#83, and my branch is: https://github.com/dingo-d/theme-sniffer/tree/feature/use-namespace |
You can pass an array of CLI args to the Config constructor. If you do this, it wont go searching for |
Oh, so basically, instead of doing this $runner->config = new Config();
$runner->init();
$runner->config->files = implode( ',', $all_files );
$runner->config->standards = $standards_array;
$runner->config->annotations = $ignore_annotations;
$runner->config->verbosity = 1;
$runner->config->parallel = true;
$runner->config->colors = false;
$runner->config->showProgress = true;
$runner->config->reportWidth = 110;
$runner->config->ignored = $ignored; I should add the config inside the |
I think we've come full circle :) This previous comment is what I think you should be trying:
So you could either pass everything into the constructor, or pass at least one option and then continuing setting config vars using |
I'll test it this weekend and report back :) |
I think I narrowed down the problem. I am calling the phpcs with So I did it like this: $ignored = '.*/node_modules/.*,.*/vendor/.*,.*/assets/build/.*,.*/build/.*,.*/bin/.*';
ob_start();
// Set up a PHPCS Runner and Config.
$runner = new Runner();
// Set default standard.
Config::setConfigData( 'default_standard', 'WordPress-Theme', true );
// Ignoring warnings when generating the exit code.
Config::setConfigData( 'ignore_warnings_on_exit', true, true );
// Show only errors?
Config::setConfigData( 'show_warnings', $show_warnings, true );
// Set minimum supported PHP version.
Config::setConfigData( 'testVersion', $minimum_php_version . '-', true );
// Set text domains.
Config::setConfigData( 'text_domain', implode( ',', $args['text_domains'] ), true );
if ( $theme_prefixes !== '' ) {
// Set prefix.
Config::setConfigData( 'prefixes', $theme_prefixes, true );
}
// Path to WordPress Theme coding standard.
Config::setConfigData( 'installed_paths', WP_PLUGIN_DIR . '/' . $this->plugin_name . '/vendor/wp-coding-standards/wpcs/', true );
$standards_list = implode( ',', $standards_array );
$cliArgs[] = "--standard={$standards_list}"; // List of standards.
// Ignora annotations.
if ( $ignore_annotations ) {
$cliArgs[] = '--ignore-annotations';
}
$cliArgs[] = '-vv'; // Verbosity.
$cliArgs[] = '--parallel=5'; // Number of files to run in parallel.
$cliArgs[] = '--no-colors'; // No colors on the output.
$cliArgs[] = '-p'; // Show progress.
$cliArgs[] = '--report-width=110'; // Report width.
$cliArgs[] = "--ignore={$ignored}"; // List of ignored patterns.
$cliArgs[] = implode( ',', $all_files ); // List of files to sniff.
$runner->config = new Config( $cliArgs );
$runner->init();
$report = $runner->runPHPCS();
$output = ob_get_clean(); And now I see this error
EDIT I added if ( defined( 'PHP_CODESNIFFER_CBF' ) === false ) {
define( 'PHP_CODESNIFFER_CBF', false );
}
if ( defined( 'PHP_CODESNIFFER_VERBOSITY' ) === false ) {
define( 'PHP_CODESNIFFER_VERBOSITY', 2 );
} After the
|
You can't call runPHPCS when you are writing your own runner. That method is what kicks off the main CLI processing code. That's why my original sample gist didn't do that either. You'll need to replicate the parts of functionality you want in your own code. |
Oh, basically, I'll have to see how that method works, and copy its behavior in my class. Thanks for the help, I'll try to see what else I need. |
@dingo-d Just checking in to see if you need more help. This is a long running issue so I'd like to try and resolve it so I can close it. Thanks. |
I managed to get something by packing the CLI arguments in an array and then passing it to the Thanks for all the suggestions, they helped :) |
No problem. |
I'm rewriting the Theme Sniffer plugin, and I'm getting errors due to missing
PHP_CodeSniffer_CLI()
class.https://github.com/WPTRT/theme-sniffer/blob/a0af96a9f5217c11b04a22691d56e60c6ff257e1/inc/checks.php#L72
Now from what I gathered I should be using
\PHP_CodeSniffer\Runner()
class, but I'm unsure how to replace it exaclty.The old way was:
Now I'm trying
And I've set some config like
\PHP_CodeSniffer\Config::setConfigData( 'show_warnings', absint( $args['show_warnings'] ), true );
Do I need to set all CLI arguments in that way? Before there was an array
$values
that got passed to theprocess()
method. Should I just set all the config using\PHP_CodeSniffer\Config::setConfigData
?The text was updated successfully, but these errors were encountered: