Skip to content

Commit b0814e1

Browse files
Merge pull request #52 from VincentLanglet/improve
✨ Minor improvements
2 parents e181258 + ad2ee79 commit b0814e1

13 files changed

+192
-15
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace SymfonyCustom\Sniffs\Namespaces;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Ensure there is a blank line before namespace.
10+
* PSR2 checks only for blank line after namespace.
11+
*/
12+
class NamespaceDeclarationSniff implements Sniff
13+
{
14+
/**
15+
* Returns an array of tokens this test wants to listen for.
16+
*
17+
* @return array
18+
*/
19+
public function register()
20+
{
21+
return [T_NAMESPACE];
22+
}
23+
24+
/**
25+
* Processes this test, when one of its tokens is encountered.
26+
*
27+
* @param File $phpcsFile The file being scanned.
28+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
29+
*
30+
* @return void
31+
*/
32+
public function process(File $phpcsFile, $stackPtr)
33+
{
34+
$tokens = $phpcsFile->getTokens();
35+
36+
for ($i = $stackPtr - 1; $i > 0; $i--) {
37+
if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
38+
continue;
39+
}
40+
41+
break;
42+
}
43+
44+
// The $i var now points to the last token on the line before the
45+
// namespace declaration, which must be a blank line.
46+
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $i, null, true);
47+
if (false === $previous) {
48+
return;
49+
}
50+
51+
$diff = ($tokens[$i]['line'] - $tokens[$previous]['line']);
52+
if (1 === $diff) {
53+
return;
54+
}
55+
56+
if ($diff < 0) {
57+
$diff = 0;
58+
}
59+
60+
$error = 'There must be one blank line before the namespace declaration';
61+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineBefore');
62+
63+
if (true === $fix) {
64+
if (0 === $diff) {
65+
$phpcsFile->fixer->addNewlineBefore($stackPtr);
66+
} else {
67+
$phpcsFile->fixer->beginChangeset();
68+
for ($x = $stackPtr - 1; $x > $previous; $x--) {
69+
if ($tokens[$x]['line'] === $tokens[$previous]['line']) {
70+
break;
71+
}
72+
73+
$phpcsFile->fixer->replaceToken($x, '');
74+
}
75+
76+
$phpcsFile->fixer->addNewlineBefore($stackPtr);
77+
$phpcsFile->fixer->endChangeset();
78+
}
79+
}
80+
}
81+
}

SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ public function process(File $phpcsFile, $stackPtr)
109109
true
110110
);
111111

112-
// If a backslash is used before the class name then this is some other use statement.
113-
if (T_USE !== $tokens[$beforeUsage]['code'] && T_NS_SEPARATOR !== $tokens[$beforeUsage]['code']) {
112+
if (!in_array($tokens[$beforeUsage]['code'], [
113+
T_USE,
114+
// If a backslash is used before the class name then this is some other use statement.
115+
T_NS_SEPARATOR,
116+
// If an object operator is used before the class name then is a class property.
117+
T_OBJECT_OPERATOR,
118+
])) {
114119
return;
115120
}
116121

SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Util\Tokens;
88

99
/**
10-
* Checks that there is no white space before a closing bracket, for ")" and "}".
10+
* Checks that there is no white space before a closing bracket, for ")", "}", and array bracket.
1111
* Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
1212
*/
1313
class CloseBracketSpacingSniff implements Sniff

SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHP_CodeSniffer\Sniffs\Sniff;
77

88
/**
9-
* Checks that there is no white space after an opening bracket, for "(" and "{".
9+
* Checks that there is no white space after an opening bracket, for "(", "{", and array bracket.
1010
* Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
1111
*/
1212
class OpenBracketSpacingSniff implements Sniff
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace MyProject;
3+
4+
namespace Foo;
5+
6+
7+
namespace Toto;
8+
;namespace FooBar;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MyProject;
4+
5+
namespace Foo;
6+
7+
namespace Toto;
8+
;
9+
10+
namespace FooBar;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace SymfonyCustom\Tests\Namespaces;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the UnusedUse sniff.
9+
*
10+
* @group SymfonyCustom
11+
*/
12+
class NamespaceDeclarationUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @return array<int, int>
21+
*/
22+
public function getErrorList()
23+
{
24+
return [
25+
2 => 1,
26+
7 => 1,
27+
8 => 1,
28+
];
29+
}
30+
31+
/**
32+
* Returns the lines where warnings should occur.
33+
*
34+
* The key of the array should represent the line number and the value
35+
* should represent the number of errors that should occur on that line.
36+
*
37+
* @return array(int => int)
38+
*/
39+
public function getWarningList()
40+
{
41+
return [];
42+
}
43+
}

SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Client;
1313
use Clients;
1414
use PasClient;
1515
use Somewhere\Something;
16+
use Somewhere\SomethingElse;
1617

1718
class Container
1819
{
@@ -28,7 +29,8 @@ class Container
2829
function test (Bar $bar): Something
2930
{
3031
/** @var Client|Clients[]|PasClient $client */
32+
$client = [$this->somethingElse];
3133

32-
return;
34+
return $client;
3335
}
3436
}

SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class Container
2727
function test (Bar $bar): Something
2828
{
2929
/** @var Client|Clients[]|PasClient $client */
30+
$client = [$this->somethingElse];
3031

31-
return;
32+
return $client;
3233
}
3334
}

SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function getErrorList()
2323
{
2424
return [
2525
11 => 1,
26+
16 => 1,
2627
];
2728
}
2829

SymfonyCustom/ruleset.xml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<!-- See http://symfony.com/doc/current/contributing/code/standards.html -->
44
<description>The SymfonyCustom coding standard.</description>
55

6+
<arg name="extensions" value="php,inc"/>
7+
<exclude-pattern>*/vendor/*</exclude-pattern>
8+
69
<!-- ************** -->
710
<!-- *** PSR2 *** -->
811
<!-- ************** -->
@@ -60,12 +63,10 @@
6063
</rule>
6164

6265
<!-- Added by VincentLanglet repo -->
63-
<rule ref="Generic.PHP.ForbiddenFunctions"/>
64-
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
65-
<rule ref="Squiz.Strings.DoubleQuoteUsage">
66-
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
67-
</rule>
66+
<!-- Whitespace -->
67+
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
6868
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
69+
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
6970
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
7071
<properties>
7172
<property name="ignoreNewlines" value="true"/>
@@ -77,9 +78,19 @@
7778
</properties>
7879
</rule>
7980
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
81+
82+
<!-- Comment -->
83+
<rule ref="Generic.Commenting.DocComment">
84+
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
85+
</rule>
86+
<rule ref="Squiz.Commenting.DocCommentAlignment"/>
87+
88+
<!-- Others -->
89+
<rule ref="Generic.PHP.ForbiddenFunctions"/>
90+
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
8091
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
92+
<rule ref="Squiz.Strings.DoubleQuoteUsage">
93+
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
94+
</rule>
8195
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
82-
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
83-
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
84-
<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing"/>
8596
</ruleset>

docs/standards.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ we do not respect this rule:
138138
</rule>
139139
```
140140

141+
- DocComment should start on a new line, end on a new line and be correctly indented
142+
143+
```
144+
<rule ref="Generic.Commenting.DocComment">
145+
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
146+
</rule>
147+
<rule ref="Squiz.Commenting.DocCommentAlignment"/>
148+
```
149+
141150
### Custom
142151
- Some others checks are made about array (`=>` alignments and indentation)
143152

@@ -193,3 +202,9 @@ we do not respect this rule:
193202
```
194203
<rule ref="SymfonyCustom.WhiteSpace.DocCommentTagSpacing"/>
195204
```
205+
206+
- Add a single space before `namespace` declaration
207+
208+
```
209+
<rule ref="SymfonyCustom.Namespaces.NamespaceDeclaration"/>
210+
```

docs/standards/symfony.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Covered by `PSR1` completed by
182182

183183
- Add PHPDoc blocks for all classes, methods, and functions
184184

185-
We added exceptions for functions `setUp`, `tearDown` and `tests` with no `@param` or `@return`
185+
We added exceptions for functions with no `@param` or `@return`
186186
```
187187
<rule ref="SymfonyCustom.Commenting.ClassComment"/>
188188
<rule ref="SymfonyCustom.Commenting.FunctionComment"/>

0 commit comments

Comments
 (0)