Skip to content

Commit 24ef62a

Browse files
authored
Merge pull request #8 from Space48/S48-667-blank-line-before-return
S48-667: Blank line before return
2 parents e072b2b + 57ab083 commit 24ef62a

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

rulesets/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"indent": ["error", 4],
77
"semi": [2, "always"],
88
"yoda": ["error", "never"],
9+
"newline-before-return": "error",
910
"no-tabs": "error",
1011
"camelcase": "error",
1112
"max-len": ["error", { "code": 120, "comments": 150 }],
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Space48\CodeQuality\RuleSets\PhpCs\Space48Extra\Sniffs;
4+
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
use PHP_CodeSniffer\Files\File as CodeSnifferFile;
7+
8+
class BlankLineBeforeReturnSniff implements Sniff
9+
{
10+
/**
11+
* A list of tokenizers this sniff supports.
12+
*
13+
* @var array
14+
*/
15+
public $supportedTokenizers = array(
16+
'PHP',
17+
'JS',
18+
);
19+
20+
/**
21+
* Returns an array of tokens this test wants to listen for.
22+
*
23+
* @return array
24+
*/
25+
public function register()
26+
{
27+
return array(T_RETURN);
28+
}
29+
30+
/**
31+
* Processes this test, when one of its tokens is encountered.
32+
*
33+
* @param CodeSnifferFile $phpcsFile All the tokens found in the document.
34+
* @param int $stackPtr The position of the current token in
35+
* the stack passed in $tokens.
36+
*
37+
* @return void
38+
*/
39+
public function process(CodeSnifferFile $phpcsFile, $stackPtr)
40+
{
41+
$tokens = $phpcsFile->getTokens();
42+
$current = $stackPtr;
43+
$previousLine = $tokens[$stackPtr]['line'] - 1;
44+
$prevLineTokens = array();
45+
46+
while ($current >= 0 && $tokens[$current]['line'] >= $previousLine) {
47+
if ($tokens[$current]['line'] == $previousLine
48+
&& $tokens[$current]['type'] !== 'T_WHITESPACE'
49+
&& $tokens[$current]['type'] !== 'T_COMMENT'
50+
) {
51+
$prevLineTokens[] = $tokens[$current]['type'];
52+
}
53+
$current--;
54+
}
55+
56+
if (isset($prevLineTokens[0])
57+
&& ($prevLineTokens[0] === 'T_OPEN_CURLY_BRACKET'
58+
|| $prevLineTokens[0] === 'T_COLON')
59+
) {
60+
return;
61+
} else if (count($prevLineTokens) > 0) {
62+
$fix = $phpcsFile->addFixableError(
63+
'Missing blank line before return statement',
64+
$stackPtr,
65+
'MissedBlankLineBeforeRetrun'
66+
);
67+
68+
if ($fix === true) {
69+
$phpcsFile->fixer->beginChangeset();
70+
$i = 1;
71+
while($tokens[$stackPtr-$i]['type'] == "T_WHITESPACE") {
72+
$i++;
73+
}
74+
$phpcsFile->fixer->addNewLine($stackPtr-$i);
75+
$phpcsFile->fixer->endChangeset();
76+
}
77+
}
78+
79+
return;
80+
}
81+
}

rulesets/PhpCs/Space48Extra/extra.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
<property name="set" value="compiler_optimized" />
77
</properties>
88
</rule>
9+
<rule ref="vendor/space48/magento2-code-quality/rulesets/PhpCs/Space48Extra/Sniffs/BlankLineBeforeReturnSniff.php">
10+
</rule>
911
</ruleset>

src/Configuration/FixerConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class FixerConfig extends \GrumPHP\Configuration\Model\FixerConfig
77
/**
88
* {@inheritdoc }
99
*/
10-
public static function fromArray(array $config): self
10+
public static function fromArray(array $config): \GrumPHP\Configuration\Model\FixerConfig
1111
{
1212
return new self(
1313
($config['enabled'] ?? false),

0 commit comments

Comments
 (0)