Skip to content

Commit eb18941

Browse files
Merge pull request #29 from VincentLanglet/staging
✨ Add elseif, else, break rules
2 parents 5710c54 + 4431da9 commit eb18941

File tree

10 files changed

+238
-15
lines changed

10 files changed

+238
-15
lines changed

Symfony3Custom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public function process(File $phpcsFile, $stackPtr)
6060
$phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
6161

6262
return;
63-
} else {
64-
// The comment may not be required, we'll see in next checks
6563
}
6664
} else {
6765
$hasComment = true;

Symfony3Custom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public function process(File $phpcsFile, $stackPtr)
5757
|| 'T_COLON' === $prevLineTokens[0])
5858
) {
5959
return;
60-
} elseif (count($prevLineTokens) > 0) {
60+
}
61+
62+
if (count($prevLineTokens) > 0) {
6163
$fix = $phpcsFile->addFixableError(
6264
'Missing blank line before return statement',
6365
$stackPtr,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Sniffs\Formatting;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Checks whether there are else(if) or break statements after return or throw
10+
*/
11+
class ConditionalReturnOrThrowSniff implements Sniff
12+
{
13+
/**
14+
* @var array
15+
*/
16+
private $openers = [
17+
T_IF,
18+
T_CASE,
19+
];
20+
21+
/**
22+
* @var array
23+
*/
24+
private $conditions = [
25+
T_ELSEIF,
26+
T_ELSE,
27+
T_BREAK,
28+
];
29+
30+
/**
31+
* Registers the tokens that this sniff wants to listen for.
32+
*
33+
* @return array
34+
*/
35+
public function register()
36+
{
37+
return [
38+
T_THROW,
39+
T_RETURN,
40+
];
41+
}
42+
43+
/**
44+
* Called when one of the token types that this sniff is listening for is found.
45+
*
46+
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
47+
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack
48+
* where the token was found.
49+
*
50+
* @return void|int Optionally returns a stack pointer. The sniff will not be
51+
* called again on the current file until the returned stack
52+
* pointer is reached. Return (count($tokens) + 1) to skip
53+
* the rest of the file.
54+
*/
55+
public function process(File $phpcsFile, $stackPtr)
56+
{
57+
$tokens = $phpcsFile->getTokens();
58+
$opener = $phpcsFile->findPrevious($this->openers, $stackPtr);
59+
60+
if ($opener && $stackPtr <= $tokens[$opener]['scope_closer']) {
61+
$isClosure = $phpcsFile->findPrevious(T_CLOSURE, $stackPtr, $opener);
62+
63+
if (false !== $isClosure) {
64+
return;
65+
}
66+
67+
$condition = $phpcsFile->findNext($this->conditions, $stackPtr + 1);
68+
69+
if (false !== $condition) {
70+
$start = $stackPtr;
71+
$end = $condition;
72+
73+
$next = $phpcsFile->findNext($this->openers, $start + 1, $end);
74+
while (false !== $next) {
75+
if ($tokens[$condition]['level'] >= $tokens[$next]['level']) {
76+
$err = false;
77+
break;
78+
}
79+
80+
$start = $next;
81+
$next = $phpcsFile->findNext($this->openers, $start + 1, $end);
82+
}
83+
84+
if (!isset($err)) {
85+
$err = $tokens[$condition]['level'] === $tokens[$opener]['level'];
86+
}
87+
88+
if (true === $err) {
89+
$phpcsFile->addError(
90+
'Do not use else, elseif, break after if and case conditions which return or throw something',
91+
$condition,
92+
'Invalid'
93+
);
94+
}
95+
}
96+
}
97+
}
98+
}

Symfony3Custom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ private function compareNamespaces($namespace1, $namespace2)
138138

139139
if (true === $this->caseSensitive && strcmp($array1[$i], $array2[$i]) !== 0) {
140140
return strcmp($array1[$i], $array2[$i]);
141-
} elseif (false === $this->caseSensitive && strcasecmp($array1[$i], $array2[$i]) !== 0) {
141+
}
142+
143+
if (false === $this->caseSensitive && strcasecmp($array1[$i], $array2[$i]) !== 0) {
142144
return strcasecmp($array1[$i], $array2[$i]);
143145
}
144146
}

Symfony3Custom/Sniffs/NamingConventions/ValidFileNameSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function register()
2424
* Process.
2525
*
2626
* @param File $phpcsFile
27-
* @param int $stackPtr
27+
* @param int $stackPtr
2828
*
2929
* @return void
3030
*/
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
class Test
4+
{
5+
public function tester($dummy, $mergedOptions)
6+
{
7+
if (true === $dummy) {
8+
return null;
9+
}
10+
11+
if ('string' === $dummy) {
12+
if ('values' === $mergedOptions['some_default']) {
13+
return substr($dummy, 0, 5);
14+
}
15+
16+
return ucwords($dummy);
17+
}
18+
19+
if ('int' === $dummy) {
20+
if ('values' === $mergedOptions['some_default']) {
21+
return substr($dummy, 0, 5);
22+
} elseif (true) {
23+
return ucwords($dummy);
24+
}
25+
}
26+
27+
if ('a' === $dummy) {
28+
$mergedOptions['a'] = true;
29+
} else {
30+
$mergedOptions['a'] = false;
31+
}
32+
33+
if ('b' === $dummy) {
34+
$mergedOptions['a'] = true;
35+
36+
throw new Error();
37+
} else {
38+
$mergedOptions['a'] = false;
39+
}
40+
41+
switch (true) {
42+
case false:
43+
return null;
44+
break;
45+
default:
46+
break;
47+
}
48+
49+
if ('c' === $dummy) {
50+
$function = function () {
51+
return null;
52+
};
53+
} elseif ('d' === $dummy) {
54+
return null;
55+
}
56+
57+
if (1 === 1) {
58+
if (2 === 3) {
59+
return null;
60+
}
61+
} else {
62+
return null;
63+
}
64+
65+
return null;
66+
}
67+
68+
public function __construct()
69+
{
70+
if (1 === 1) {
71+
if (2 === 2) {
72+
return null;
73+
}
74+
} else {
75+
$a = 1;
76+
}
77+
78+
if ($a = 1) {
79+
$b = 2;
80+
}
81+
}
82+
}
83+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Tests\Formatting;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the ConditionalReturnOrThrow sniff.
9+
*
10+
* @group Symfony3Custom
11+
*/
12+
class ConditionalReturnOrThrowUnitTest 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 array(
25+
22 => 1,
26+
37 => 1,
27+
44 => 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 array();
42+
}
43+
}

Symfony3Custom/ruleset.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
<property name="ignoreNewlines" value="true" />
5454
</properties>
5555
</rule>
56-
<rule ref="Symfony3Custom.Commenting.FunctionComment.MissingParamComment">
57-
<severity>0</severity>
56+
<rule ref="Symfony3Custom.Commenting.FunctionComment">
57+
<exclude name="Symfony3Custom.Commenting.FunctionComment.MissingParamComment"/>
5858
</rule>
5959

6060
<!-- Added by VincentLanglet repo -->

docs/standards.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ We imported the [PSR2 Standard](./standards/psr2.md) with these overrides:
2424
## From symfony
2525

2626
We mainly respect the [Symfony Standard](./standards/symfony.md) but
27-
28-
- We do not respect this rule:
27+
we do not respect this rule:
2928

3029
- Declare all the arguments on the same line as the method/function name, no matter how many arguments there are
3130

32-
- We do not currently check this rule:
33-
34-
- Do not use `else`, `elseif`, `break` after `if` and `case` conditions which return or throw something
35-
3631
## Others
3732
### Imported
3833
- Do not use `<?` to define a php file
@@ -124,7 +119,7 @@ We mainly respect the [Symfony Standard](./standards/symfony.md) but
124119
<rule ref="Symfony3Custom.Commenting.VariableComment"/>
125120
```
126121

127-
- USE keywords should be alphabetically sorted
122+
- `use` keywords should be alphabetically sorted
128123

129124
```
130125
<rule ref="Symfony3Custom.Namespaces.AlphabeticallySortedUse"/>

docs/standards/symfony.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ Not checked because of the limit of 120 characters per line
108108

109109
- Do not use `else`, `elseif`, `break` after `if` and `case` conditions which return or throw something
110110

111-
Not covered
111+
```
112+
<rule ref="Symfony3Custom.Formatting.ConditionalReturnOrThrowSniff" />
113+
```
112114

113115
- Do not use spaces around `[` offset accessor and before `]` offset accessor
114116

0 commit comments

Comments
 (0)