Given the following configuration:
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse">
<properties>
<property name="linesCountBeforeFirstUse" value="0"/>
</properties>
</rule>
And the following code:
class Foo
{
use Bar;
public $id;
}
The following error is reported:
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------------------------------------------------
11 | ERROR | [x] Expected 1 lines before first use statement, found 0.
| | (SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse)
The expected number of lines should be 0 not 1.
You configuration is invalid.
@kukulich how so? According to this code it seems correct.
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file
@kukulich obviously that is not my entire config file. But if you insist:
<?xml version="1.0"?>
<ruleset name="Default">
<!-- show progress -->
<arg value="p"/>
<!-- check source and tests -->
<file>src</file>
<file>tests</file>
<!-- enable PSR-1 and PSR-2 checking -->
<rule ref="PSR1"/>
<rule ref="PSR2"/>
<!-- https://github.com/slevomat/coding-standard -->
<rule ref="SlevomatCodingStandard">
<exclude name="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces.NoEmptyLineAfterOpeningBrace"/>
<exclude name="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces.NoEmptyLineBeforeClosingBrace"/>
<exclude name="SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedConstant"/>
<exclude name="SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedConstant"/>
<exclude name="SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment.OneLinePropertyComment"/>
<exclude name="SlevomatCodingStandard.ControlStructures.NewWithoutParentheses.UselessParentheses"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireYodaComparison.RequiredYodaComparison"/>
<exclude name="SlevomatCodingStandard.Functions.TrailingCommaInCall.MissingTrailingComma"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation.NonFullyQualifiedClassName"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions.NonFullyQualifiedException"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants.NonFullyQualified"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions.NonFullyQualified"/>
<exclude name="SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces.NonFullyQualified"/>
<exclude name="SlevomatCodingStandard.Namespaces.UseSpacing.IncorrectLinesCountBetweenDifferentTypeOfUse"/>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array">
<element key="src" value="Outfitter"/>
<element key="tests" value="Outfitter\Tests"/>
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse">
<properties>
<property name="linesCountBeforeFirstUse" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UseSpacing.UseSpacingSniff">
<properties>
<property name="linesCountBetweenUseTypes" value="2"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFallbackGlobalFunctions" value="true"/>
<property name="allowFallbackGlobalConstants" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="1"/>
<property name="newlinesCountAfterDeclare" value="2"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>
</rule>
<!-- functions are for utility -->
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEmpty.DisallowedEmpty">
<exclude-pattern>src/functions.php</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint">
<exclude-pattern>src/functions.php</exclude-pattern>
</rule>
<!-- custom types and migrations do not require parameter checks -->
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint">
<exclude-pattern>src/Infrastructure/Persistence/Type/*.php</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification">
<exclude-pattern>src/Infrastructure/Persistence/Type/*.php</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter">
<exclude-pattern>src/Infrastructure/Persistence/Migration/*.php</exclude-pattern>
<exclude-pattern>src/Infrastructure/Persistence/Type/*.php</exclude-pattern>
</rule>
<!-- all comments are useful in tests -->
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessDocComment">
<exclude-pattern>tests/*/*.php</exclude-pattern>
</rule>
</ruleset>
@shadowhand You configuration is still invalid, see the link I posted.
@kukulich it is not, other sniffs are working just fine. I am using https://github.com/sirbrillig/phpcs-import-detection to automatically register sniffs when installed.
@kukulich I just tried with this config, copy paste from this project README, modified to run the specific sniff:
<?xml version="1.0"?>
<ruleset name="AcmeProject">
<config name="installed_paths" value="../../slevomat/coding-standard"/><!-- relative path from PHPCS source location -->
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse">
<properties>
<property name="linesCountBeforeFirstUse" value="0"/>
</properties>
</rule>
</ruleset>
Exact same result, false positive:
phpcs -s src/Infrastructure/Factory/ContainerFactory.php
FILE: /Users/wgilk/Code/acme/src/Infrastructure/Factory/ContainerFactory.php
-----------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------------------------------------------------
11 | ERROR | [x] Expected 1 lines before first use statement, found 0.
| | (SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Here's the complete file, if it helps:
<?php
declare(strict_types=1);
namespace Acme\Infrastructure\Factory;
use Acme\Infrastructure\CanConfigureContainer;
use Psr\Container\ContainerInterface;
final class ContainerFactory
{
use CanConfigureContainer;
public static function make(): ContainerInterface
{
return call_user_func(new self());
}
public function __invoke(): ContainerInterface
{
return $this->createContainer();
}
}
I reckon you need to ditch IncorrectLinesCountBeforeFirstUse in this line <rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse">
So it would become:
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing">
<properties>
<property name="linesCountBeforeFirstUse" value="0"/>
</properties>
</rule>
If you don't want the other checks you either need to exclude them (below the SlevomatCodingStandard.Classes.TraitUseSpacing rule):
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.linesCountBetweenUses">
<exclude-pattern>*.php</exclude-pattern>
</rule>
Or downgrade the severity:
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.linesCountBetweenUses">
<severity>0</severity>
</rule>
@Levivb 馃帀 thank you, that worked! I didn't realize that copy/pasting the failed sniff from phpcs -s output would be incorrect.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I reckon you need to ditch
IncorrectLinesCountBeforeFirstUsein this line<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing.IncorrectLinesCountBeforeFirstUse">So it would become:
If you don't want the other checks you either need to exclude them (below the
SlevomatCodingStandard.Classes.TraitUseSpacingrule):Or downgrade the severity: