Completed
Branch dev (588c07)
by
unknown
24:15 queued 17:27
created
php_codesniffer/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.2.inc 2 patches
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
 const CONSTANT2 = 2;
6 6
 
7 7
 if ($something) {
8
-    echo 'hi';
8
+	echo 'hi';
9 9
 }
10 10
 
11 11
 $var = myFunction();
@@ -16,9 +16,9 @@  discard block
 block discarded – undo
16 16
 
17 17
 $c = new class extends Something{
18 18
 
19
-    public function someMethod()
20
-    {
21
-        // ...
22
-    }
19
+	public function someMethod()
20
+	{
21
+		// ...
22
+	}
23 23
 
24 24
 };
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@
 block discarded – undo
14 14
 echo $object -> define();
15 15
 Foo::define();
16 16
 
17
-$c = new class extends Something{
17
+$c = new class extends Something {
18 18
 
19 19
     public function someMethod()
20 20
     {
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.9.inc 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -4,5 +4,5 @@
 block discarded – undo
4 4
 // phpcs:enable
5 5
 $defined = true;
6 6
 if (defined('MINSIZE') === false) {
7
-    $defined = false;
7
+	$defined = false;
8 8
 }
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.4.inc 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@
 block discarded – undo
1 1
 <html>
2 2
 <?php
3 3
 function printHead() {
4
-    echo '<head></head>';
4
+	echo '<head></head>';
5 5
 }
6 6
 
7 7
 printHead();
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.php 3 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
      *
26 26
      * @return array<int, int>
27 27
      */
28
-    public function getErrorList($testFile='')
28
+    public function getErrorList($testFile = '')
29 29
     {
30 30
         return [];
31 31
 
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
      *
43 43
      * @return array<int, int>
44 44
      */
45
-    public function getWarningList($testFile='')
45
+    public function getWarningList($testFile = '')
46 46
     {
47 47
         switch ($testFile) {
48 48
         case 'SideEffectsUnitTest.3.inc':
Please login to merge, or discard this patch.
Indentation   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -15,66 +15,66 @@
 block discarded – undo
15 15
 {
16 16
 
17 17
 
18
-    /**
19
-     * Set CLI values before the file is tested.
20
-     *
21
-     * @param string                  $testFile The name of the file being tested.
22
-     * @param \PHP_CodeSniffer\Config $config   The config data for the test run.
23
-     *
24
-     * @return void
25
-     */
26
-    public function setCliValues($testFile, $config)
27
-    {
28
-        if ($testFile === 'SideEffectsUnitTest.12.inc') {
29
-            $config->annotations = false;
30
-        }
18
+	/**
19
+	 * Set CLI values before the file is tested.
20
+	 *
21
+	 * @param string                  $testFile The name of the file being tested.
22
+	 * @param \PHP_CodeSniffer\Config $config   The config data for the test run.
23
+	 *
24
+	 * @return void
25
+	 */
26
+	public function setCliValues($testFile, $config)
27
+	{
28
+		if ($testFile === 'SideEffectsUnitTest.12.inc') {
29
+			$config->annotations = false;
30
+		}
31 31
 
32
-    }//end setCliValues()
32
+	}//end setCliValues()
33 33
 
34 34
 
35
-    /**
36
-     * Returns the lines where errors should occur.
37
-     *
38
-     * The key of the array should represent the line number and the value
39
-     * should represent the number of errors that should occur on that line.
40
-     *
41
-     * @param string $testFile The name of the file being tested.
42
-     *
43
-     * @return array<int, int>
44
-     */
45
-    public function getErrorList($testFile='')
46
-    {
47
-        return [];
35
+	/**
36
+	 * Returns the lines where errors should occur.
37
+	 *
38
+	 * The key of the array should represent the line number and the value
39
+	 * should represent the number of errors that should occur on that line.
40
+	 *
41
+	 * @param string $testFile The name of the file being tested.
42
+	 *
43
+	 * @return array<int, int>
44
+	 */
45
+	public function getErrorList($testFile='')
46
+	{
47
+		return [];
48 48
 
49
-    }//end getErrorList()
49
+	}//end getErrorList()
50 50
 
51 51
 
52
-    /**
53
-     * Returns the lines where warnings should occur.
54
-     *
55
-     * The key of the array should represent the line number and the value
56
-     * should represent the number of warnings that should occur on that line.
57
-     *
58
-     * @param string $testFile The name of the file being tested.
59
-     *
60
-     * @return array<int, int>
61
-     */
62
-    public function getWarningList($testFile='')
63
-    {
64
-        switch ($testFile) {
65
-        case 'SideEffectsUnitTest.3.inc':
66
-        case 'SideEffectsUnitTest.4.inc':
67
-        case 'SideEffectsUnitTest.5.inc':
68
-        case 'SideEffectsUnitTest.10.inc':
69
-        case 'SideEffectsUnitTest.12.inc':
70
-        case 'SideEffectsUnitTest.15.inc':
71
-        case 'SideEffectsUnitTest.16.inc':
72
-            return [1 => 1];
73
-        default:
74
-            return [];
75
-        }//end switch
52
+	/**
53
+	 * Returns the lines where warnings should occur.
54
+	 *
55
+	 * The key of the array should represent the line number and the value
56
+	 * should represent the number of warnings that should occur on that line.
57
+	 *
58
+	 * @param string $testFile The name of the file being tested.
59
+	 *
60
+	 * @return array<int, int>
61
+	 */
62
+	public function getWarningList($testFile='')
63
+	{
64
+		switch ($testFile) {
65
+		case 'SideEffectsUnitTest.3.inc':
66
+		case 'SideEffectsUnitTest.4.inc':
67
+		case 'SideEffectsUnitTest.5.inc':
68
+		case 'SideEffectsUnitTest.10.inc':
69
+		case 'SideEffectsUnitTest.12.inc':
70
+		case 'SideEffectsUnitTest.15.inc':
71
+		case 'SideEffectsUnitTest.16.inc':
72
+			return [1 => 1];
73
+		default:
74
+			return [];
75
+		}//end switch
76 76
 
77
-    }//end getWarningList()
77
+	}//end getWarningList()
78 78
 
79 79
 
80 80
 }//end class
Please login to merge, or discard this patch.
Switch Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -62,16 +62,16 @@
 block discarded – undo
62 62
     public function getWarningList($testFile='')
63 63
     {
64 64
         switch ($testFile) {
65
-        case 'SideEffectsUnitTest.3.inc':
66
-        case 'SideEffectsUnitTest.4.inc':
67
-        case 'SideEffectsUnitTest.5.inc':
68
-        case 'SideEffectsUnitTest.10.inc':
69
-        case 'SideEffectsUnitTest.12.inc':
70
-        case 'SideEffectsUnitTest.15.inc':
71
-        case 'SideEffectsUnitTest.16.inc':
72
-            return [1 => 1];
73
-        default:
74
-            return [];
65
+        	case 'SideEffectsUnitTest.3.inc':
66
+        	case 'SideEffectsUnitTest.4.inc':
67
+        	case 'SideEffectsUnitTest.5.inc':
68
+        	case 'SideEffectsUnitTest.10.inc':
69
+        	case 'SideEffectsUnitTest.12.inc':
70
+        	case 'SideEffectsUnitTest.15.inc':
71
+        	case 'SideEffectsUnitTest.16.inc':
72
+            	return [1 => 1];
73
+        	default:
74
+            	return [];
75 75
         }//end switch
76 76
 
77 77
     }//end getWarningList()
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.11.inc 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@
 block discarded – undo
4 4
 define("MAXSIZE", 100);
5 5
 $defined = true;
6 6
 if (defined('MINSIZE') === false) {
7
-    $defined = false;
7
+	$defined = false;
8 8
 }
9 9
 
10 10
 // phpcs:enable PEAR
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -33,28 +33,28 @@
 block discarded – undo
33 33
     echo 'hi';
34 34
 }
35 35
 
36
-if (!function_exists('YourFunction')) {
36
+if ( ! function_exists('YourFunction')) {
37 37
     // Define a function.
38 38
     function YourFunction() {
39 39
         echo 'hi';
40 40
     }
41 41
 }
42 42
 
43
-if (!class_exists('MyClass')) {
43
+if ( ! class_exists('MyClass')) {
44 44
     // Define a class.
45 45
     class MyClass
46 46
     {
47 47
         function SomeFunction() {}
48 48
     }
49
-} else if (!interface_exists('MyInterface')) {
49
+} else if ( ! interface_exists('MyInterface')) {
50 50
     interface MyInterface {}
51
-} elseif (!interface_exists('MyInterface')) {
51
+} elseif ( ! interface_exists('MyInterface')) {
52 52
     interface MyInterface {}
53 53
 } else {
54 54
     interface MyInterface {}
55 55
 }
56 56
 
57
-if (!interface_exists('MyInterface')) {
57
+if ( ! interface_exists('MyInterface')) {
58 58
     // Define an interface.
59 59
     interface MyInterface {}
60 60
 }
Please login to merge, or discard this patch.
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -10,62 +10,62 @@
 block discarded – undo
10 10
 declare(ticks=1);
11 11
 
12 12
 declare(ticks=1) {
13
-    echo $i;
13
+	echo $i;
14 14
 }
15 15
 
16 16
 declare(ticks=1) :
17
-    echo $i;
17
+	echo $i;
18 18
 enddeclare;
19 19
 
20 20
 define("MAXSIZE", 100);
21 21
 if (defined('MINSIZE') === false) {
22
-    define("MINSIZE", 10);
22
+	define("MINSIZE", 10);
23 23
 }
24 24
 
25 25
 // Class comment.
26 26
 final class Foo
27 27
 {
28
-    public $myvar = 'foo';
28
+	public $myvar = 'foo';
29 29
 
30
-    // Function comment.
31
-    function Bar() {
32
-        echo 'hi';
33
-    }
30
+	// Function comment.
31
+	function Bar() {
32
+		echo 'hi';
33
+	}
34 34
 }
35 35
 
36 36
 function MyFunction() {
37
-    echo 'hi';
37
+	echo 'hi';
38 38
 }
39 39
 
40 40
 if (!function_exists('YourFunction')) {
41
-    // Define a function.
42
-    function YourFunction() {
43
-        echo 'hi';
44
-    }
41
+	// Define a function.
42
+	function YourFunction() {
43
+		echo 'hi';
44
+	}
45 45
 }
46 46
 
47 47
 if (!class_exists('MyClass')) {
48
-    // Define a class.
49
-    class MyClass
50
-    {
51
-        function SomeFunction() {}
52
-    }
48
+	// Define a class.
49
+	class MyClass
50
+	{
51
+		function SomeFunction() {}
52
+	}
53 53
 } else if (!interface_exists('MyInterface')) {
54
-    interface MyInterface {}
54
+	interface MyInterface {}
55 55
 } elseif (!interface_exists('MyInterface')) {
56
-    interface MyInterface {}
56
+	interface MyInterface {}
57 57
 } else {
58
-    interface MyInterface {}
58
+	interface MyInterface {}
59 59
 }
60 60
 
61 61
 if (!interface_exists('MyInterface')) {
62
-    // Define an interface.
63
-    interface MyInterface {}
62
+	// Define an interface.
63
+	interface MyInterface {}
64 64
 }
65 65
 
66 66
 #[\Attribute]
67 67
 namespace {
68
-    class A {}
68
+	class A {}
69 69
 }
70 70
 
71 71
 defined('APP_BASE_URL') or define('APP_BASE_URL', '/');
Please login to merge, or discard this patch.
src/Standards/PSR1/Tests/Classes/ClassDeclarationUnitTest.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@
 block discarded – undo
25 25
      *
26 26
      * @return array<int, int>
27 27
      */
28
-    public function getErrorList($testFile='')
28
+    public function getErrorList($testFile = '')
29 29
     {
30 30
         if ($testFile === 'ClassDeclarationUnitTest.2.inc') {
31 31
             return [];
Please login to merge, or discard this patch.
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -15,43 +15,43 @@
 block discarded – undo
15 15
 {
16 16
 
17 17
 
18
-    /**
19
-     * Returns the lines where errors should occur.
20
-     *
21
-     * The key of the array should represent the line number and the value
22
-     * should represent the number of errors that should occur on that line.
23
-     *
24
-     * @param string $testFile The name of the file being tested.
25
-     *
26
-     * @return array<int, int>
27
-     */
28
-    public function getErrorList($testFile='')
29
-    {
30
-        if ($testFile === 'ClassDeclarationUnitTest.2.inc') {
31
-            return [];
32
-        }
33
-
34
-        return [
35
-            2 => 1,
36
-            3 => 2,
37
-        ];
38
-
39
-    }//end getErrorList()
40
-
41
-
42
-    /**
43
-     * Returns the lines where warnings should occur.
44
-     *
45
-     * The key of the array should represent the line number and the value
46
-     * should represent the number of warnings that should occur on that line.
47
-     *
48
-     * @return array<int, int>
49
-     */
50
-    public function getWarningList()
51
-    {
52
-        return [];
53
-
54
-    }//end getWarningList()
18
+	/**
19
+	 * Returns the lines where errors should occur.
20
+	 *
21
+	 * The key of the array should represent the line number and the value
22
+	 * should represent the number of errors that should occur on that line.
23
+	 *
24
+	 * @param string $testFile The name of the file being tested.
25
+	 *
26
+	 * @return array<int, int>
27
+	 */
28
+	public function getErrorList($testFile='')
29
+	{
30
+		if ($testFile === 'ClassDeclarationUnitTest.2.inc') {
31
+			return [];
32
+		}
33
+
34
+		return [
35
+			2 => 1,
36
+			3 => 2,
37
+		];
38
+
39
+	}//end getErrorList()
40
+
41
+
42
+	/**
43
+	 * Returns the lines where warnings should occur.
44
+	 *
45
+	 * The key of the array should represent the line number and the value
46
+	 * should represent the number of warnings that should occur on that line.
47
+	 *
48
+	 * @return array<int, int>
49
+	 */
50
+	public function getWarningList()
51
+	{
52
+		return [];
53
+
54
+	}//end getWarningList()
55 55
 
56 56
 
57 57
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -103,7 +103,7 @@
 block discarded – undo
103 103
             ) {
104 104
                 do {
105 105
                     $i = $phpcsFile->findNext(T_PHPCS_ENABLE, ($i + 1));
106
-                } while ($i !== false
106
+                }while ($i !== false
107 107
                     && empty($tokens[$i]['sniffCodes']) === false
108 108
                     && isset($tokens[$i]['sniffCodes']['PSR1']) === false
109 109
                     && isset($tokens[$i]['sniffCodes']['PSR1.Files']) === false
Please login to merge, or discard this patch.
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -17,282 +17,282 @@
 block discarded – undo
17 17
 {
18 18
 
19 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 [T_OPEN_TAG];
28
-
29
-    }//end register()
30
-
31
-
32
-    /**
33
-     * Processes this sniff, when one of its tokens is encountered.
34
-     *
35
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36
-     * @param int                         $stackPtr  The position of the current token in
37
-     *                                               the token stack.
38
-     *
39
-     * @return void
40
-     */
41
-    public function process(File $phpcsFile, $stackPtr)
42
-    {
43
-        $tokens = $phpcsFile->getTokens();
44
-        $result = $this->searchForConflict($phpcsFile, 0, ($phpcsFile->numTokens - 1), $tokens);
45
-
46
-        if ($result['symbol'] !== null && $result['effect'] !== null) {
47
-            $error = 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line %s and the first side effect is on line %s.';
48
-            $data  = [
49
-                $tokens[$result['symbol']]['line'],
50
-                $tokens[$result['effect']]['line'],
51
-            ];
52
-            $phpcsFile->addWarning($error, 0, 'FoundWithSymbols', $data);
53
-            $phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'yes');
54
-        } else {
55
-            $phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'no');
56
-        }
57
-
58
-        // Ignore the rest of the file.
59
-        return ($phpcsFile->numTokens + 1);
60
-
61
-    }//end process()
62
-
63
-
64
-    /**
65
-     * Searches for symbol declarations and side effects.
66
-     *
67
-     * Returns the positions of both the first symbol declared and the first
68
-     * side effect in the file. A NULL value for either indicates nothing was
69
-     * found.
70
-     *
71
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
72
-     * @param int                         $start     The token to start searching from.
73
-     * @param int                         $end       The token to search to.
74
-     * @param array                       $tokens    The stack of tokens that make up
75
-     *                                               the file.
76
-     *
77
-     * @return array
78
-     */
79
-    private function searchForConflict($phpcsFile, $start, $end, $tokens)
80
-    {
81
-        $symbols = [
82
-            T_CLASS     => T_CLASS,
83
-            T_INTERFACE => T_INTERFACE,
84
-            T_TRAIT     => T_TRAIT,
85
-            T_FUNCTION  => T_FUNCTION,
86
-        ];
87
-
88
-        $conditions = [
89
-            T_IF     => T_IF,
90
-            T_ELSE   => T_ELSE,
91
-            T_ELSEIF => T_ELSEIF,
92
-        ];
93
-
94
-        $checkAnnotations = $phpcsFile->config->annotations;
95
-
96
-        $firstSymbol = null;
97
-        $firstEffect = null;
98
-        for ($i = $start; $i <= $end; $i++) {
99
-            // Respect phpcs:disable comments.
100
-            if ($checkAnnotations === true
101
-                && $tokens[$i]['code'] === T_PHPCS_DISABLE
102
-                && (empty($tokens[$i]['sniffCodes']) === true
103
-                || isset($tokens[$i]['sniffCodes']['PSR1']) === true
104
-                || isset($tokens[$i]['sniffCodes']['PSR1.Files']) === true
105
-                || isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true)
106
-            ) {
107
-                do {
108
-                    $i = $phpcsFile->findNext(T_PHPCS_ENABLE, ($i + 1));
109
-                } while ($i !== false
110
-                    && empty($tokens[$i]['sniffCodes']) === false
111
-                    && isset($tokens[$i]['sniffCodes']['PSR1']) === false
112
-                    && isset($tokens[$i]['sniffCodes']['PSR1.Files']) === false
113
-                    && isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false);
114
-
115
-                if ($i === false) {
116
-                    // The entire rest of the file is disabled,
117
-                    // so return what we have so far.
118
-                    break;
119
-                }
120
-
121
-                continue;
122
-            }
123
-
124
-            // Ignore whitespace and comments.
125
-            if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
126
-                continue;
127
-            }
128
-
129
-            // Ignore PHP tags.
130
-            if ($tokens[$i]['code'] === T_OPEN_TAG
131
-                || $tokens[$i]['code'] === T_CLOSE_TAG
132
-            ) {
133
-                continue;
134
-            }
135
-
136
-            // Ignore shebang.
137
-            if (substr($tokens[$i]['content'], 0, 2) === '#!') {
138
-                continue;
139
-            }
140
-
141
-            // Ignore logical operators.
142
-            if (isset(Tokens::$booleanOperators[$tokens[$i]['code']]) === true) {
143
-                continue;
144
-            }
145
-
146
-            // Ignore entire namespace, declare, const and use statements.
147
-            if ($tokens[$i]['code'] === T_NAMESPACE
148
-                || $tokens[$i]['code'] === T_USE
149
-                || $tokens[$i]['code'] === T_DECLARE
150
-                || $tokens[$i]['code'] === T_CONST
151
-            ) {
152
-                if (isset($tokens[$i]['scope_opener']) === true) {
153
-                    $i = $tokens[$i]['scope_closer'];
154
-                    if ($tokens[$i]['code'] === T_ENDDECLARE) {
155
-                        $semicolon = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
156
-                        if ($semicolon !== false && $tokens[$semicolon]['code'] === T_SEMICOLON) {
157
-                            $i = $semicolon;
158
-                        }
159
-                    }
160
-                } else {
161
-                    $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
162
-                    if ($semicolon !== false) {
163
-                        $i = $semicolon;
164
-                    }
165
-                }
166
-
167
-                continue;
168
-            }
169
-
170
-            // Ignore function/class prefixes.
171
-            if (isset(Tokens::$methodPrefixes[$tokens[$i]['code']]) === true) {
172
-                continue;
173
-            }
174
-
175
-            // Ignore anon classes.
176
-            if ($tokens[$i]['code'] === T_ANON_CLASS) {
177
-                $i = $tokens[$i]['scope_closer'];
178
-                continue;
179
-            }
180
-
181
-            // Ignore attributes.
182
-            if ($tokens[$i]['code'] === T_ATTRIBUTE
183
-                && isset($tokens[$i]['attribute_closer']) === true
184
-            ) {
185
-                $i = $tokens[$i]['attribute_closer'];
186
-                continue;
187
-            }
188
-
189
-            // Detect and skip over symbols.
190
-            if (isset($symbols[$tokens[$i]['code']]) === true
191
-                && isset($tokens[$i]['scope_closer']) === true
192
-            ) {
193
-                if ($firstSymbol === null) {
194
-                    $firstSymbol = $i;
195
-                }
196
-
197
-                $i = $tokens[$i]['scope_closer'];
198
-                continue;
199
-            } else if ($tokens[$i]['code'] === T_STRING
200
-                && strtolower($tokens[$i]['content']) === 'define'
201
-            ) {
202
-                $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
203
-                if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
204
-                    && $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
205
-                    && $tokens[$prev]['code'] !== T_DOUBLE_COLON
206
-                    && $tokens[$prev]['code'] !== T_FUNCTION
207
-                ) {
208
-                    if ($firstSymbol === null) {
209
-                        $firstSymbol = $i;
210
-                    }
211
-
212
-                    $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
213
-                    if ($semicolon !== false) {
214
-                        $i = $semicolon;
215
-                    }
216
-
217
-                    continue;
218
-                }
219
-            }//end if
220
-
221
-            // Special case for defined() as it can be used to see
222
-            // if a constant (a symbol) should be defined or not and
223
-            // doesn't need to use a full conditional block.
224
-            if ($tokens[$i]['code'] === T_STRING
225
-                && strtolower($tokens[$i]['content']) === 'defined'
226
-            ) {
227
-                $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
228
-                if ($openBracket !== false
229
-                    && $tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS
230
-                    && isset($tokens[$openBracket]['parenthesis_closer']) === true
231
-                ) {
232
-                    $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
233
-                    if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
234
-                        && $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
235
-                        && $tokens[$prev]['code'] !== T_DOUBLE_COLON
236
-                        && $tokens[$prev]['code'] !== T_FUNCTION
237
-                    ) {
238
-                        $i = $tokens[$openBracket]['parenthesis_closer'];
239
-                        continue;
240
-                    }
241
-                }
242
-            }//end if
243
-
244
-            // Conditional statements are allowed in symbol files as long as the
245
-            // contents is only a symbol definition. So don't count these as effects
246
-            // in this case.
247
-            if (isset($conditions[$tokens[$i]['code']]) === true) {
248
-                if (isset($tokens[$i]['scope_opener']) === false) {
249
-                    // Probably an "else if", so just ignore.
250
-                    continue;
251
-                }
252
-
253
-                $result = $this->searchForConflict(
254
-                    $phpcsFile,
255
-                    ($tokens[$i]['scope_opener'] + 1),
256
-                    ($tokens[$i]['scope_closer'] - 1),
257
-                    $tokens
258
-                );
259
-
260
-                if ($result['symbol'] !== null) {
261
-                    if ($firstSymbol === null) {
262
-                        $firstSymbol = $result['symbol'];
263
-                    }
264
-
265
-                    if ($result['effect'] !== null) {
266
-                        // Found a conflict.
267
-                        $firstEffect = $result['effect'];
268
-                        break;
269
-                    }
270
-                }
271
-
272
-                if ($firstEffect === null) {
273
-                    $firstEffect = $result['effect'];
274
-                }
275
-
276
-                $i = $tokens[$i]['scope_closer'];
277
-                continue;
278
-            }//end if
279
-
280
-            if ($firstEffect === null) {
281
-                $firstEffect = $i;
282
-            }
283
-
284
-            if ($firstSymbol !== null) {
285
-                // We have a conflict we have to report, so no point continuing.
286
-                break;
287
-            }
288
-        }//end for
289
-
290
-        return [
291
-            'symbol' => $firstSymbol,
292
-            'effect' => $firstEffect,
293
-        ];
294
-
295
-    }//end searchForConflict()
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 [T_OPEN_TAG];
28
+
29
+	}//end register()
30
+
31
+
32
+	/**
33
+	 * Processes this sniff, when one of its tokens is encountered.
34
+	 *
35
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36
+	 * @param int                         $stackPtr  The position of the current token in
37
+	 *                                               the token stack.
38
+	 *
39
+	 * @return void
40
+	 */
41
+	public function process(File $phpcsFile, $stackPtr)
42
+	{
43
+		$tokens = $phpcsFile->getTokens();
44
+		$result = $this->searchForConflict($phpcsFile, 0, ($phpcsFile->numTokens - 1), $tokens);
45
+
46
+		if ($result['symbol'] !== null && $result['effect'] !== null) {
47
+			$error = 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line %s and the first side effect is on line %s.';
48
+			$data  = [
49
+				$tokens[$result['symbol']]['line'],
50
+				$tokens[$result['effect']]['line'],
51
+			];
52
+			$phpcsFile->addWarning($error, 0, 'FoundWithSymbols', $data);
53
+			$phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'yes');
54
+		} else {
55
+			$phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'no');
56
+		}
57
+
58
+		// Ignore the rest of the file.
59
+		return ($phpcsFile->numTokens + 1);
60
+
61
+	}//end process()
62
+
63
+
64
+	/**
65
+	 * Searches for symbol declarations and side effects.
66
+	 *
67
+	 * Returns the positions of both the first symbol declared and the first
68
+	 * side effect in the file. A NULL value for either indicates nothing was
69
+	 * found.
70
+	 *
71
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
72
+	 * @param int                         $start     The token to start searching from.
73
+	 * @param int                         $end       The token to search to.
74
+	 * @param array                       $tokens    The stack of tokens that make up
75
+	 *                                               the file.
76
+	 *
77
+	 * @return array
78
+	 */
79
+	private function searchForConflict($phpcsFile, $start, $end, $tokens)
80
+	{
81
+		$symbols = [
82
+			T_CLASS     => T_CLASS,
83
+			T_INTERFACE => T_INTERFACE,
84
+			T_TRAIT     => T_TRAIT,
85
+			T_FUNCTION  => T_FUNCTION,
86
+		];
87
+
88
+		$conditions = [
89
+			T_IF     => T_IF,
90
+			T_ELSE   => T_ELSE,
91
+			T_ELSEIF => T_ELSEIF,
92
+		];
93
+
94
+		$checkAnnotations = $phpcsFile->config->annotations;
95
+
96
+		$firstSymbol = null;
97
+		$firstEffect = null;
98
+		for ($i = $start; $i <= $end; $i++) {
99
+			// Respect phpcs:disable comments.
100
+			if ($checkAnnotations === true
101
+				&& $tokens[$i]['code'] === T_PHPCS_DISABLE
102
+				&& (empty($tokens[$i]['sniffCodes']) === true
103
+				|| isset($tokens[$i]['sniffCodes']['PSR1']) === true
104
+				|| isset($tokens[$i]['sniffCodes']['PSR1.Files']) === true
105
+				|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true)
106
+			) {
107
+				do {
108
+					$i = $phpcsFile->findNext(T_PHPCS_ENABLE, ($i + 1));
109
+				} while ($i !== false
110
+					&& empty($tokens[$i]['sniffCodes']) === false
111
+					&& isset($tokens[$i]['sniffCodes']['PSR1']) === false
112
+					&& isset($tokens[$i]['sniffCodes']['PSR1.Files']) === false
113
+					&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false);
114
+
115
+				if ($i === false) {
116
+					// The entire rest of the file is disabled,
117
+					// so return what we have so far.
118
+					break;
119
+				}
120
+
121
+				continue;
122
+			}
123
+
124
+			// Ignore whitespace and comments.
125
+			if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
126
+				continue;
127
+			}
128
+
129
+			// Ignore PHP tags.
130
+			if ($tokens[$i]['code'] === T_OPEN_TAG
131
+				|| $tokens[$i]['code'] === T_CLOSE_TAG
132
+			) {
133
+				continue;
134
+			}
135
+
136
+			// Ignore shebang.
137
+			if (substr($tokens[$i]['content'], 0, 2) === '#!') {
138
+				continue;
139
+			}
140
+
141
+			// Ignore logical operators.
142
+			if (isset(Tokens::$booleanOperators[$tokens[$i]['code']]) === true) {
143
+				continue;
144
+			}
145
+
146
+			// Ignore entire namespace, declare, const and use statements.
147
+			if ($tokens[$i]['code'] === T_NAMESPACE
148
+				|| $tokens[$i]['code'] === T_USE
149
+				|| $tokens[$i]['code'] === T_DECLARE
150
+				|| $tokens[$i]['code'] === T_CONST
151
+			) {
152
+				if (isset($tokens[$i]['scope_opener']) === true) {
153
+					$i = $tokens[$i]['scope_closer'];
154
+					if ($tokens[$i]['code'] === T_ENDDECLARE) {
155
+						$semicolon = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
156
+						if ($semicolon !== false && $tokens[$semicolon]['code'] === T_SEMICOLON) {
157
+							$i = $semicolon;
158
+						}
159
+					}
160
+				} else {
161
+					$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
162
+					if ($semicolon !== false) {
163
+						$i = $semicolon;
164
+					}
165
+				}
166
+
167
+				continue;
168
+			}
169
+
170
+			// Ignore function/class prefixes.
171
+			if (isset(Tokens::$methodPrefixes[$tokens[$i]['code']]) === true) {
172
+				continue;
173
+			}
174
+
175
+			// Ignore anon classes.
176
+			if ($tokens[$i]['code'] === T_ANON_CLASS) {
177
+				$i = $tokens[$i]['scope_closer'];
178
+				continue;
179
+			}
180
+
181
+			// Ignore attributes.
182
+			if ($tokens[$i]['code'] === T_ATTRIBUTE
183
+				&& isset($tokens[$i]['attribute_closer']) === true
184
+			) {
185
+				$i = $tokens[$i]['attribute_closer'];
186
+				continue;
187
+			}
188
+
189
+			// Detect and skip over symbols.
190
+			if (isset($symbols[$tokens[$i]['code']]) === true
191
+				&& isset($tokens[$i]['scope_closer']) === true
192
+			) {
193
+				if ($firstSymbol === null) {
194
+					$firstSymbol = $i;
195
+				}
196
+
197
+				$i = $tokens[$i]['scope_closer'];
198
+				continue;
199
+			} else if ($tokens[$i]['code'] === T_STRING
200
+				&& strtolower($tokens[$i]['content']) === 'define'
201
+			) {
202
+				$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
203
+				if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
204
+					&& $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
205
+					&& $tokens[$prev]['code'] !== T_DOUBLE_COLON
206
+					&& $tokens[$prev]['code'] !== T_FUNCTION
207
+				) {
208
+					if ($firstSymbol === null) {
209
+						$firstSymbol = $i;
210
+					}
211
+
212
+					$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
213
+					if ($semicolon !== false) {
214
+						$i = $semicolon;
215
+					}
216
+
217
+					continue;
218
+				}
219
+			}//end if
220
+
221
+			// Special case for defined() as it can be used to see
222
+			// if a constant (a symbol) should be defined or not and
223
+			// doesn't need to use a full conditional block.
224
+			if ($tokens[$i]['code'] === T_STRING
225
+				&& strtolower($tokens[$i]['content']) === 'defined'
226
+			) {
227
+				$openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
228
+				if ($openBracket !== false
229
+					&& $tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS
230
+					&& isset($tokens[$openBracket]['parenthesis_closer']) === true
231
+				) {
232
+					$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
233
+					if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
234
+						&& $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
235
+						&& $tokens[$prev]['code'] !== T_DOUBLE_COLON
236
+						&& $tokens[$prev]['code'] !== T_FUNCTION
237
+					) {
238
+						$i = $tokens[$openBracket]['parenthesis_closer'];
239
+						continue;
240
+					}
241
+				}
242
+			}//end if
243
+
244
+			// Conditional statements are allowed in symbol files as long as the
245
+			// contents is only a symbol definition. So don't count these as effects
246
+			// in this case.
247
+			if (isset($conditions[$tokens[$i]['code']]) === true) {
248
+				if (isset($tokens[$i]['scope_opener']) === false) {
249
+					// Probably an "else if", so just ignore.
250
+					continue;
251
+				}
252
+
253
+				$result = $this->searchForConflict(
254
+					$phpcsFile,
255
+					($tokens[$i]['scope_opener'] + 1),
256
+					($tokens[$i]['scope_closer'] - 1),
257
+					$tokens
258
+				);
259
+
260
+				if ($result['symbol'] !== null) {
261
+					if ($firstSymbol === null) {
262
+						$firstSymbol = $result['symbol'];
263
+					}
264
+
265
+					if ($result['effect'] !== null) {
266
+						// Found a conflict.
267
+						$firstEffect = $result['effect'];
268
+						break;
269
+					}
270
+				}
271
+
272
+				if ($firstEffect === null) {
273
+					$firstEffect = $result['effect'];
274
+				}
275
+
276
+				$i = $tokens[$i]['scope_closer'];
277
+				continue;
278
+			}//end if
279
+
280
+			if ($firstEffect === null) {
281
+				$firstEffect = $i;
282
+			}
283
+
284
+			if ($firstSymbol !== null) {
285
+				// We have a conflict we have to report, so no point continuing.
286
+				break;
287
+			}
288
+		}//end for
289
+
290
+		return [
291
+			'symbol' => $firstSymbol,
292
+			'effect' => $firstEffect,
293
+		];
294
+
295
+	}//end searchForConflict()
296 296
 
297 297
 
298 298
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/PSR1/Sniffs/Classes/ClassDeclarationSniff.php 1 patch
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -16,59 +16,59 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * Returns an array of tokens this test wants to listen for.
21
-     *
22
-     * @return array
23
-     */
24
-    public function register()
25
-    {
26
-        return [
27
-            T_CLASS,
28
-            T_INTERFACE,
29
-            T_TRAIT,
30
-        ];
19
+	/**
20
+	 * Returns an array of tokens this test wants to listen for.
21
+	 *
22
+	 * @return array
23
+	 */
24
+	public function register()
25
+	{
26
+		return [
27
+			T_CLASS,
28
+			T_INTERFACE,
29
+			T_TRAIT,
30
+		];
31 31
 
32
-    }//end register()
32
+	}//end register()
33 33
 
34 34
 
35
-    /**
36
-     * Processes this test, when one of its tokens is encountered.
37
-     *
38
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
39
-     * @param integer                     $stackPtr  The position of the current token in
40
-     *                                               the token stack.
41
-     *
42
-     * @return void
43
-     */
44
-    public function process(File $phpcsFile, $stackPtr)
45
-    {
46
-        $tokens = $phpcsFile->getTokens();
47
-        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
48
-            return;
49
-        }
35
+	/**
36
+	 * Processes this test, when one of its tokens is encountered.
37
+	 *
38
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
39
+	 * @param integer                     $stackPtr  The position of the current token in
40
+	 *                                               the token stack.
41
+	 *
42
+	 * @return void
43
+	 */
44
+	public function process(File $phpcsFile, $stackPtr)
45
+	{
46
+		$tokens = $phpcsFile->getTokens();
47
+		if (isset($tokens[$stackPtr]['scope_closer']) === false) {
48
+			return;
49
+		}
50 50
 
51
-        $errorData = [strtolower($tokens[$stackPtr]['content'])];
51
+		$errorData = [strtolower($tokens[$stackPtr]['content'])];
52 52
 
53
-        $nextClass = $phpcsFile->findNext([T_CLASS, T_INTERFACE, T_TRAIT], ($tokens[$stackPtr]['scope_closer'] + 1));
54
-        if ($nextClass !== false) {
55
-            $error = 'Each %s must be in a file by itself';
56
-            $phpcsFile->addError($error, $nextClass, 'MultipleClasses', $errorData);
57
-            $phpcsFile->recordMetric($stackPtr, 'One class per file', 'no');
58
-        } else {
59
-            $phpcsFile->recordMetric($stackPtr, 'One class per file', 'yes');
60
-        }
53
+		$nextClass = $phpcsFile->findNext([T_CLASS, T_INTERFACE, T_TRAIT], ($tokens[$stackPtr]['scope_closer'] + 1));
54
+		if ($nextClass !== false) {
55
+			$error = 'Each %s must be in a file by itself';
56
+			$phpcsFile->addError($error, $nextClass, 'MultipleClasses', $errorData);
57
+			$phpcsFile->recordMetric($stackPtr, 'One class per file', 'no');
58
+		} else {
59
+			$phpcsFile->recordMetric($stackPtr, 'One class per file', 'yes');
60
+		}
61 61
 
62
-        $namespace = $phpcsFile->findNext([T_NAMESPACE, T_CLASS, T_INTERFACE, T_TRAIT], 0);
63
-        if ($tokens[$namespace]['code'] !== T_NAMESPACE) {
64
-            $error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
65
-            $phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $errorData);
66
-            $phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'no');
67
-        } else {
68
-            $phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'yes');
69
-        }
62
+		$namespace = $phpcsFile->findNext([T_NAMESPACE, T_CLASS, T_INTERFACE, T_TRAIT], 0);
63
+		if ($tokens[$namespace]['code'] !== T_NAMESPACE) {
64
+			$error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
65
+			$phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $errorData);
66
+			$phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'no');
67
+		} else {
68
+			$phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'yes');
69
+		}
70 70
 
71
-    }//end process()
71
+	}//end process()
72 72
 
73 73
 
74 74
 }//end class
Please login to merge, or discard this patch.