Skip to content

Commit

Permalink
Re-generate custom ruleset (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
zingimmick authored Jan 10, 2022
1 parent 6883a76 commit 62e8732
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 331 deletions.
343 changes: 149 additions & 194 deletions config/set/php_codesniffer.php

Large diffs are not rendered by default.

231 changes: 102 additions & 129 deletions config/set/slevomat-coding-standard.php

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Zing\CodingStandard\Set\ECSSetList;

return static function (ContainerConfigurator $containerConfigurator): void {
\PHP_CodeSniffer\Config::setConfigData('php_version', 70200);
$containerConfigurator->import(ECSSetList::PHP71_MIGRATION);
$containerConfigurator->import(ECSSetList::PHP71_MIGRATION_RISKY);
$containerConfigurator->import(ECSSetList::CUSTOM);
Expand Down
31 changes: 31 additions & 0 deletions src/PHPCodeSnifferRuleSetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class PHPCodeSnifferRuleSetGenerator
'Squiz' => 'squiz.php',
'Zend' => 'zend.php',
'SlevomatCodingStandard' => 'slevomat-coding-standard.php',
'PHPCodeSnifferCustom' => '../php_codesniffer.php',
'SlevomatCodingStandardCustom' => '../slevomat-coding-standard.php',
];

/**
Expand Down Expand Up @@ -62,12 +64,19 @@ private function formatRulesToServices(Ruleset $ruleset): array
{
$sniffs = [];
foreach ($ruleset->sniffs as $sniff) {
if (! $this->isPhpSniff($sniff, str_contains($ruleset->name, 'Custom'))) {
continue;
}

$sniffs[is_object($sniff) ? get_class($sniff) : $sniff] = [];
}

foreach ($ruleset->ruleset as $code => $attr) {
if (isset($ruleset->sniffCodes[$code])) {
$sniff = $ruleset->sniffCodes[$code];
if (! $this->isPhpSniff($sniff, str_contains($ruleset->name, 'Custom'))) {
continue;
}

$sniffs[$sniff] = $attr['properties'] ?? [];
}
Expand All @@ -76,6 +85,24 @@ private function formatRulesToServices(Ruleset $ruleset): array
return $sniffs;
}

private function isPhpSniff($sniff, bool $isStandard = false): bool
{
if (! $isStandard) {
return true;
}

if (! class_exists($sniff)) {
return false;
}

$sniffObject = new $sniff();
if (! property_exists($sniffObject, 'supportedTokenizers')) {
return true;
}

return in_array('PHP', $sniffObject->supportedTokenizers, true);
}

/**
* @return iterable<\Symfony\Component\Finder\SplFileInfo>
*/
Expand All @@ -90,5 +117,9 @@ private function getSetDefinitions(): iterable
->files()
->in(__DIR__ . '/../vendor/slevomat/coding-standard')
->name('ruleset.xml');
yield from Finder::create()
->files()
->in(__DIR__ . '/Standards')
->name('ruleset.xml');
}
}
18 changes: 10 additions & 8 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ protected function hasNodeWithComments(array $nodes): bool

protected function pExpr_MethodCall(MethodCall $node): string
{
$nextCallIndentReplacement = ')' . PHP_EOL . preg_replace(
'#(?:^|[\\r\\n]+)(?=[^\\r\\n])#',
'$0' . \str_repeat(' ', 8),
'->'
);
$content = parent::pExpr_MethodCall($node);
$content = preg_replace('#\\n#', PHP_EOL . str_repeat(' ', 4), $content);
if ($node->var instanceof MethodCall) {
$var = $this->pDereferenceLhs($node->var);
$this->indent();
$content = $var . $this->nl . '->' . $this->pObjectProperty($node->name)
. '(' . $this->pMaybeMultiline($node->args) . ')';
$this->outdent();

return preg_replace('#\)->#', $nextCallIndentReplacement, $content);
return $content;
}

return parent::pExpr_MethodCall($node);
}

protected function pExpr_Closure(Closure $node): string
Expand Down
174 changes: 174 additions & 0 deletions src/Standards/PHPCodeSnifferCustom/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP Code Sniffer Custom" xsi:noNamespaceSchemaLocation="../../../vendor/squizlabs/php_codesniffer/phpcs.xsd">

<rule ref="Generic">
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent"/>
<exclude name="Generic.PHP.ClosingPHPTag"/>
<exclude name="Generic.Files.EndFileNoNewline"/>
<exclude name="Generic.PHP.RequireStrictTypes"/>
<exclude name="Generic.Files.LowercasedFilename"/>
<exclude name="Generic.Formatting.NoSpaceAfterCast"/>
<exclude name="Generic.Functions.OpeningFunctionBraceKernighanRitchie"/>
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
<exclude name="Generic.PHP.UpperCaseConstant"/>
<exclude name="Generic.Classes.OpeningBraceSameLine"/>
<exclude name="Generic.Commenting.DocComment"/>
<exclude name="Generic.NamingConventions.TraitNameSuffix"/>
<exclude name="Generic.NamingConventions.AbstractClassNamePrefix"/>
<exclude name="Generic.NamingConventions.InterfaceNameSuffix"/>
<exclude name="Generic.Formatting.SpaceBeforeCast"/>
<exclude name="Generic.NamingConventions.CamelCapsFunctionName"/>
<exclude name="Generic.NamingConventions.ConstructorName"/>
<exclude name="Generic.WhiteSpace.LanguageConstructSpacing"/>
<exclude name="Generic.Files.InlineHTML"/>
<exclude name="Generic.Debug.CSSLint"/>
<exclude name="Generic.Debug.ESLint"/>
<exclude name="Generic.Debug.JSHint"/>
<exclude name="Generic.Debug.ClosureLinter"/>
</rule>

<rule ref="PEAR">
<exclude name="PEAR.Functions.FunctionCallSignature"/>
<exclude name="PEAR.ControlStructures.MultiLineCondition"/>
<exclude name="PEAR.Commenting.FileComment"/>
<exclude name="Generic.Commenting.DocComment"/>
<exclude name="PEAR.Commenting.ClassComment"/>
<exclude name="PEAR.Commenting.FunctionComment"/>
<exclude name="PEAR.Formatting.MultiLineAssignment"/>
<exclude name="PEAR.WhiteSpace.ScopeIndent"/>
<exclude name="PEAR.Files.IncludingFile"/>
<exclude name="PEAR.NamingConventions.ValidFunctionName"/>
<exclude name="PEAR.NamingConventions.ValidVariableName"/>
<exclude name="PEAR.ControlStructures.ControlSignature"/>
</rule>

<rule ref="PSR1">
</rule>

<rule ref="PSR2">
<exclude name="PSR2.ControlStructures.ControlStructureSpacing"/>
</rule>

<rule ref="PSR12">
<exclude name="PSR12.Files.FileHeader"/>
<exclude name="PSR12.Classes.AnonClassDeclaration"/>
<exclude name="PSR12.Traits.UseDeclaration"/>
<exclude name="PSR12.Operators.OperatorSpacing"/>
</rule>

<rule ref="Squiz">
<exclude name="PSR12.Traits.UseDeclaration"/>
<exclude name="Generic.Commenting.DocComment"/>
<exclude name="Squiz.Commenting.FileComment"/>
<exclude name="Squiz.Arrays.ArrayDeclaration"/>
<exclude name="Generic.Formatting.MultipleStatementAlignment"/>
<exclude name="PEAR.Functions.FunctionCallSignature"/>
<exclude name="Squiz.Strings.DoubleQuoteUsage"/>
<exclude name="Squiz.WhiteSpace.ObjectOperatorSpacing"/>
<exclude name="Squiz.ControlStructures.SwitchDeclaration"/>
<exclude name="Squiz.Operators.ComparisonOperatorUsage"/>
<exclude name="Squiz.PHP.DisallowInlineIf"/>
<exclude name="Squiz.Commenting.BlockComment"/>
<exclude name="Squiz.Commenting.InlineComment"/>
<exclude name="Squiz.Strings.ConcatenationSpacing"/>
<exclude name="Squiz.WhiteSpace.OperatorSpacing"/>
<exclude name="Squiz.NamingConventions.ValidVariableName"/>
<exclude name="Squiz.Formatting.OperatorBracket"/>
<exclude name="Squiz.ControlStructures.InlineIfDeclaration"/>
<exclude name="Squiz.Commenting.ClosingDeclarationComment"/>
<exclude name="Squiz.Files.FileExtension"/>
<exclude name="PEAR.Formatting.MultiLineAssignment"/>
<exclude name="Squiz.Objects.ObjectInstantiation"/>
<exclude name="PEAR.ControlStructures.MultiLineCondition"/>
<exclude name="PEAR.Files.IncludingFile"/>
<exclude name="Squiz.ControlStructures.ElseIfDeclaration"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing"/>
<exclude name="Squiz.PHP.DisallowComparisonAssignment"/>
<exclude name="Squiz.PHP.DisallowBooleanStatement"/>
<exclude name="Squiz.Commenting.ClassComment"/>
<exclude name="Squiz.Commenting.VariableComment"/>
<exclude name="Squiz.Commenting.FunctionComment"/>
<exclude name="Squiz.WhiteSpace.MemberVarSpacing"/>
<exclude name="Squiz.WhiteSpace.FunctionSpacing"/>
<exclude name="Squiz.WhiteSpace.FunctionClosingBraceSpace"/>
<exclude name="Squiz.NamingConventions.ValidFunctionName"/>
<exclude name="Squiz.PHP.DisallowMultipleAssignments"/>
<exclude name="Squiz.Commenting.FunctionCommentThrowTag"/>
<exclude name="Generic.NamingConventions.ConstructorName"/>
<exclude name="Squiz.PHP.Heredoc"/>
<exclude name="Generic.WhiteSpace.LanguageConstructSpacing"/>
<exclude name="Squiz.CSS"/>
<exclude name="Squiz.Debug.JSLint"/>
<exclude name="Squiz.Debug.JavaScriptLint"/>
<exclude name="Squiz.Classes.DuplicateProperty"/>
<exclude name="Squiz.Objects.ObjectMemberComma"/>
<exclude name="Squiz.Objects.DisallowObjectStringIndex"/>
<exclude name="Squiz.WhiteSpace.PropertyLabelSpacing"/>
<exclude name="Squiz.Commenting.PostStatementComment"/>
</rule>

<rule ref="Zend">
<exclude name="Zend.NamingConventions.ValidVariableName"/>
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
<exclude name="PEAR.Functions.FunctionCallSignature"/>
</rule>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="80"/>
<property name="absoluteLineLimit" value="0"/>
<property name="ignoreComments" value="true"/>
</properties>
</rule>
<rule ref="Generic.ControlStructures.InlineControlStructure">
<properties>
<property name="error" value="true"/>
</properties>
</rule>
<rule ref="Generic.ControlStructures.InlineControlStructure">
<properties>
<property name="error" value="true"/>
</properties>
</rule>
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n"/>
</properties>
</rule>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="ignoreIndentationTokens" type="array">
<element value="T_COMMENT"/>
<element value="T_DOC_COMMENT_OPEN_TAG"/>
</property>
</properties>
</rule>
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern type="relative">*/migrations/*</exclude-pattern>
<exclude-pattern type="relative">*/seeds/*</exclude-pattern>
</rule>
<rule ref="PSR1.Methods.CamelCapsMethodName">
<exclude-pattern>*Test.php</exclude-pattern>
</rule>
<rule ref="Squiz.ControlStructures.ForLoopDeclaration">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
<properties>
<property name="equalsSpacing" value="1"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="true"/>
</properties>
</rule>
</ruleset>
87 changes: 87 additions & 0 deletions src/Standards/SlevomatCodingStandardCustom/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0"?>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="Slevomat Coding Standard Custom"
xsi:noNamespaceSchemaLocation="../../../vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<rule ref="../../../vendor/slevomat/coding-standard/SlevomatCodingStandard/ruleset.xml">
<exclude name="SlevomatCodingStandard.Numbers.RequireNumericLiteralSeparator"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
<exclude name="SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces"/>
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall"/>
<exclude name="SlevomatCodingStandard.Operators.DisallowEqualOperators"/>
<exclude name="SlevomatCodingStandard.Functions.StrictCall"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireYodaComparison"/>
<exclude name="SlevomatCodingStandard.PHP.RequireExplicitAssertion"/>
<exclude name="SlevomatCodingStandard.Functions.StaticClosure"/>
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing"/>
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator"/>
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
<exclude name="SlevomatCodingStandard.Functions.DisallowArrowFunction"/>
<exclude name="SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing"/>
<exclude name="SlevomatCodingStandard.Files.LineLength"/>
<exclude name="SlevomatCodingStandard.Functions.RequireMultiLineCall"/>
<exclude name="SlevomatCodingStandard.ControlStructures.NewWithoutParentheses"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireSingleLineCondition"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireMultiLineCondition"/>
<exclude name="SlevomatCodingStandard.PHP.DisallowReference"/>
<exclude name="SlevomatCodingStandard.Files.TypeNameMatchesFileName"/>
<exclude name="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces"/>
<exclude name="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireMultiLineTernaryOperator"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/>
<exclude name="SlevomatCodingStandard.Files.FunctionLength"/>
<exclude name="SlevomatCodingStandard.Functions.FunctionLength"/>
<exclude name="SlevomatCodingStandard.Operators.DisallowIncrementAndDecrementOperators"/>
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLineDocComment"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
<exclude name="SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint"/>
<exclude name="SlevomatCodingStandard.Classes.ForbiddenPublicProperty"/>
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment"/>
<exclude name="SlevomatCodingStandard.Functions.DisallowEmptyFunction"/>
<exclude name="SlevomatCodingStandard.ControlStructures.DisallowEmpty"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.Classes.ClassStructure"/>
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
<exclude name="SlevomatCodingStandard.Functions.RequireSingleLineCall"/>
<exclude name="SlevomatCodingStandard.TypeHints.DisallowArrayTypeHintSyntax"/>
<exclude name="SlevomatCodingStandard.Functions.UnusedParameter"/>
<exclude name="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
<exclude name="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion"/>
<exclude name="SlevomatCodingStandard.Classes.RequireSingleLineMethodSignature"/>
<exclude name="SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration"/>
<exclude name="SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireTernaryOperator"/>
<exclude name="SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming"/>
<exclude name="SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall"/>
<exclude name="SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousErrorNaming"/>
</rule>

<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing">
<properties>
<property name="linesCountAfterLastUse" value="1"/>
<property name="linesCountAfterLastUseWhenLastInClass" value="0"/>
<property name="linesCountBeforeFirstUse" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="2"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Commenting.DocCommentSpacing">
<properties>
<property name="linesCountBetweenDifferentAnnotationsTypes" value="1"/>
</properties>
</rule>
</ruleset>

0 comments on commit 62e8732

Please sign in to comment.