Skip to content

Commit 3c1e079

Browse files
Merge pull request #78 from VincentLanglet/improvements
💄 Multiple improvements
2 parents cd971c0 + aa82964 commit 3c1e079

File tree

8 files changed

+143
-163
lines changed

8 files changed

+143
-163
lines changed

SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ public function register()
4444
public function process(File $phpcsFile, $stackPtr)
4545
{
4646
$tokens = $phpcsFile->getTokens();
47-
if (in_array(
48-
$tokens[$stackPtr]['content'],
49-
$this->tags
50-
)
51-
) {
47+
if (in_array($tokens[$stackPtr]['content'], $this->tags)) {
5248
$phpcsFile->addError(
5349
'The %s annotation is forbidden to use',
5450
$stackPtr,

SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,8 @@ public function process(File $phpcsFile, $stackPtr)
118118
*
119119
* @return void
120120
*/
121-
protected function processReturn(
122-
File $phpcsFile,
123-
$stackPtr,
124-
$commentStart,
125-
$hasComment = true
126-
) {
121+
protected function processReturn(File $phpcsFile, $stackPtr, $commentStart, $hasComment = true)
122+
{
127123
// Check for inheritDoc if there is comment
128124
if ($hasComment && $this->isInheritDoc($phpcsFile, $stackPtr)) {
129125
return;
@@ -166,16 +162,51 @@ protected function processReturn(
166162
}
167163
}
168164

165+
/**
166+
* @param File $phpcsFile
167+
* @param int $stackPtr
168+
* @param int $commentStart
169+
*/
170+
protected function processThrows(File $phpcsFile, $stackPtr, $commentStart)
171+
{
172+
$tokens = $phpcsFile->getTokens();
173+
174+
$throw = null;
175+
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
176+
if ('@throws' === $tokens[$tag]['content']) {
177+
if (null !== $throw) {
178+
$error = 'Only 1 @throws tag is allowed in a function comment';
179+
$phpcsFile->addError($error, $tag, 'DuplicateThrow');
180+
181+
return;
182+
}
183+
184+
$throw = $tag;
185+
}
186+
}
187+
188+
if (null !== $throw) {
189+
$exception = null;
190+
if (T_DOC_COMMENT_STRING === $tokens[($throw + 2)]['code']) {
191+
$matches = [];
192+
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($throw + 2)]['content'], $matches);
193+
$exception = $matches[1];
194+
}
195+
196+
if (null === $exception) {
197+
$error = 'Exception type missing for @throws tag in function comment';
198+
$phpcsFile->addError($error, $throw, 'InvalidThrows');
199+
}
200+
}
201+
}
202+
169203
/**
170204
* @param File $phpcsFile
171205
* @param int $commentStart
172206
* @param bool $hasComment
173207
*/
174-
protected function processWhitespace(
175-
File $phpcsFile,
176-
$commentStart,
177-
$hasComment = true
178-
) {
208+
protected function processWhitespace(File $phpcsFile, $commentStart, $hasComment = true)
209+
{
179210
$tokens = $phpcsFile->getTokens();
180211
$before = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
181212

@@ -249,11 +280,8 @@ protected function isInheritDoc(File $phpcsFile, $stackPtr)
249280
*
250281
* @return void
251282
*/
252-
protected function processParams(
253-
File $phpcsFile,
254-
$stackPtr,
255-
$commentStart
256-
) {
283+
protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
284+
{
257285
if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
258286
return;
259287
}

SymfonyCustom/Sniffs/NamingConventions/ValidFileNameSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function process(File $phpcsFile, $stackPtr)
3636
return;
3737
}
3838

39-
$filenamePhp = str_replace('_', '', basename($filename, '.php'));
40-
$filenameInc = str_replace('_', '', basename($filename, '.inc'));
39+
$filenamePhp = basename($filename, '.php');
40+
$filenameInc = basename($filename, '.inc');
4141

4242
if (strlen($filenameInc) < strlen($filenamePhp)) {
4343
$filename = $filenameInc;

SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,28 @@ function toIgnore2()
103103
{
104104
$test = 42;
105105
}
106+
107+
/**
108+
* @throws Exception
109+
*/
110+
function throwTest()
111+
{
112+
throw new Exception();
113+
}
114+
115+
/**
116+
* @throws Exception|RuntimeException
117+
*/
118+
function throwTest2()
119+
{
120+
throw new Exception();
121+
}
122+
123+
/**
124+
* @throws Exception
125+
* @throws RuntimeException
126+
*/
127+
function throwTest3()
128+
{
129+
throw new Exception();
130+
}

SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,28 @@ function toIgnore2()
107107
{
108108
$test = 42;
109109
}
110+
111+
/**
112+
* @throws Exception
113+
*/
114+
function throwTest()
115+
{
116+
throw new Exception();
117+
}
118+
119+
/**
120+
* @throws Exception|RuntimeException
121+
*/
122+
function throwTest2()
123+
{
124+
throw new Exception();
125+
}
126+
127+
/**
128+
* @throws Exception
129+
* @throws RuntimeException
130+
*/
131+
function throwTest3()
132+
{
133+
throw new Exception();
134+
}

SymfonyCustom/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function getErrorList()
3030
83 => 1,
3131
93 => 1,
3232
102 => 1,
33+
125 => 1,
3334
];
3435
}
3536

SymfonyCustom/ruleset.xml

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,19 @@
3939
</properties>
4040
</rule>
4141

42-
<!-- **************** -->
43-
<!-- *** OTHERS *** -->
44-
<!-- **************** -->
42+
<!-- ************** -->
43+
<!-- *** MORE *** -->
44+
<!-- ************** -->
4545

46-
<!-- From djoos repo -->
47-
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
48-
<rule ref="Squiz.NamingConventions.ValidVariableName">
49-
<exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
50-
<exclude name="Squiz.NamingConventions.ValidVariableName.ContainsNumbers"/>
51-
</rule>
46+
<!-- Whitespace -->
5247
<rule ref="Generic.Formatting.SpaceAfterCast"/>
48+
<rule ref="Generic.WhiteSpace.SpreadOperatorSpacingAfter"/>
5349
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
54-
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
55-
<rule ref="Squiz.Scope.MemberVarScope"/>
56-
<rule ref="PEAR.Commenting.InlineComment"/>
57-
58-
<!-- From endouble repo -->
5950
<rule ref="Squiz.Strings.ConcatenationSpacing">
6051
<properties>
6152
<property name="ignoreNewlines" value="true" />
6253
</properties>
6354
</rule>
64-
<rule ref="SymfonyCustom.Commenting.FunctionComment">
65-
<exclude name="SymfonyCustom.Commenting.FunctionComment.MissingParamComment"/>
66-
</rule>
67-
68-
<!-- Added by VincentLanglet repo -->
69-
<!-- Whitespace -->
7055
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
7156
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
7257
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
@@ -82,15 +67,32 @@
8267
</rule>
8368
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
8469

70+
<!-- Disallow -->
71+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
72+
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
73+
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
74+
<rule ref="Generic.PHP.ForbiddenFunctions"/>
75+
<rule ref="PEAR.Commenting.InlineComment"/>
76+
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
77+
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
78+
79+
<!-- Others -->
80+
<rule ref="Squiz.Classes.ClassFileName"/>
8581
<rule ref="Squiz.Commenting.DocCommentAlignment">
86-
<!-- Allow to indent special annotations like @ORM\AttributeOverride -->
8782
<exclude name="Squiz.Commenting.DocCommentAlignment.SpaceAfterStar"/>
8883
</rule>
89-
<rule ref="Generic.PHP.ForbiddenFunctions"/>
90-
<rule ref="Squiz.PHP.DiscouragedFunctions"/>
91-
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
84+
<rule ref="Squiz.NamingConventions.ValidVariableName">
85+
<exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
86+
<exclude name="Squiz.NamingConventions.ValidVariableName.ContainsNumbers"/>
87+
</rule>
88+
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
89+
<rule ref="Squiz.Scope.MemberVarScope"/>
9290
<rule ref="Squiz.Strings.DoubleQuoteUsage">
9391
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
9492
</rule>
95-
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
93+
94+
<!-- Custom -->
95+
<rule ref="SymfonyCustom.Commenting.FunctionComment">
96+
<exclude name="SymfonyCustom.Commenting.FunctionComment.MissingParamComment"/>
97+
</rule>
9698
</ruleset>

0 commit comments

Comments
 (0)