Skip to content

Add more new sniffs #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 0 additions & 117 deletions lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php

This file was deleted.

4 changes: 2 additions & 2 deletions lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function ensureSpaceBefore(File $file, array $tokens, int $position, s
return;
}

if ( ! $file->addFixableError($message, $position, 'before', ['before', $spacing])) {
if (! $file->addFixableError($message, $position, 'before', ['before', $spacing])) {
return;
}

Expand All @@ -36,7 +36,7 @@ protected function ensureSpaceAfter(File $file, array $tokens, int $position, st
return;
}

if ( ! $file->addFixableError($message, $position, 'after', ['after', $spacing])) {
if (! $file->addFixableError($message, $position, 'after', ['after', $spacing])) {
return;
}

Expand Down
28 changes: 0 additions & 28 deletions lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php

This file was deleted.

171 changes: 160 additions & 11 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,180 @@
<description>The Doctrine coding standard.</description>

<!-- Import PSR-2 coding standard (base) -->
<rule ref="PSR2">
<!-- Using \Doctrine\Sniffs\Spacing\ControlStructureSniff instead -->
<exclude name="PSR2.ControlStructures.ControlStructureSpacing"/>
</rule>
<rule ref="PSR2"/>

<!-- Disallow else if in favour elseif -->
<rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
<type>error</type>
<!-- Forbid `array(...)` -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<!-- Forbid duplicate classes -->
<rule ref="Generic.Classes.DuplicateClassName"/>
<!-- Forbid empty statements -->
<rule ref="Generic.CodeAnalysis.EmptyStatement">
<!-- But allow empty catch -->
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCATCH"/>
</rule>

<!-- Forbid final methods in final classes -->
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<!-- Forbid useless empty method overrides -->
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
<!-- Forbid inline control structures, i.e. `if ($foo) bar();` -->
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
<!-- Forbid inline HTML in PHP code -->
<rule ref="Generic.Files.InlineHTML"/>
<!-- Align corresponding assignment statement tokens -->
<rule ref="Generic.Formatting.MultipleStatementAlignment">
<properties>
<property name="error" value="true"/>
</properties>
</rule>

<!-- Force whitespace after a type cast -->
<rule ref="Generic.Formatting.SpaceAfterCast" />

<rule ref="Generic.Formatting.SpaceAfterCast"/>
<!-- Force whitespace after `!` -->
<rule ref="Generic.Formatting.SpaceAfterNot"/>
<!-- Forbid PHP 4 constructors -->
<rule ref="Generic.NamingConventions.ConstructorName"/>
<!-- Forbid any content before opening tag -->
<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/>
<!-- Forbid deprecated functions -->
<rule ref="Generic.PHP.DeprecatedFunctions"/>
<!-- Forbid alias functions, i.e. `sizeof()`, `delete()` -->
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<!-- Forbid useless inline string concatenation -->
<rule ref="Generic.Strings.UnnecessaryStringConcat">
<!-- But multiline is useful for readability -->
<properties>
<property name="allowMultiline" value="true"/>
</properties>
</rule>
<!-- Force scope changes to increase indentation -->
<rule ref="Generic.WhiteSpace.ScopeIndent"/>
<!-- Require EOL at EOF -->
<rule ref="Generic.Files.EndFileNewline"/>
<!-- Forbid multiple classes per file -->
<rule ref="Generic.Files.OneClassPerFile"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one will be problematic for ORM where tests have their entities defined in the same file. Could be excluded on project level though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be excluded per-directory IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is possible to configure, even better. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would simply require a custom local phpcs.xml file per directory to be inspected, with overrides for disabled rules

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this covered by PSR1.Classes.ClassDeclaration.MultipleClasses (which is part of PSR-2)? If so, we already exclude things in the ORM

https://github.com/doctrine/doctrine2/blob/374e7ace49d864dad8cddbc55346447c8a6a2083/phpcs.xml.dist#L20-L22

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize it's already included 👍, we can remove these checks then, PSR-1 sniff provides check for classes/interfaces/traits at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 2ac121d.

<!-- Forbid multiple interfaces per file -->
<rule ref="Generic.Files.OneInterfacePerFile"/>
<!-- Forbid multiple traits per file -->
<rule ref="Generic.Files.OneTraitPerFile"/>
<!-- Forbid backtick operator -->
<rule ref="Generic.PHP.BacktickOperator"/>
<!-- Forbid short open tag -->
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<!-- Forbid `php_sapi_name()` function -->
<rule ref="Generic.PHP.SAPIUsage"/>
<!-- Forbid comments starting with # -->
<rule ref="PEAR.Commenting.InlineComment"/>
<!-- Disallow else if in favour elseif -->
<rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
<type>error</type>
</rule>
<!-- Forbid spaces around square brackets -->
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
<!-- Force array declaration structure -->
<rule ref="Squiz.Arrays.ArrayDeclaration">
<!-- Disable arrow alignment -->
<exclude name="Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned"/>
<!-- Uses indentation of only single space -->
<exclude name="Squiz.Arrays.ArrayDeclaration.KeyNotAligned"/>
<!-- Allow multiple values on a single line -->
<exclude name="Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed"/>
<!-- Disable alignment of braces -->
<exclude name="Squiz.Arrays.ArrayDeclaration.CloseBraceNotAligned"/>
<!-- Disable alignment of values with opening brace -->
<exclude name="Squiz.Arrays.ArrayDeclaration.ValueNotAligned"/>
</rule>
<!-- Forbid class being in a file with different name -->
<rule ref="Squiz.Classes.ClassFileName"/>
<!-- Force `self::` for self-reference, force lower-case self, forbid spaces around `::` -->
<rule ref="Squiz.Classes.SelfMemberReference"/>
<!-- Force phpDoc alignment -->
<rule ref="Squiz.Commenting.DocCommentAlignment">
<!-- Allow extra spaces after star, i.e. for indented annotations -->
<exclude name="Squiz.Commenting.DocCommentAlignment.SpaceAfterStar"/>
</rule>
<!-- Force rules for function phpDoc -->
<rule ref="Squiz.Commenting.FunctionComment">
<!-- Allow `@throws` without description -->
<exclude name="Squiz.Commenting.FunctionComment.EmptyThrows"/>
<!-- Does not work properly with PHP 7 / short-named types -->
<exclude name="Squiz.Commenting.FunctionComment.IncorrectParamVarName"/>
<!-- Does not support collections, i.e. `string[]` -->
<exclude name="Squiz.Commenting.FunctionComment.IncorrectTypeHint"/>
<!-- Forces incorrect types -->
<exclude name="Squiz.Commenting.FunctionComment.InvalidReturn"/>
<!-- Breaks with compound return types, i.e. `string|null` -->
<exclude name="Squiz.Commenting.FunctionComment.InvalidReturnNotVoid"/>
<!-- Breaks when all params are not documented -->
<exclude name="Squiz.Commenting.FunctionComment.InvalidTypeHint"/>
<!-- Doc comment is not required for every method -->
<exclude name="Squiz.Commenting.FunctionComment.Missing"/>
<!-- Do not require comments for `@param` -->
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
<!-- Do not require `@param` for all parameters -->
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<!-- Do not require `@return` for void methods -->
<exclude name="Squiz.Commenting.FunctionComment.MissingReturn"/>
<!-- Comments don't have to be sentences -->
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/>
<!-- Comments don't have to be sentences -->
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentNotCapital"/>
<!-- Breaks when all params are not documented -->
<exclude name="Squiz.Commenting.FunctionComment.ParamNameNoMatch"/>
<!-- Doesn't respect inheritance -->
<exclude name="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing"/>
<!-- Doesn't work with self as typehint -->
<exclude name="Squiz.Commenting.FunctionComment.TypeHintMissing"/>
</rule>
<!-- Forbid global functions -->
<rule ref="Squiz.Functions.GlobalFunction"/>
<!-- Forbid `AND` and `OR`, require `&&` and `||` -->
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
<!-- Forbid `global` -->
<rule ref="Squiz.PHP.GlobalKeyword"/>
<!-- Forbid functions inside functions -->
<rule ref="Squiz.PHP.InnerFunctions"/>
<!-- Require PHP function calls in lowercase -->
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
<!-- Forbid dead code -->
<rule ref="Squiz.PHP.NonExecutableCode"/>
<!-- Forbid `$this` inside static function -->
<rule ref="Squiz.Scope.StaticThisUsage"/>
<!-- Force whitespace before and after concatenation -->
<rule ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="spacing" value="1"/>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<!-- Forbid strings in `"` unless necessary -->
<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar">
<message>Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead</message>
</rule>
<!-- Forbid braces around string in `echo` -->
<rule ref="Squiz.Strings.EchoedStrings"/>
<!-- Forbid spaces in type casts -->
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<!-- Forbid blank line after function opening brace -->
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
<!-- Require space after language constructs -->
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
<!-- Require space around logical operators -->
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<!-- Forbid spaces around `->` operator -->
<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
<!-- Forbid superfluous whitespaces -->
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<!-- turned on by PSR2 -> turning back off -->
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
<!-- turned off by PSR2 -> turning back on -->
<severity>5</severity>
</rule>
</ruleset>
6 changes: 3 additions & 3 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ PHP CODE SNIFFER REPORT SUMMARY
----------------------------------------------------------------------
FILE ERRORS WARNINGS
----------------------------------------------------------------------
tests/input/concatenation_spacing.php 19 0
tests/input/concatenation_spacing.php 24 0
tests/input/not_spacing.php 7 0
tests/input/return_type_on_closures.php 21 0
tests/input/return_type_on_methods.php 16 0
tests/input/return_type_on_methods.php 17 0
----------------------------------------------------------------------
A TOTAL OF 63 ERRORS AND 0 WARNINGS WERE FOUND IN 4 FILES
A TOTAL OF 69 ERRORS AND 0 WARNINGS WERE FOUND IN 4 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 63 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions tests/fixed/not_spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

$test = 1;

if ( ! $test > 0) {
if (! $test > 0) {
echo 1;
} elseif ( ! $test === 0) {
} elseif (! $test === 0) {
echo 0;
} else {
echo -1;
}

while ( ! true) {
while (! true) {
echo 1;
}

do {
echo 1;
} while ( ! true);
} while (! true);