JustBlackBird / gulp-phpcs

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

PHPcs encoding issue #10

Closed tyler36 closed 9 years ago

tyler36 commented 9 years ago

I'm trying to run gulp-phpcs but have an encoding issue with the output.

I'm running Windows 7 x64 Japanese edition. I noticed the issue when running through Phpstorm 9's terminal. I tried a different terminal wrapper (ConEmu) but with the same results.

Results show fine if SSH into a Homestead 2 environment (both via PHPstore or via ConEmu).

How can I get it to display correctly in Windows?

clipboard01 PS. Screenshot of displayed error - English and Japanese both display fine in the console. PHPcs error does not.

JustBlackBird commented 9 years ago

gulp-phpcs is just a wrapper around PHPCS utility. So the first thing you should to try is run PHPCS directly via CLI. Is the problem persists?

tyler36 commented 9 years ago

I tried your suggestion.

phpcs ..\..\gulp-tasks

results in various 'errors' in several files

FILE: I:\Lavarel\testserver\gulp-tasks\gulp-tasks\sassdoc.js
----------------------------------------------------------------------
FOUND 6 ERRORS AFFECTING 6 LINES
----------------------------------------------------------------------
  1 | ERROR | [x] End of line character is invalid; expected "\n" but
    |       |     found "\r\n"
  2 | ERROR | [ ] Doc comment short description must start with a
    |       |     capital letter
----------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

however

gulp phpcs

generates bad output

[09:09:39] PHP Code Sniffer found a problem in I:\Lavarel\testserver\gulp-tasks\gulp-tasks\sassdoc.js
Message:
    Error: Command failed: '.' �́A�����R�}���h�܂��͊O���R�}���h�A
�����”\�ȃv���O�����܂��̓o�b�` �t�@�C���Ƃ��ĔF�������Ă��܂����B
[09:09:39] Finished 'phpcs' after 928 ms
JustBlackBird commented 9 years ago

Well, I cannot reproduce the issue you are talking about. Is the project you're working on an open-source one?

tyler36 commented 9 years ago

Sorry for the delay. Thanks for taking the time to help me with this.

I have created a bitbucket git repo: git@bitbucket.org: shendrix_IQ/test-gulp-phpcs.git

This was created with:

  1. Install Compose: "composer init"
  2. Install gulp-phpcs: "npm install gulp-phpcs --save-dev"
  3. Install php_codesniffer: added to composer file

"{ "require-dev": { "squizlabs/php_codesniffer": "2.*" } }"

  1. Run composer: "composer update"
  2. Install gulp: "npm install gulp"
  3. Create gulp file
  4. Add dummy test file
  5. Run: "gulp"

It's probably something stupid I'm doing wrong, but it's really annoying me trying to get it working. Any help is greatly appreciated. Thanks again for your time

JustBlackBird commented 9 years ago

First of all make sure you've installed gulp itself with npm install gulp --save-dev command.

Next, your gulp file is invalid. Here is the correct one:

var gulp = require('gulp');
var phpcs = require('gulp-phpcs');

gulp.task('default', function () {
    return gulp.src(['src/**/*.php'])
        // Validate files using PHP Code Sniffer
        .pipe(phpcs({
            bin: 'vendor/bin/phpcs',
            standard: 'PSR2',
            warningSeverity: 0
        }))
        // Log all problems that was found
        .pipe(phpcs.reporter('log'));
});

After all the corrected example works fine for me, so I don't see any problems with gulp-phpcs.

tyler36 commented 9 years ago

Thank you for the advice, unfortunately it is still not working for me I ran the npm command as you said and replaced my gulp file with the correct one.

I get the following

[09:18:45] PHP Code Sniffer found a problem in I:\gulp-phpcs\src\test.php
Message:
    Error: Command failed: 'vendor' �́A�����R�}���h�܂��͊O���R�}���h�A
�����”\�ȃv���O�����܂��̓o�b�` �t�@�C���Ƃ��ĔF�������Ă��܂����B

Thanks for you help. I guess I'll just ran the command manually.

CLOSE: unable to reproduce

JustBlackBird commented 9 years ago

One more guess: did not you try to specify full path to composer's executable with windows-style slashes?

tyler36 commented 9 years ago

OK ... been working on this for a while now.

The following works in Homestead 2, but not Win7

        .pipe(phpcs({
            bin: './vendor/bin/phpcs',

The following works in Win7, but not Homestead

        .pipe(phpcs({
            bin: '.\\vendor\\bin\\phpcs',

Solution 1: I first tried a Windows TEST to my gulpfile.js. But it felt a little hack-y and not very dynamic.

var isWin = /^win/.test(process.platform);
var phpcsBIN = (isWin) ? '.\\vendor\\bin\\phpcs' : './vendor/bin/phpcs'

gulp.task('default', function () {
    return gulp.src(['src/**/*.php'])
        .pipe(phpcs({
            bin: phpcsBIN,
...

Solution 2: I stumbled across the Nodejs PATH functions and tried that which also worked in both Link to PATH doc

var path = require('path');

gulp.task('default', function () {
    return gulp.src(['src/**/*.php'])
        .pipe(phpcs({
            bin: path.normalize('./vendor/bin/phpcs'),
...

Solution 3: Ultimately, I forked the repo and made a pull request so that this might help others.

JustBlackBird commented 9 years ago

It seems that the best option is "solution 2" you've proposed. I've described the reason why we cannot use "solution 3" here.

tyler36 commented 9 years ago

Thanks for explaining.

CLOSE: Resolved