Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Additional sniffs and tests #10

Merged
merged 17 commits into from
Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
81 changes: 56 additions & 25 deletions src/ZendCodingStandard/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<exclude name="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/>
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose"/>
<!-- Replaced by WebimpressCodingStandard.WhiteSpace.CommaSpacing -->
<exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpaceBeforeComma"/>
<exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.NoSpaceBeforeArg"/>
</rule>

<!-- Force array element indentation with 4 spaces -->
Expand All @@ -34,6 +37,8 @@
<!-- Forbid blank line after opening braces and before closing braces -->
<rule ref="WebimpressCodingStandard.WhiteSpace.BraceBlankLine"/>

<!-- Forbid FQN for classes/traits/interfaces/constants/functions -->
<rule ref="WebimpressCodingStandard.PHP.DisallowFqn"/>
<!-- Import internal constants -->
<rule ref="WebimpressCodingStandard.PHP.ImportInternalConstant"/>
<!-- Import internal functions -->
Expand All @@ -46,22 +51,62 @@
<!-- Requires one space after namespace keyword -->
<rule ref="WebimpressCodingStandard.WhiteSpace.Namespace"/>
<!-- Forbid spaces around namespace separator -->
<!-- TODO: Cannot be included because of error in SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly -->
<!--<rule ref="WebimpressCodingStandard.WhiteSpace.NamespaceSeparator"/>-->
<rule ref="WebimpressCodingStandard.WhiteSpace.NamespaceSeparator"/>

<!-- One space after break/continue with argument, remove redundant 1 -->
<rule ref="WebimpressCodingStandard.ControlStructures.BreakAndContinue"/>
<!-- Forbid continue in switch; use break instead -->
<rule ref="WebimpressCodingStandard.ControlStructures.ContinueInSwitch"/>

<!-- Forbid boolean operator at the end of the line -->
<rule ref="WebimpressCodingStandard.Operators.BooleanOperator"/>
<!-- Formatting of ternary operator - content must be after ?/:, not in new line -->
<rule ref="WebimpressCodingStandard.Operators.TernaryOperator"/>

<!-- Abstract class must have Abstract* prefix -->
<rule ref="WebimpressCodingStandard.NamingConventions.AbstractClass"/>
<!-- Exception must have *Exception suffix -->
<rule ref="WebimpressCodingStandard.NamingConventions.Exception"/>
<!-- Interface must have *Interface suffix -->
<rule ref="WebimpressCodingStandard.NamingConventions.Interface"/>
<!-- Trait must have *Traif suffix -->
<rule ref="WebimpressCodingStandard.NamingConventions.Trait"/>
<!-- Require camelCase variable names -->
<rule ref="WebimpressCodingStandard.NamingConventions.ValidVariableName"/>

<!-- Use the correct class name, i.e. DateTime not Datetime, etc @todo: test -->
<rule ref="WebimpressCodingStandard.PHP.CorrectClassNameCase"/>

<!-- Only one blank line allowed -->
<rule ref="WebimpressCodingStandard.WhiteSpace.BlankLine"/>

<!-- Detects for-loops that can be simplified to a while-loop -->
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<!-- Detects unconditional if- and elseif-statements -->
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>

<!-- Forbid inline comments after closing curly bracket, like // end if etc -->
<rule ref="WebimpressCodingStandard.Commenting.NoInlineCommentAfterCurlyClose"/>
<!-- Use correct case of default PHPDocs and PHPUnit tags @var not @VAR etc -->
<rule ref="WebimpressCodingStandard.Commenting.TagCase"/>
<!-- Heredoc/nowdoc tag uppercase without spaces -->
<rule ref="WebimpressCodingStandard.Formatting.Heredoc"/>

<!-- Forbid space after splat operator -->
<rule ref="WebimpressCodingStandard.Formatting.NoSpaceAfterSplat"/>
<!-- Forbid reference operator before new, and formatting of the reference operator -->
<rule ref="WebimpressCodingStandard.Formatting.Reference"/>
<!-- Use ::class instead of class string reference @todo: add tests -->
<rule ref="WebimpressCodingStandard.Formatting.StringClassReference"/>
<!-- Single line between methods in class -->
<rule ref="WebimpressCodingStandard.Methods.LineAfter"/>

<!-- Forbid space before comma, require single space after comma @todo: add more tests, like echo 1,2; -->
<rule ref="WebimpressCodingStandard.WhiteSpace.CommaSpacing"/>

<!-- Forbid concatenation character at the end of the line @todo: add tests -->
<rule ref="WebimpressCodingStandard.Strings.NoConcatenationAtTheEnd"/>

<!-- Forbid duplicate classes -->
<rule ref="Generic.Classes.DuplicateClassName"/>
<!-- Forbid empty statements -->
Expand Down Expand Up @@ -177,6 +222,10 @@
<element value="@package"/>
<element value="@subpackage"/>
<element value="@version"/>
<element value="@expectedException"/>
<element value="@expectedExceptionCode"/>
<element value="@expectedExceptionMessage"/>
<element value="@expectedExceptionMessageRegExp"/>
</property>
</properties>
</rule>
Expand Down Expand Up @@ -224,20 +273,6 @@
<rule ref="SlevomatCodingStandard.Namespaces.MultipleUsesPerLine"/>
<!-- Require newlines around namespace declaration -->
<rule ref="SlevomatCodingStandard.Namespaces.NamespaceSpacing"/>
<!-- Forbid using absolute class name references (except global ones) -->
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFallbackGlobalConstants" value="false"/>
<property name="allowFallbackGlobalFunctions" value="false"/>
<property name="allowFullyQualifiedGlobalClasses" value="false"/>
<property name="allowFullyQualifiedGlobalConstants" value="false"/>
<property name="allowFullyQualifiedGlobalFunctions" value="false"/>
<property name="allowFullyQualifiedNameForCollidingClasses" value="true"/>
<property name="allowFullyQualifiedNameForCollidingConstants" value="true"/>
<property name="allowFullyQualifiedNameForCollidingFunctions" value="true"/>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<!-- Forbid useless alias for classes, constants and functions -->
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
<!-- Require the usage of assignment operators, eg `+=`, `.=` when possible -->
Expand All @@ -247,17 +282,13 @@
<!-- Forbid use of longhand cast operators -->
<rule ref="SlevomatCodingStandard.PHP.TypeCast"/>
<!-- Require presence of declare(strict_types=1) -->
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="2"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
<property name="newlinesCountAfterDeclare" value="2"/>
</properties>
</rule>
<rule ref="WebimpressCodingStandard.Files.DeclareStrictTypes"/>
<!-- Format of strict type declaration -->
<rule ref="WebimpressCodingStandard.PHP.DeclareStrictTypes"/>
<!-- Forbid useless parentheses -->
<rule ref="WebimpressCodingStandard.Formatting.UnnecessaryParentheses"/>
<!-- Forbid useless semicolon `;` -->
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<!-- Forbid redundant semicolons -->
<rule ref="WebimpressCodingStandard.PHP.RedundantSemicolon"/>
Copy link
Member

Choose a reason for hiding this comment

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

Any reason for replacing these Slevomat tools with Webimpress ones, besides ownership?

<!-- Require use of short versions of scalar types (i.e. int instead of integer) -->
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
<!-- Require ? when default value is null -->
Expand Down
24 changes: 13 additions & 11 deletions test/expected-report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ test/fixable/class-properties.php 6 0
test/fixable/classes-traits-interfaces.php 13 0
test/fixable/closures.php 19 0
test/fixable/commenting.php 17 0
test/fixable/concatenation-spacing.php 15 0
test/fixable/concatenation-spacing.php 17 0
test/fixable/control-structures.php 5 0
test/fixable/example-class.php 30 0
test/fixable/example-class.php 26 0
test/fixable/extends-and-implements-multiline.php 10 0
test/fixable/extends-and-implements.php 5 0
test/fixable/forbidden-comments.php 4 0
test/fixable/forbidden-comments.php 6 0
test/fixable/forbidden-functions.php 6 0
test/fixable/heredoc-nowdoc.php 6 0
test/fixable/LowCaseTypes.php 2 0
test/fixable/method-and-function-arguments.php 11 0
test/fixable/method-and-function-calls.php 13 0
test/fixable/namespaces-spacing.php 3 0
test/fixable/method-and-function-arguments.php 16 0
test/fixable/method-and-function-calls.php 15 0
test/fixable/namespaces-spacing.php 7 0
test/fixable/naming.php 11 0
test/fixable/new-with-parentheses.php 19 0
test/fixable/not-spacing.php 14 0
test/fixable/operators.php 9 0
test/fixable/not-spacing.php 20 1
test/fixable/operators.php 12 0
test/fixable/return-type-on-methods.php 17 0
test/fixable/semicolon-spacing.php 3 0
test/fixable/statement-alignment.php 19 0
test/fixable/test-case.php 4 0
test/fixable/traits-uses.php 9 0
test/fixable/UnusedVariables.php 1 0
test/fixable/useless-semicolon.php 2 0
test/fixable/useless-semicolon.php 7 0
test/fixable/variable-names.php 5 2
test/fixable/visibility-declaration.php 1 0
----------------------------------------------------------------------
A TOTAL OF 323 ERRORS AND 3 WARNINGS WERE FOUND IN 30 FILES
A TOTAL OF 365 ERRORS AND 4 WARNINGS WERE FOUND IN 32 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 276 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 302 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


4 changes: 2 additions & 2 deletions test/fixable/classes-traits-interfaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Foo

}

trait Bar
trait BarTrait
{

public $var;

}

interface Baz
interface BazInterface
{

public function big() : int;
Expand Down
10 changes: 5 additions & 5 deletions test/fixable/forbidden-comments.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

/** Created by PhpStorm. */

declare(strict_types=1);

/** Created by PhpStorm. */

namespace Test;

class Foo
class ForbiddenComments
{
/**
* Constructor.
Expand All @@ -30,5 +30,5 @@ public function getBar() : int
public function getBaz() : int
{
return 456;
}
}
} // end getBaz
} // end class
25 changes: 25 additions & 0 deletions test/fixable/heredoc-nowdoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

$hereDoc = <<<hereDoc
<?php

declare(strict_types=1);

class %s extends %s
{
}
hereDoc;

$hereDoc2 = <<< EOT
other...
EOT;

$nowDoc1 = <<< "now_doc"
some content here
now_doc;

$nowDoc2 = <<< 'now'
now content!
now;
13 changes: 13 additions & 0 deletions test/fixable/method-and-function-arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ public function returnTypeVariations(string $arg1 = null, string $arg2 = null):?
{
return $arg1 ?? $arg2;
}

public function splat(... $args)
{
return $args ?? [];
}

public function reference(& $ref, array $arr)
{
$obj =& new Foo();
$bar =& $arr;

return $obj->process($bar);
}
}
2 changes: 1 addition & 1 deletion test/fixable/namespaces-spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
->format(DATE_RFC3339)
);

// new \ Bar \ Baz();
new \ Bar \ Baz();
25 changes: 25 additions & 0 deletions test/fixable/naming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace NamingConventions;

abstract class MyAbstractClass
{
}

interface InterfaceHello
{
}

trait TraitHello
{
}

class InvalidValue extends Exception
{
}

class InvalidName extends InvalidArgumentException
{
}
16 changes: 16 additions & 0 deletions test/fixable/not-spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@

new DateTime();
new\DateTime();


class SingleLineBetweenMethods
{
public function __construct()
{
}
public function method()
{
}


public function twoLines()
{
}
}
9 changes: 9 additions & 0 deletions test/fixable/operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@
echo Something
::
BAR;

if ($a &&
$b) {
echo 1;
}

$a ?
$b :
$c;
10 changes: 10 additions & 0 deletions test/fixable/useless-semicolon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@
for (;;) {
echo 'To infity and beyond';
};

echo 1;;

$closure = function () {
;
};

$anonym = new class()
{
};
2 changes: 1 addition & 1 deletion test/fixable/visibility-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace VisibilityDeclaration;

abstract class ClassName
abstract class AbstractClassName
{
protected static $foo;

Expand Down
4 changes: 2 additions & 2 deletions test/fixed/classes-traits-interfaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class Foo
public $bar;
}

trait Bar
trait BarTrait
{
public $var;
}

interface Baz
interface BazInterface
{
public function big() : int;
}
6 changes: 3 additions & 3 deletions test/fixed/concatenation-spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
. '2'
. '3';

$string = '1' .
'2' .
'3';
$string = '1'
. '2'
. '3';
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't these two be aligned with the equal sign?

$string = '1'
        . '2'
        . '3';

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe it should but we don't have any sniff like that... And what if concatenation is in let say sprintf:

throw new \Exception(sprintf(
    'This is very very very long error message'
    . ' with some exception: %s',
    $e->getMessage()
));

There is so many cases. ScopeIndent from PHP_CodeSniffer doesn't include so many cases and my is super strict, and controversial in some places ;-) We can try, but ... hah

Loading