Skip to content

Commit eb40b31

Browse files
committed
Instaclick coding standard: blank line before "if"
1 parent 8e3c88b commit eb40b31

8 files changed

+200
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Symfony2-coding-standard (phpcs standard)
5+
*
6+
* PHP version 5
7+
*
8+
* @category PHP
9+
* @package PHP_CodeSniffer-Symfony2
10+
* @author Symfony2-phpcs-authors <[email protected]>
11+
* @license http://spdx.org/licenses/MIT MIT License
12+
* @version GIT: master
13+
* @link https://github.com/opensky/Symfony2-coding-standard
14+
*/
15+
16+
/**
17+
* Symfony2_Sniffs_Instaclick_BlankLineBeforeIfSniff.
18+
*
19+
* Throws errors if there's no blank line before if statements. Symfony
20+
* coding standard specifies: "Add a blank line before if statements,
21+
* unless the if is alone inside a statement-group (like an if statement);"
22+
*
23+
* @category PHP
24+
* @package PHP_CodeSniffer-Symfony2
25+
* @author Dave Hauenstein <[email protected]>
26+
* @license http://spdx.org/licenses/MIT MIT License
27+
* @link https://github.com/opensky/Symfony2-coding-standard
28+
*/
29+
class Symfony2_Sniffs_Instaclick_BlankLineBeforeIfSniff implements PHP_CodeSniffer_Sniff
30+
{
31+
/**
32+
* A list of tokenizers this sniff supports.
33+
*
34+
* @var array
35+
*/
36+
public $supportedTokenizers = array(
37+
'PHP',
38+
'JS',
39+
);
40+
41+
/**
42+
* Returns an array of tokens this test wants to listen for.
43+
*
44+
* @return array
45+
*/
46+
public function register()
47+
{
48+
return array(T_IF);
49+
}
50+
51+
/**
52+
* Processes this test, when one of its tokens is encountered.
53+
*
54+
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
55+
* @param int $stackPtr The position of the current token in
56+
* the stack passed in $tokens.
57+
*
58+
* @return void
59+
*/
60+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61+
{
62+
$tokens = $phpcsFile->getTokens();
63+
$current = $stackPtr;
64+
$previousLine = $tokens[$stackPtr]['line'] - 1;
65+
$prevLineTokens = array();
66+
67+
while ($current >= 0 && $tokens[$current]['line'] >= $previousLine) {
68+
if ($tokens[$current]['line'] == $previousLine
69+
&& $tokens[$current]['type'] !== 'T_WHITESPACE'
70+
&& $tokens[$current]['type'] !== 'T_COMMENT'
71+
) {
72+
$prevLineTokens[] = $tokens[$current]['type'];
73+
}
74+
$current--;
75+
}
76+
77+
if (isset($prevLineTokens[0])
78+
&& ($prevLineTokens[0] === 'T_OPEN_CURLY_BRACKET'
79+
|| $prevLineTokens[0] === 'T_COLON')
80+
) {
81+
return;
82+
}
83+
84+
if (count($prevLineTokens) > 0) {
85+
$phpcsFile->addError(
86+
'Missing blank line before if statement',
87+
$stackPtr
88+
);
89+
}
90+
91+
return;
92+
}
93+
}

Tests/Formatting/BlankLineBeforeReturnUnitTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function getErrorList()
4040
return array(
4141
23 => 1,
4242
);
43-
4443
}
4544

4645
/**
@@ -53,7 +52,7 @@ public function getErrorList()
5352
*/
5453
public function getWarningList()
5554
{
56-
return array();
57-
55+
return array(
56+
);
5857
}
59-
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
function validFunctionIfOne()
3+
{
4+
if (true) {
5+
}
6+
}
7+
8+
function validFunctionIfTwo()
9+
{
10+
11+
if (true) {
12+
}
13+
}
14+
15+
function validFunctionIfThree()
16+
{
17+
echo "";
18+
19+
if (true) {
20+
}
21+
}
22+
23+
function invalidFunctionIfOne()
24+
{
25+
echo "";
26+
if (true) {
27+
}
28+
}
29+
30+
switch ($condition) {
31+
case 'foo':
32+
if (true) {
33+
}
34+
35+
default:
36+
if (true) {
37+
}
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* This file is part of the Symfony2-coding-standard (phpcs standard)
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer-Symfony2
9+
* @author Symfony2-phpcs-authors <[email protected]>
10+
* @license http://spdx.org/licenses/MIT MIT License
11+
* @version GIT: master
12+
* @link https://github.com/opensky/Symfony2-coding-standard
13+
*/
14+
15+
/**
16+
* Unit test class for the BlankLineBeforeIf sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Tom Klingenberg <[email protected]>
24+
* @copyright 2012 Tom Klingenberg, some rights reserved.
25+
* @license http://spdx.org/licenses/MIT MIT License
26+
* @link https://github.com/opensky/Symfony2-coding-standard
27+
*/
28+
class Symfony2_Tests_Instaclick_BlankLineBeforeIfUnitTest extends AbstractSniffUnitTest
29+
{
30+
/**
31+
* Returns the lines where errors should occur.
32+
*
33+
* The key of the array should represent the line number and the value
34+
* should represent the number of errors that should occur on that line.
35+
*
36+
* @return array(int => int)
37+
*/
38+
public function getErrorList()
39+
{
40+
return array(
41+
26 => 1,
42+
);
43+
}
44+
45+
/**
46+
* Returns the lines where warnings should occur.
47+
*
48+
* The key of the array should represent the line number and the value
49+
* should represent the number of warnings that should occur on that line.
50+
*
51+
* @return array(int => int)
52+
*/
53+
public function getWarningList()
54+
{
55+
return array(
56+
);
57+
}
58+
}

Tests/ObjectCalisthenics/NoElseUnitTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class Symfony2_Tests_ObjectCalisthenics_NoElseUnitTest extends AbstractSniffUnit
3838
public function getErrorList()
3939
{
4040
return array(
41-
26 => 1,
41+
22 => 1,
4242
32 => 1,
43-
33 => 2,
43+
33 => 1,
4444
);
4545
}
4646

Tests/ObjectCalisthenics/OneIndentationLevelUnitTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public function getErrorList()
5757
public function getWarningList()
5858
{
5959
return array(
60-
10 => 1,
61-
39 => 1,
62-
57 => 1,
6360
);
6461
}
6562
}

Tests/ObjectCalisthenics/UseAccessorsUnitTest.inc

-10
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ class UseAccessorsSniffTest
4141
return $obj;
4242
}
4343

44-
/**
45-
* Invalid setter
46-
*
47-
* @param mixed $object
48-
*/
49-
public function setObject($object)
50-
{
51-
$object->enabled = true;
52-
}
53-
5444
/**
5545
* Invalid setter
5646
*/

Tests/ObjectCalisthenics/UseAccessorsUnitTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ class Symfony2_Tests_ObjectCalisthenics_UseAccessorsUnitTest extends AbstractSni
3838
public function getErrorList()
3939
{
4040
return array(
41-
7 => 2,
42-
8 => 1,
43-
37 => 1,
44-
49 => 1,
4541
57 => 1,
46-
67 => 1,
47-
77 => 2,
4842
);
4943
}
5044

@@ -59,7 +53,12 @@ public function getErrorList()
5953
public function getWarningList()
6054
{
6155
return array(
56+
7 => 1,
57+
8 => 1,
6258
9 => 1,
59+
37 => 1,
60+
47 => 1,
61+
67 => 2,
6362
);
6463
}
6564
}

0 commit comments

Comments
 (0)