JustBlackBird / gulp-phpcs

Gulp plugin for running PHP Code Sniffer
MIT License
47 stars 12 forks source link

Just sniff changed file #22

Closed iamthomasbarrett closed 8 years ago

iamthomasbarrett commented 8 years ago

Hi

I'm trying to hook this into gulp watch and sniff only the changed file. Is that possible?

JustBlackBird commented 8 years ago

Sure. gulp-phpcs pass to PHPCS utility only files from the stream. So you just need to pass only updated files through the stream.

iamthomasbarrett commented 8 years ago

Is there a chance you could help me with that? I'm not sure about the syntax needed.

This is what I have:

// ### Code Sniffer
gulp.task( 'codesniff', function() {
  return gulp.src(['*.php', '!node_modules/'] )
    .pipe( phpcs({
      bin:             '/usr/local/bin/phpcs',
      standard:        'WordPress-Extra',
      extensions:      'php',
      warningSeverity: 0
    }) )
    // Log all problems that was found
  .pipe(phpcs.reporter('log'))
  .pipe(phpcs.reporter('file', { path: "/tmp/testing.log" }));
});
gulp.task('watch', function() {
  gulp.watch( ['**/*.php'], ['codesniff'] );
});
JustBlackBird commented 8 years ago

Sorry for the late answer, there were too many tasks in my TODO list.

I've though for a while about your problem and I don't get why you need to watch and validate your files. The result of gulp-phpcs plugin is a log (either printed or saved to file) with problems, so how it could be useful in development process? What is your use case?

JustBlackBird commented 8 years ago

I've thought for a while and finally make a solution for you:

var gulp = require('gulp'),
    phpcs = require('gulp-phpcs'),
    changed = require('gulp-changed-in-place');

var phpSrc = './src/*.php';

gulp.task('codesniff', function() {
  return gulp.src(phpSrc)
    .pipe(changed({firstPass: true}))
    .pipe( phpcs({
      bin: './vendor/bin/phpcs',
      standard: 'PSR2',
      extensions: 'php',
      warningSeverity: 0
    }))
    // Log all problems that was found
  .pipe(phpcs.reporter('log'))
  .pipe(phpcs.reporter('file', { path: './testing.log'}));
});

gulp.task('watch', ['codesniff'], function() {
  gulp.watch(phpSrc, ['codesniff']);
});

Notice, I've changed some environment values with my own, so you should change them back :wink:

iamthomasbarrett commented 8 years ago

Thank you.

My config looks like this:

var phpSrc = '**/*.php';

gulp.task( 'codesniff', function() {
  return gulp.src( [phpSrc, '!node_modules/'] )
    .pipe( changed_in_place( { firstPass: true } ) )
    .pipe( phpcs({
      bin:             '/usr/local/bin/phpcs',
      standard:        'WordPress-Extra',
      extensions:      'php',
      warningSeverity: 0
    }) )
    .pipe( phpcs.reporter( 'log' ) );
});

It increases the time it takes from 25ms to 3.5s - is there a way I can make it faster?

JustBlackBird commented 8 years ago

Well, I see only one thing you could optimize here. Try to use more specific source path instead of just **/*.php. It seems that all third-party stuff (brought by composer) are checked too.

iamthomasbarrett commented 8 years ago

A little long winded, but much quicker.

var phpSrc = [
        '*.php',                                // Root theme dir
        'inc?(lude|ludes)/**/*.php',            // /inc/ /include/ /incudes/
        'function?(s)/**/*.php',                // /function/ /functions/
        'template?(s|-part|-parts)/**/*.php',   // /template/ /templates/ /template-part/ /template-parts/
        'framework?(s)/**/*.php',               // /framework/ /framewoks/
        'lib/**/*.php'                          // /lib/
        ];

Perhaps being explicit is actually better.

Thanks!