Skip to content
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

Batch process files in a PHP app? #2207

Closed
dingo-d opened this issue Oct 28, 2018 · 3 comments
Closed

Batch process files in a PHP app? #2207

dingo-d opened this issue Oct 28, 2018 · 3 comments

Comments

@dingo-d
Copy link

dingo-d commented Oct 28, 2018

This issue connects to the #2035 issue. I made some progress in the meantime, but I'm still not getting any results.

I'm rewriting the WordPress theme sniffer plugin and in my run_sniffer() ajax callback method I have

public function run_sniffer() {
  // A lot of settings I get from the plugin and from what user selects.

  $runner = new Runner();

  $runner->config            = new Config( [ '-s' ] );
  $runner->config->standards = $standards_array;

  // Set default standard.
  Config::setConfigData( 'default_standard', 'WPThemeReview', 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 );
  }

  $runner->config->files       = array_values( $all_files );
  $runner->config->annotations = $ignore_annotations;
  $runner->config->parallel    = 32;
  $runner->config->colors      = false;
  $runner->config->tabWidth    = 0;
  $runner->config->reportWidth = 110;
  $runner->config->interactive = false;
  $runner->config->cache       = false;
  $runner->config->ignored     = $ignored;

  $runner->init();

  $runner->reporter = new Reporter( $runner->config );

  \wp_send_json_success( $runner->reporter->printReports() );
}

What I get back from the ajax is {"success":true,"data":true}.

I logged the config and runner class, and the config is picked up

log output when adding error_log( print_r( $this->config, true ) ); in init() method of Runner.php
[28-Oct-2018 09:48:33 UTC] PHP_CodeSniffer\Config Object
(
    [settings:PHP_CodeSniffer\Config:private] => Array
        (
            [files] => Array
                (
                    [0] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/404.php
                    [1] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/archive.php
                    [2] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/customize-controls.js
                    [3] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/customize-preview.js
                    [4] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/global.js
                    [5] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/html5.js
                    [6] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js
                    [7] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/navigation.js
                    [8] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js
                    [9] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/comments.php
                    [10] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/footer.php
                    [11] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/front-page.php
                    [12] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/functions.php
                    [13] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/header.php
                    [14] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/back-compat.php
                    [15] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/color-patterns.php
                    [16] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/custom-header.php
                    [17] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/customizer.php
                    [18] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/icon-functions.php
                    [19] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/template-functions.php
                    [20] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/inc/template-tags.php
                    [21] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/index.php
                    [22] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/page.php
                    [23] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/search.php
                    [24] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/searchform.php
                    [25] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/sidebar.php
                    [26] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/single.php
                    [27] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/footer/footer-widgets.php
                    [28] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/footer/site-info.php
                    [29] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/header/header-image.php
                    [30] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/header/site-branding.php
                    [31] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/navigation/navigation-top.php
                    [32] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/page/content-front-page-panels.php
                    [33] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/page/content-front-page.php
                    [34] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/page/content-page.php
                    [35] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content-audio.php
                    [36] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content-excerpt.php
                    [37] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content-gallery.php
                    [38] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content-image.php
                    [39] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content-none.php
                    [40] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content-video.php
                    [41] => /srv/www/personal/wordpress-plugin/public_html/wp-content/themes/twentyseventeen/template-parts/post/content.php
                )

            [standards] => Array
                (
                    [0] => WPThemeReview
                )

            [verbosity] => 0
            [interactive] => 
            [parallel] => 32
            [cache] => 
            [cacheFile] => 
            [colors] => 
            [explain] => 
            [local] => 
            [showSources] => 1
            [showProgress] => 
            [quiet] => 
            [annotations] => 
            [tabWidth] => 0
            [encoding] => utf-8
            [extensions] => Array
                (
                    [php] => PHP
                    [inc] => PHP
                    [js] => JS
                    [css] => CSS
                )

            [sniffs] => Array
                (
                )

            [exclude] => Array
                (
                )

            [ignored] => .*/node_modules/.*,.*/vendor/.*,.*/assets/build/.*,.*/build/.*,.*/bin/.*
            [reportFile] => 
            [generator] => 
            [filter] => 
            [bootstrap] => Array
                (
                )

            [reports] => Array
                (
                    [full] => 
                )

            [basepath] => 
            [reportWidth] => 110
            [errorSeverity] => 5
            [warningSeverity] => 5
            [recordErrors] => 1
            [suffix] => 
            [stdin] => 
            [stdinContent] => 
            [stdinPath] => 
            [unknown] => Array
                (
                )

        )

    [dieOnUnknownArg] => 1
    [cliArgs:PHP_CodeSniffer\Config:private] => Array
        (
            [0] => -s
        )

)

It looks like no report is being generated.

If I add

$runner->runPHPCS();

after the init() I get errors, as this deletes the config and it will report missing argv parameters and such.

I'm trying to sniff everything at once (that's why I'm using the parallel option). I'd like to avoid the previous way where I ran the sniffer every time on a single file (which takes a long time to process and is memory expensive).

Also I'm not sure Config::setConfigData parts in my code are being picked up in the runner.

I'm not sure what part am I missing here.

@dingo-d
Copy link
Author

dingo-d commented Nov 4, 2018

Ok, I think I finally understood what @gsherwood meant by 'using your own runner'. After some debugging I realized that I need to add this to my code

$runner->reporter = new Reporter( $runner->config );

foreach ( $all_files as $file_path ) {
  $file = new DummyFile( file_get_contents( $file_path ), $runner->ruleset, $runner->config );
  $file->path = $file_path;

  $runner->processFile( $file );
}

Which works, and I'm getting the results. I'm just wondering does this means that the parallel option is useless, no?

@gsherwood
Copy link
Member

Sorry, I haven't had time to look into this. Work is really crazy so I'm not sure when I will.

But I can say that you understood what I meant by doing your own runner. And yes, if you do this, you can't use the parallel feature of the main runner - you'd have to implement that yourself.

@dingo-d
Copy link
Author

dingo-d commented Nov 5, 2018

Awesome. I'll look into the implementation of the parallel option, for now, I don't think it's of high importance, but I'll add it to my to-do list 🙂

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants