Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit 5498ab3

Browse files
committed
Verticle empty lines
1 parent 332a257 commit 5498ab3

File tree

5 files changed

+240
-13
lines changed

5 files changed

+240
-13
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Vertical Empty Lines
4+
*
5+
* @package CodeIgniter4-Standard
6+
* @author Louis Linehan <louis.linehan@gmail.com>
7+
* @copyright 2017 Louis Linehan
8+
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
9+
*/
10+
11+
namespace CodeIgniter4\Sniffs\WhiteSpace;
12+
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Files\File;
15+
use CodeIgniter4\Util\Common;
16+
17+
/**
18+
* Vertical Empty Lines Sniff
19+
*
20+
* Checks for consecutive empty vertical lines.
21+
*
22+
* @author Louis Linehan <louis.linehan@gmail.com>
23+
*/
24+
class VerticalEmptyLinesSniff implements Sniff
25+
{
26+
27+
/**
28+
* Consecutive empty vertical lines allowed.
29+
*
30+
* @var integer
31+
*/
32+
public $allowed = 1;
33+
34+
35+
/**
36+
* Returns an array of tokens this test wants to listen for.
37+
*
38+
* @return array
39+
*/
40+
public function register()
41+
{
42+
return array(T_OPEN_TAG);
43+
44+
}//end register()
45+
46+
47+
/**
48+
* Processes this test, when one of its tokens is encountered.
49+
*
50+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
51+
* @param int $stackPtr The position of the current token in
52+
* the stack passed in $tokens.
53+
*
54+
* @return int
55+
*/
56+
public function process(File $phpcsFile, $stackPtr)
57+
{
58+
$errors = array();
59+
$tokens = $phpcsFile->getTokens();
60+
for ($i = 1; $i < $phpcsFile->numTokens; $i++) {
61+
$nextContentPtr = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
62+
63+
$lines = ($tokens[$nextContentPtr]['line'] - $tokens[$i]['line'] - 1);
64+
$errorLine = (($nextContentPtr - $lines) + $this->allowed - 1);
65+
66+
if ($lines > ($this->allowed) && in_array($errorLine, $errors) === false) {
67+
$errors[] = $errorLine;
68+
69+
$data = array(
70+
$this->allowed,
71+
Common::pluralize('line', $this->allowed),
72+
);
73+
$error = 'Expected only %s empty %s';
74+
$fix = $phpcsFile->addFixableError($error, $errorLine, 'VerticalEmptyLines', $data);
75+
if ($fix === true) {
76+
$phpcsFile->fixer->replaceToken($errorLine, '');
77+
}
78+
}
79+
}
80+
81+
// Ignore the rest of the file.
82+
return ($phpcsFile->numTokens + 1);
83+
84+
}//end process()
85+
86+
87+
}//end class
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
class Test
4+
{
5+
6+
public function test()
7+
{
8+
$a = 1;
9+
10+
$b = 2;
11+
}
12+
13+
14+
public function test2()
15+
{
16+
$a = 1;
17+
$b = 2;
18+
19+
20+
$b = 3;
21+
}
22+
23+
24+
25+
public function test3()
26+
{
27+
$a = 1;
28+
$b = 2;
29+
30+
31+
$c = 3;
32+
}
33+
34+
}
35+
36+
$a = 1;
37+
$b = 2;
38+
39+
40+
$c = 3;
41+
42+
43+
$d = 4;
44+
45+
46+
47+
48+
49+
$e = 5;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
class Test
4+
{
5+
6+
public function test()
7+
{
8+
$a = 1;
9+
10+
$b = 2;
11+
}
12+
13+
public function test2()
14+
{
15+
$a = 1;
16+
$b = 2;
17+
18+
$b = 3;
19+
}
20+
21+
public function test3()
22+
{
23+
$a = 1;
24+
$b = 2;
25+
26+
$c = 3;
27+
}
28+
29+
}
30+
31+
$a = 1;
32+
$b = 2;
33+
34+
$c = 3;
35+
36+
$d = 4;
37+
38+
$e = 5;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Vertical Empty Lines Unit Test
4+
*
5+
* @package CodeIgniter4-Standard
6+
* @author Louis Linehan <louis.linehan@gmail.com>
7+
* @copyright 2017 Louis Linehan
8+
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
9+
*/
10+
11+
namespace CodeIgniter4\Tests\WhiteSpace;
12+
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
14+
15+
class VerticalEmptyLinesUnitTest extends AbstractSniffUnitTest
16+
{
17+
18+
19+
/**
20+
* Returns the lines where errors should occur.
21+
*
22+
* The key of the array should represent the line number and the value
23+
* should represent the number of errors that should occur on that line.
24+
*
25+
* @return array<int, int>
26+
*/
27+
public function getErrorList()
28+
{
29+
return array(
30+
13 => 1,
31+
19 => 1,
32+
23 => 1,
33+
24 => 1,
34+
30 => 1,
35+
38 => 1,
36+
41 => 1,
37+
44 => 1,
38+
45 => 1,
39+
46 => 1,
40+
47 => 1,
41+
);
42+
43+
}//end getErrorList()
44+
45+
46+
/**
47+
* Returns the lines where warnings should occur.
48+
*
49+
* The key of the array should represent the line number and the value
50+
* should represent the number of warnings that should occur on that line.
51+
*
52+
* @return array<int, int>
53+
*/
54+
public function getWarningList()
55+
{
56+
return array();
57+
58+
}//end getWarningList()
59+
60+
61+
}//end class

CodeIgniter4/ruleset.xml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@
135135
-->
136136
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
137137
<!--
138+
Checks for more than one consecutive empty line.
139+
-->
140+
<rule ref="CodeIgniter4.WhiteSpace.VerticalEmptyLines"/>
141+
<!--
138142
There should be one class per file, execept for cases where
139143
breaking this rule makes more sense.
140144
'Exception.php',
@@ -229,25 +233,13 @@
229233
</properties>
230234
</rule>
231235
<!--
232-
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
233-
<properties>
234-
<property name="spacing" value="1" />
235-
</properties>
236-
</rule>
237-
@todo Clash here with function scopes using if (function_exists.
238-
-->
239-
<!--
240236
Checks there are no blank lines after a function opening brace.
241237
-->
242238
<rule ref="Squiz.WhiteSpace.FunctionOpeningBraceSpace"/>
243239
<!--
244240
Checks there are no blank lines before a function closing brace.
245241
-->
246-
<rule ref="CodeIgniter4.WhiteSpace.FunctionClosingBraceSpace">
247-
<properties>
248-
<property name="allowedLines" value="0" />
249-
</properties>
250-
</rule>
242+
<rule ref="CodeIgniter4.WhiteSpace.FunctionClosingBraceSpace"/>
251243
<!--
252244
Checks for each declarations are styled properly.
253245
-->

0 commit comments

Comments
 (0)