Skip to content

Commit

Permalink
Enforce blank lines between class members
Browse files Browse the repository at this point in the history
  • Loading branch information
nicwortel committed Jan 11, 2024
1 parent 7791ca8 commit 4a93b2b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
8 changes: 8 additions & 0 deletions NicWortel/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
</properties>
</rule>

<!-- Require a blank line between class members (constants, properties, methods) -->
<!-- Between properties and constants without annotations or attributes, the blank line is optional -->
<rule ref="SlevomatCodingStandard.Classes.ClassMemberSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.ConstantSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.PropertySpacing"/>
<rule ref="SlevomatCodingStandard.Classes.MethodSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.EnumCaseSpacing"/>

<!-- Use PHP native type hints whenever possible. Use docblocks only when
native type hints don't provide enough information. -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.1",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"slevomat/coding-standard": "^8.8",
"slevomat/coding-standard": "^8.9",
"squizlabs/php_codesniffer": "^3.7"
},
"require-dev": {
Expand Down
26 changes: 26 additions & 0 deletions tests/correct/class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Foo;

class Bar
{
private const FOO = 'foo';

private const BAR = 'bar';

private string $foo = 'foo';

private string $bar = 'bar';

public function foo(): string
{
return $this->foo;
}

public function bar(): string
{
return $this->bar;
}
}
3 changes: 3 additions & 0 deletions tests/expected.csv
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"tests/incorrect/imports-unused.php",8,1,error,"Type array_filter is not used in this file.",SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse,5,1
"tests/incorrect/imports-unused.php",8,26,error,"Header blocks must be separated by a single blank line",PSR12.Files.FileHeader.SpacingAfterBlock,5,1
"tests/incorrect/imports-unused.php",9,1,error,"Type PHP_VERSION is not used in this file.",SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse,5,1
"tests/incorrect/members-newlines.php",11,20,error,"Expected 1 blank line between class members, found 0.",SlevomatCodingStandard.Classes.ClassMemberSpacing.IncorrectCountOfBlankLinesBetweenMembers,5,1
"tests/incorrect/members-newlines.php",13,12,error,"Expected 1 blank line after method, found 0.",SlevomatCodingStandard.Classes.MethodSpacing.IncorrectLinesCountBetweenMethods,5,1
"tests/incorrect/members-newlines.php",13,12,error,"Expected 1 blank line between class members, found 0.",SlevomatCodingStandard.Classes.ClassMemberSpacing.IncorrectCountOfBlankLinesBetweenMembers,5,1
"tests/incorrect/namespaces.php",10,12,error,"Class \DateTime should not be referenced via a fully qualified name, but via a use statement.",SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName,5,1
"tests/incorrect/namespaces.php",12,10,error,"Function array_filter() should not be referenced via a fallback global name, but via a use statement.",SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName,5,1
"tests/incorrect/namespaces.php",14,12,error,"Constant PHP_VERSION should not be referenced via a fallback global name, but via a use statement.",SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName,5,1
Expand Down
21 changes: 21 additions & 0 deletions tests/incorrect/members-newlines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Foo;

class Foo
{
private const FOO = 'foo';
private const BAR = 'bar';
private string $foo = 'foo';
private string $bar = 'bar';
public function foo(): string
{
return $this->foo;
}
public function bar(): string
{
return $this->bar;
}
}

0 comments on commit 4a93b2b

Please sign in to comment.