PHPCSStandards / PHP_CodeSniffer

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

Wrong standard reported or loading standards not defined in phpcs.xml #583

Closed Rasmus-Bertell closed 3 months ago

Rasmus-Bertell commented 3 months ago

Describe the bug

I'm trying to configure phpcs, but I keep getting errors from standards I haven't defined in my phpcs.xml file.

Code sample

<?php

declare(strict_types=1);

abstract class TestCase {}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="custom">
    <rule ref="PSR1"/>
    <rule ref="PSR12"/>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php -s
  3. Standards PSR2 and Squiz are in error report but not defined in ruleset.
❯ ./vendor/bin/phpcs tests/TestCase.php -s

FILE: /home/rasmus/projects/wasa-translations/tests/TestCase.php
--------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------
 5 | ERROR | [ ] Each class must be in a namespace of at least one level (a top-level vendor name)
   |       |     (PSR1.Classes.ClassDeclaration.MissingNamespace)
 5 | ERROR | [x] Opening brace of a class must be on the line after the definition
   |       |     (PSR2.Classes.ClassDeclaration.OpenBraceNewLine)
 5 | ERROR | [x] Closing brace must be on a line by itself
   |       |     (Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore)
--------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------

Time: 89ms; Memory: 12MB

Expected behavior

I expect standards not defined in config to not be used.

Versions (please complete the following information)

Operating System Archlinux 6.10.2-arch1-1
PHP version 8.3.9
PHP_CodeSniffer version 3.10.2
Standard PSR1 & PSR12
Install type composer local

Additional context

This might also be a configuration error on my part, but I can't for the life of me find proper information from the wiki. I had to search a long time to even find information that the file should be named phpcs.xml.

Please confirm

jrfnl commented 3 months ago

Closing as invalid.

Please don't open support questions as issues.

Standards can include sniffs from other standards, so this is perfectly normal behaviour.

To see which sniffs are included for your ruleset, run PHPCS with the -e flag.

Rasmus-Bertell commented 3 months ago

I don't see any PSR2 or Squiz sniffs in here

PSR1 (3 sniffs)
---------------
  PSR1.Classes.ClassDeclaration
  PSR1.Files.SideEffects
  PSR1.Methods.CamelCapsMethodName

PSR12 (17 sniffs)
-----------------
  PSR12.Classes.AnonClassDeclaration
  PSR12.Classes.ClassInstantiation
  PSR12.Classes.ClosingBrace
  PSR12.Classes.OpeningBraceSpace
  PSR12.ControlStructures.BooleanOperatorPlacement
  PSR12.ControlStructures.ControlStructureSpacing
  PSR12.Files.DeclareStatement
  PSR12.Files.FileHeader
  PSR12.Files.ImportStatement
  PSR12.Files.OpenTag
  PSR12.Functions.NullableTypeDeclaration
  PSR12.Functions.ReturnTypeDeclaration
  PSR12.Keywords.ShortFormTypeKeywords
  PSR12.Namespaces.CompoundNamespaceDepth
  PSR12.Operators.OperatorSpacing
  PSR12.Properties.ConstantVisibility
  PSR12.Traits.UseDeclaration
Rasmus-Bertell commented 3 months ago

Sorry, I read the output wrong. I thought it listed the sniffs a standard includes, but it just grouped them.

The whole output was

❯ ./vendor/bin/phpcs -e

The custom standard contains 60 sniffs

Generic (15 sniffs)
-------------------
  Generic.ControlStructures.InlineControlStructure
  Generic.Files.ByteOrderMark
  Generic.Files.LineEndings
  Generic.Files.LineLength
  Generic.Formatting.DisallowMultipleStatements
  Generic.Functions.FunctionCallArgumentSpacing
  Generic.NamingConventions.UpperCaseConstantName
  Generic.PHP.DisallowAlternativePHPTags
  Generic.PHP.DisallowShortOpenTag
  Generic.PHP.LowerCaseConstant
  Generic.PHP.LowerCaseKeyword
  Generic.PHP.LowerCaseType
  Generic.WhiteSpace.DisallowTabIndent
  Generic.WhiteSpace.IncrementDecrementSpacing
  Generic.WhiteSpace.ScopeIndent

PEAR (1 sniff)
--------------
  PEAR.Functions.ValidDefaultValue

PSR1 (3 sniffs)
---------------
  PSR1.Classes.ClassDeclaration
  PSR1.Files.SideEffects
  PSR1.Methods.CamelCapsMethodName

PSR2 (9 sniffs)
---------------
  PSR2.Classes.ClassDeclaration
  PSR2.Classes.PropertyDeclaration
  PSR2.ControlStructures.ElseIfDeclaration
  PSR2.ControlStructures.SwitchDeclaration
  PSR2.Files.ClosingTag
  PSR2.Files.EndFileNewline
  PSR2.Methods.FunctionCallSignature
  PSR2.Methods.FunctionClosingBrace
  PSR2.Methods.MethodDeclaration

PSR12 (17 sniffs)
-----------------
  PSR12.Classes.AnonClassDeclaration
  PSR12.Classes.ClassInstantiation
  PSR12.Classes.ClosingBrace
  PSR12.Classes.OpeningBraceSpace
  PSR12.ControlStructures.BooleanOperatorPlacement
  PSR12.ControlStructures.ControlStructureSpacing
  PSR12.Files.DeclareStatement
  PSR12.Files.FileHeader
  PSR12.Files.ImportStatement
  PSR12.Files.OpenTag
  PSR12.Functions.NullableTypeDeclaration
  PSR12.Functions.ReturnTypeDeclaration
  PSR12.Keywords.ShortFormTypeKeywords
  PSR12.Namespaces.CompoundNamespaceDepth
  PSR12.Operators.OperatorSpacing
  PSR12.Properties.ConstantVisibility
  PSR12.Traits.UseDeclaration

Squiz (15 sniffs)
-----------------
  Squiz.Classes.ValidClassName
  Squiz.ControlStructures.ControlSignature
  Squiz.ControlStructures.ForEachLoopDeclaration
  Squiz.ControlStructures.ForLoopDeclaration
  Squiz.ControlStructures.LowercaseDeclaration
  Squiz.Functions.FunctionDeclaration
  Squiz.Functions.FunctionDeclarationArgumentSpacing
  Squiz.Functions.LowercaseFunctionKeywords
  Squiz.Functions.MultiLineFunctionDeclaration
  Squiz.Scope.MethodScope
  Squiz.WhiteSpace.CastSpacing
  Squiz.WhiteSpace.ControlStructureSpacing
  Squiz.WhiteSpace.ScopeClosingBrace
  Squiz.WhiteSpace.ScopeKeywordSpacing
  Squiz.WhiteSpace.SuperfluousWhitespace