squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

Travis won't work with custom PHP Standard #2070

Closed gamcoh closed 6 years ago

gamcoh commented 6 years ago

I'm new at this, but I tried to add phpcs with Travis and it won't work. It seems that it's because i made my own PHP Standard from the code of the PEAR Standard. Is there maybe a way to tell Travis to look into the src/Standard folder in order to see mine

.travis.yml:
script:
  - ./vendor/bin/phpcs ./
Travis build:
$ ./vendor/bin/phpcs ./
ERROR: Referenced sniff "Gampear.Commenting.ClassComment" does not exist
dingo-d commented 6 years ago

Works for me 🤷‍♂️

https://github.com/infinum/wp-boilerplate/blob/master/.travis.yml

you just need to specify the custom standard manually.

before_script:
  - export PATH="$HOME/.composer/vendor/bin:$PATH"
  - if [[ "$SNIFF" == "1" ]]; then
      composer global require wp-coding-standards/wpcs;
      phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs;
      phpcs --config-set standard infinum;
    fi

script:
  # Search theme for PHP syntax errors.
  - find . \( -name '*.php' \) -exec php -lf {} \;
  - |
    if [[ "$SNIFF" == "1" ]]; then
      composer global require wp-coding-standards/wpcs;
      phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs;
      phpcs --config-set standard infinum;
    fi
jrfnl commented 6 years ago

@dingo-d Looks like you're doing the same thing twice and not actually running the standard ?

As you're using Composer, why not use dealerdirect/phpcodesniffer-composer-installer ? That will sort the installed_paths out for your automatically.

jrfnl commented 6 years ago

@gamcoh To answer your question, more information is needed on where you have put the Gampear sniff files.

dingo-d commented 6 years ago

@jrfnl Yeah, it does seem that way, I've written this some time ago, will have to refactor it a bit. Although I'd say it was working. 🤔

jrfnl commented 6 years ago

@dingo-d Only for setting the path, you are not actually running the standard with the commands shown.

dingo-d commented 6 years ago

Ok, I'm writing a PR 😂 Thanks for the tip, have I told you how awesome you are? :smile:

gamcoh commented 6 years ago

@jrfnl I have put the sniff files under vendor/squizlabs/php_codesniffer/src/Standards/Gampear

dingo-d commented 6 years ago

Juliette made awesome examples that you can check here: https://github.com/jrfnl/make-phpcs-work-for-you :)

jrfnl commented 6 years ago

I have put the sniff files under vendor/squizlabs/php_codesniffer/src/Standards/Gampear

Ok, first off: it would probably be a better idea to have your sniffs in a separate project/repo to prevent issues when updating PHPCS.

Next, either way, whether you have the sniffs in a separate project or where they are now, you will need to register your ruleset with PHPCS using the installed_paths command.

Run phpcs --config-show to see if there's anything in installed_paths at this moment. Run phpcs --config-set installed_paths /existing/paths,/path/to/gampear to add your standard to PHPCS. Next, run phpcs -i to check that the Gampear standard is listed in the list of registered standards.

After that, you should be good to go.

Also see:

gamcoh commented 6 years ago

That's what i did but it won't work:

.travis.yml:
before_install:
  - composer require php-coveralls/php-coveralls '~1.0'
  - composer install
  - git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git /tmp/phpcs
  - git clone -b master https://github.com/gamcoh/Gamzer-PHP-Standard.git /tmp/Gamzer-PHP-Standard
  - /tmp/phpcs/bin/phpcs --config-set installed_path /tmp/Gamzer-PHP-Standard/Gamzerpear

script:
  - ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml
  - /tmp/phpcs/bin/phpcs . --standard=./phpcs.xml

when it runs in travis it says:

$ /tmp/phpcs/bin/phpcs . --standard=./phpcs.xml
ERROR: Referenced sniff "Gamzerpear.Commenting.ClassComment" does not exist

Here's my phpcs.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <ruleset name="Gamzer">
    <description>The Gamzer Standard</description>

    <exclude-pattern>./vendor</exclude-pattern>

    <rule ref="Generic.Classes.DuplicateClassName"/>
    <rule ref="Generic.CodeAnalysis.EmptyStatement"/>
    <rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
    <rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
    <rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
    <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
    <rule ref="Generic.ControlStructures.InlineControlStructure"/>
    <rule ref="Generic.Files.LineEndings"/>
    <rule ref="Generic.Formatting.DisallowMultipleStatements"/>
    <rule ref="Generic.Formatting.SpaceAfterCast"/>
    <rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
    <rule ref="Generic.NamingConventions.ConstructorName"/>
    <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
    <rule ref="Generic.NamingConventions.CamelCapsFunctionName"/>
    <rule ref="Generic.PHP.DeprecatedFunctions"/>
    <rule ref="Generic.PHP.ForbiddenFunctions"/>
    <rule ref="Generic.PHP.LowerCaseConstant"/>
    <rule ref="Generic.Strings.UnnecessaryStringConcat"/>
    <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
    <rule ref="Gamzerpear.Commenting.ClassComment"/>
    <rule ref="Gamzerpear.Commenting.FunctionComment"/>
    <rule ref="Gamzerpear.Commenting.InlineComment"/>
    <rule ref="Gamzerpear.ControlStructures.MultiLineCondition"/>
    <rule ref="Gamzerpear.Files.IncludingFile"/>
    <rule ref="Gamzerpear.Functions.ValidDefaultValue"/>
    <rule ref="Gamzerpear.NamingConventions.ValidClassName"/>
    <rule ref="PSR2.ControlStructures.ControlStructureSpacing"/>
    <rule ref="PSR2.ControlStructures.ElseIfDeclaration"/>
    <rule ref="PSR2.Files.EndFileNewline"/>
    <rule ref="Squiz.PHP.InnerFunctions"/>
    <rule ref="Squiz.PHP.LowercasePHPFunctions"/>
    <rule ref="Squiz.PHP.NonExecutableCode"/>
    <rule ref="Squiz.Scope.MethodScope"/>
    <rule ref="Squiz.Scope.StaticThisUsage"/>
    <rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
    <rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
    <rule ref="Zend.Files.ClosingTag"/>
</ruleset>
gsherwood commented 6 years ago

Your custom standard wont run from a git clone because of the namespaces you've chosen for your sniffs. You've used the namespace PHP_CodeSniffer\Standards\Gamzerpear\Sniffs but this tells PHPCS that the Gamzerpear standard in installed directly into PHP_CodeSniffer's Sniffs directory.

Change your namespaces to be Gamzerpear\Sniffs and it should start working for that git clone.

gamcoh commented 6 years ago

Alright i just did that and it still don't work.

Here's my error log from Travis

$ php --version
PHP 7.2.7 (cli) (built: Jun 23 2018 05:35:37) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

$ composer --version
Composer version 1.6.5 2018-05-04 11:44:59

$ composer require php-coveralls/php-coveralls '~1.0'
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 36 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-ctype (v1.8.0): Downloading (100%)
  - Installing symfony/yaml (v4.1.0): Downloading (100%)
  - Installing symfony/stopwatch (v4.1.1): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.8.0): Downloading (40%Downloading (100%)
  - Installing symfony/console (v4.1.1): Downloading (100%)
  - Installing psr/log (1.0.2): Downloading (connecting...)Downloading (100%)
  - Installing symfony/filesystem (v4.1.1): Downloading (100%)
  - Installing symfony/config (v4.1.1): Downloading (100%)
  - Installing symfony/event-dispatcher (v2.8.42): DownloadingDownloading (100%)
  - Installing guzzle/guzzle (v3.9.3): Downloading (100%)
  - Installing php-coveralls/php-coveralls (v1.1.0): Downloading (connecting...)Downloading (100%)
  - Installing sebastian/version (2.0.1): Downloading (100%)
  - Installing sebastian/resource-operations (1.0.0): Downloading (100%)
  - Installing sebastian/recursion-context (2.0.0): Downloading (100%)
  - Installing sebastian/object-enumerator (2.0.1): Downloading (100%)
  - Installing sebastian/global-state (1.1.1): Downloading (100%)
  - Installing sebastian/exporter (2.0.0): Downloading (100%)
  - Installing sebastian/environment (2.0.0): Downloading (connecting...)Downloading (100%)
  - Installing sebastian/diff (1.4.3): Downloading (50%)Downloading (100%)
  - Installing sebastian/comparator (1.2.4): Downloading (100%)
  - Installing phpunit/php-text-template (1.2.1): Downloading (100%)
  - Installing doctrine/instantiator (1.1.0): Downloading (100%)
  - Installing phpunit/phpunit-mock-objects (3.4.4): Downloading (35%Downloading (100%)
  - Installing phpunit/php-timer (1.0.9): Downloading (100%)
  - Installing phpunit/php-file-iterator (1.4.5): Downloading (connecting...)Downloading (100%)
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Downloading (100%)
  - Installing phpunit/php-token-stream (2.0.2): Downloading (100%)
  - Installing phpunit/php-code-coverage (4.0.8): DownloDownloading (100%)
  - Installing webmozart/assert (1.3.0): Downloading (100%)
  - Installing phpdocumentor/reflection-common (1.0.1): Downloading (100%)
  - Installing phpdocumentor/type-resolver (0.4.0): Downloading (connecting...)Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (4.3.0): Downloading (100%)
  - Installing phpspec/prophecy (1.7.6): DownloadingDownloading (100%)
  - Installing myclabs/deep-copy (1.8.1): Downloading (100%)
  - Installing phpunit/phpunit (5.7.27): Downloading (100%)
  - Installing squizlabs/php_codesniffer (3.3.0): DownloadDownloading (100%)
symfony/console suggests installing psr/log-implementation (For using the console logger)
symfony/console suggests installing symfony/lock ()
symfony/console suggests installing symfony/process ()
symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
guzzle/guzzle suggests installing guzzlehttp/guzzle (Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated.)
php-coveralls/php-coveralls suggests installing symfony/http-kernel (Allows Symfony integration)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Package guzzle/guzzle is abandoned, you should avoid using it. Use guzzlehttp/guzzle instead.
Writing lock file
Generating autoload files

$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Package guzzle/guzzle is abandoned, you should avoid using it. Use guzzlehttp/guzzle instead.
Generating autoload files

$ git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git /tmp/phpcs
Cloning into '/tmp/phpcs'...
remote: Counting objects: 1331, done.
remote: Compressing objects: 100% (1100/1100), done.
remote: Total 1331 (delta 465), reused 598 (delta 179), pack-reused 0
Receiving objects: 100% (1331/1331), 815.65 KiB | 8.50 MiB/s, done.
Resolving deltas: 100% (465/465), done.

$ git clone -b master https://github.com/gamcoh/Gamzer-PHP-Standard.git /tmp/Gamzer-PHP-Standard
Cloning into '/tmp/Gamzer-PHP-Standard'...
remote: Counting objects: 185, done.
remote: Compressing objects: 100% (106/106), done.
remote: Total 185 (delta 81), reused 178 (delta 77), pack-reused 0
Receiving objects: 100% (185/185), 79.95 KiB | 15.99 MiB/s, done.
Resolving deltas: 100% (81/81), done.

$ /tmp/phpcs/bin/phpcs --config-set installed_path /tmp/Gamzer-PHP-Standard/Gamzerpear
Using config file: /tmp/phpcs/CodeSniffer.conf
Config value "installed_path" added successfully

$ /tmp/phpcs/bin/phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend
The command "/tmp/phpcs/bin/phpcs -i" exited with 0.

$ ./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Error:         No whitelist configured, no code coverage will be generated
....                                                                4 / 4 (100%)
Time: 57 ms, Memory: 4.00MB
OK (4 tests, 4 assertions)
The command "./vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml" exited with 0.

$ /tmp/phpcs/bin/phpcs . --standard=./phpcs.xml
ERROR: Referenced sniff "Gamzerpear.Commenting.ClassComment" does not exist
Run "phpcs --help" for usage information
The command "/tmp/phpcs/bin/phpcs . --standard=./phpcs.xml" exited with 3.

As you can see it didn't install my custom standard

jrfnl commented 6 years ago

@gamcoh The output of the phpcs -i command shows that the path is not installed properly. PHPCS expects a standards directory which can contain one or more standards, though as of PHPCS 3.0 just passing the lower level directory should work too.

Could you try changing the installed_paths command to this and see if the standard is then shown when you run phpcs -i ? $ /tmp/phpcs/bin/phpcs --config-set installed_paths /tmp/Gamzer-PHP-Standard/

gamcoh commented 6 years ago

Nop, doesn't work. It's like the standard is not comprehended like a PHP Standard, strange because i coded it based on the PEAR Standard

jrfnl commented 6 years ago

@gamcoh I just cloned your repo & tried the commands locally and it all installs & runs without problem. Can you confirm that is also the case when you run it locally ?

If so, the problem is in the specific configuration used in combination with Travis. Could you post a link to the Travis script you use ?

gamcoh commented 6 years ago

I tried setting up another server and i followed every commands in my .travis.yml but it wont work.

Here's the .travis.yml file

jrfnl commented 6 years ago

@gamcoh I've had a quick look. You seem to try to register the PHPCS native standards as well. You don't need to do that. There is also a typo in that line - installed_path vs installed_paths (note the s at the end).

Try changing line 10 to: - ./vendor/bin/phpcs --config-set installed_paths /tmp/Gamzer-PHP-Standard/

gamcoh commented 6 years ago

It works !! i'm so embarrassed for the typo. Thank you all !!