Skip to content

Commit b167771

Browse files
committed
#94 : Add no space after control stmt test option
1 parent 6522c94 commit b167771

File tree

7 files changed

+221
-161
lines changed

7 files changed

+221
-161
lines changed

RELEASE_NOTES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
PHPCheckstyle Release Notes
22
===========================
33

4+
45
Version 0.14.9
56
--------------
67
* Issue #93 : Use Bootstrap4 for styling (thanks to HCrane)
7-
8+
* Issue #94 : Add no space after control stmt test option (thanks to CharlotteDunois)
89

910
Version 0.14.8
1011
--------------

Vagrantfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ VAGRANTFILE_API_VERSION = "2"
77

88
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
99

10-
config.vm.box = "debian/jessie64"
11-
config.vm.box_version = "8.6.1"
10+
config.vm.box = "geerlingguy/debian9"
1211

1312
#Dev config
1413
config.vm.define "phpcheckstyle_dev" do |phpcheckstyle_dev|

config/default.cfg.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@
188188
are followed by a space before the opening parenthesis. PEAR standard stipulates
189189
this to distinguish it from function calls. -->
190190
<test name="spaceAfterControlStmt" />
191+
192+
<!-- Tests that the control statements ("if", "else", "while", "for", etc.)
193+
are NOT followed by a space before the opening parenthesis. -->
194+
<!-- <test name="noSpaceAfterControlStmt" /> -->
191195

192196
<!-- Check that there is no space after a function name in a function call -->
193197
<test name="noSpaceAfterFunctionName" level="info"></test>
@@ -210,7 +214,7 @@
210214
<test name="noSpaceAfterToken" level="info" />
211215

212216
<!-- **************** -->
213-
<!-- Metrics -->
217+
<!-- Metrics -->
214218
<!-- **************** -->
215219

216220
<!-- Check that the lenght of the line doesn't pass the max value -->

src/PHPCheckstyle/PHPCheckstyle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,14 @@ private function _processControlStatement($token) {
18171817
$this->_writeError('spaceAfterControlStmt', $msg);
18181818
}
18191819
}
1820+
} else if ($this->_isActive('noSpaceAfterControlStmt')) {
1821+
// first token: if one whitespace, error
1822+
if ($this->tokenizer->checkNextToken(T_WHITESPACE)) {
1823+
if (($token->id !== T_ELSE) && ($token->id !== T_TRY) && ($token->id !== T_DO)) {
1824+
$msg = $this->_getMessage('NO_SPACE_AFTER_TOKEN', $csText);
1825+
$this->_writeError('noSpaceAfterControlStmt', $msg);
1826+
}
1827+
}
18201828
}
18211829

18221830
// for some control structures like "else" and "do",

test/IndentationTest.php

Lines changed: 181 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,182 @@
1-
<?php
2-
use PHPUnit\Framework\TestCase;
3-
4-
/**
5-
* Indentation tests.
6-
*/
7-
class IndentationTest extends TestCase {
8-
9-
/**
10-
* Test tabs indentation.
11-
*/
12-
public function testTabIndentation() {
13-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
14-
15-
$phpcheckstyle->processFiles(array(
16-
'./test/sample/bad_indentation.php'
17-
));
18-
19-
$errorCounts = $phpcheckstyle->getErrorCounts();
20-
21-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
22-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
23-
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
24-
$this->assertEquals(6, $errorCounts['warning'], 'We expect 6 warnings');
25-
}
26-
27-
/**
28-
* Test tabs indentation.
29-
*/
30-
public function testSpaceIndentation() {
31-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
32-
33-
// Change the configuration to check for spaces instead of tabs
34-
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
35-
36-
$phpcheckstyle->processFiles(array(
37-
'./test/sample/bad_indentation.php'
38-
));
39-
40-
$errorCounts = $phpcheckstyle->getErrorCounts();
41-
42-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
43-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
44-
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
45-
$this->assertEquals(11, $errorCounts['warning'], 'We expect 11 warnings');
46-
}
47-
48-
/**
49-
* Test tabs indentation.
50-
*/
51-
public function testSpaceIndentationArray() {
52-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
53-
54-
// Change the configuration to check for spaces instead of tabs
55-
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
56-
57-
$phpcheckstyle->processFiles(array(
58-
'./test/sample/bad_indentation_array.php'
59-
));
60-
61-
$errorCounts = $phpcheckstyle->getErrorCounts();
62-
63-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
64-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
65-
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
66-
$this->assertEquals(11, $errorCounts['warning'], 'We expect 11 warnings');
67-
}
68-
69-
/**
70-
* Test tabs indentation.
71-
*/
72-
public function testGoodSpaceIndentationArray() {
73-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
74-
75-
// Change the configuration to check for spaces instead of tabs
76-
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
77-
78-
$phpcheckstyle->processFiles(array(
79-
'./test/sample/good_indentation_array.php'
80-
));
81-
82-
$errorCounts = $phpcheckstyle->getErrorCounts();
83-
84-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
85-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
86-
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
87-
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
88-
}
89-
90-
/**
91-
* Test for indentation with new line indentation.
92-
*/
93-
public function testGoodIndentationNewLine() {
94-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
95-
96-
// Change the configuration to check for spaces instead of tabs
97-
$phpcheckstyle->getConfig()->setTestProperty('controlStructOpenCurly', 'position', 'nl');
98-
$phpcheckstyle->getConfig()->setTestProperty('funcDefinitionOpenCurly', 'position', 'nl');
99-
$phpcheckstyle->getConfig()->setTestProperty('controlStructElse', 'position', 'nl');
100-
101-
102-
$phpcheckstyle->processFiles(array(
103-
'./test/sample/good_indentation_newline.php'
104-
));
105-
106-
$errorCounts = $phpcheckstyle->getErrorCounts();
107-
108-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
109-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
110-
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
111-
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
112-
}
113-
114-
/**
115-
* Test for indentation with spaces.
116-
*/
117-
public function testGoodIndentationSpaces() {
118-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
119-
120-
// Change the configuration to check for spaces instead of tabs
121-
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
122-
123-
$phpcheckstyle->processFiles(array(
124-
'./test/sample/good_indentation_space.php'
125-
));
126-
127-
$errorCounts = $phpcheckstyle->getErrorCounts();
128-
129-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
130-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
131-
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
132-
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
133-
}
134-
135-
136-
/**
137-
* Test for for spaces missing or in excedent.
138-
*/
139-
public function testBadSpaces() {
140-
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
141-
142-
$phpcheckstyle->processFiles(array(
143-
'./test/sample/bad_spaces.php'
144-
));
145-
146-
$errorCounts = $phpcheckstyle->getErrorCounts();
147-
148-
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors of naming');
149-
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
150-
$this->assertEquals(2, $errorCounts['info'], 'We expect 2 info');
151-
$this->assertEquals(7, $errorCounts['warning'], 'We expect 7 warnings');
152-
}
153-
}
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
/**
5+
* Indentation tests.
6+
*/
7+
class IndentationTest extends TestCase {
8+
9+
/**
10+
* Test tabs indentation.
11+
*/
12+
public function testTabIndentation() {
13+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
14+
15+
$phpcheckstyle->processFiles(array(
16+
'./test/sample/bad_indentation.php'
17+
));
18+
19+
$errorCounts = $phpcheckstyle->getErrorCounts();
20+
21+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
22+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
23+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
24+
$this->assertEquals(6, $errorCounts['warning'], 'We expect 6 warnings');
25+
}
26+
27+
/**
28+
* Test tabs indentation.
29+
*/
30+
public function testSpaceIndentation() {
31+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
32+
33+
// Change the configuration to check for spaces instead of tabs
34+
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
35+
36+
$phpcheckstyle->processFiles(array(
37+
'./test/sample/bad_indentation.php'
38+
));
39+
40+
$errorCounts = $phpcheckstyle->getErrorCounts();
41+
42+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
43+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
44+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
45+
$this->assertEquals(11, $errorCounts['warning'], 'We expect 11 warnings');
46+
}
47+
48+
/**
49+
* Test tabs indentation.
50+
*/
51+
public function testSpaceIndentationArray() {
52+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
53+
54+
// Change the configuration to check for spaces instead of tabs
55+
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
56+
57+
$phpcheckstyle->processFiles(array(
58+
'./test/sample/bad_indentation_array.php'
59+
));
60+
61+
$errorCounts = $phpcheckstyle->getErrorCounts();
62+
63+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
64+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
65+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
66+
$this->assertEquals(11, $errorCounts['warning'], 'We expect 11 warnings');
67+
}
68+
69+
/**
70+
* Test tabs indentation.
71+
*/
72+
public function testGoodSpaceIndentationArray() {
73+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
74+
75+
// Change the configuration to check for spaces instead of tabs
76+
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
77+
78+
$phpcheckstyle->processFiles(array(
79+
'./test/sample/good_indentation_array.php'
80+
));
81+
82+
$errorCounts = $phpcheckstyle->getErrorCounts();
83+
84+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
85+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
86+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
87+
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
88+
}
89+
90+
/**
91+
* Test for indentation with new line indentation.
92+
*/
93+
public function testGoodIndentationNewLine() {
94+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
95+
96+
// Change the configuration to check for spaces instead of tabs
97+
$phpcheckstyle->getConfig()->setTestProperty('controlStructOpenCurly', 'position', 'nl');
98+
$phpcheckstyle->getConfig()->setTestProperty('funcDefinitionOpenCurly', 'position', 'nl');
99+
$phpcheckstyle->getConfig()->setTestProperty('controlStructElse', 'position', 'nl');
100+
101+
$phpcheckstyle->processFiles(array(
102+
'./test/sample/good_indentation_newline.php'
103+
));
104+
105+
$errorCounts = $phpcheckstyle->getErrorCounts();
106+
107+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
108+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
109+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
110+
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
111+
}
112+
113+
/**
114+
* Test for indentation with spaces.
115+
*/
116+
public function testGoodIndentationSpaces() {
117+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
118+
119+
// Change the configuration to check for spaces instead of tabs
120+
$phpcheckstyle->getConfig()->setTestProperty('indentation', 'type', 'spaces');
121+
122+
$phpcheckstyle->processFiles(array(
123+
'./test/sample/good_indentation_space.php'
124+
));
125+
126+
$errorCounts = $phpcheckstyle->getErrorCounts();
127+
128+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
129+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
130+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
131+
$this->assertEquals(0, $errorCounts['warning'], 'We expect 0 warnings');
132+
}
133+
134+
/**
135+
* Test for for spaces missing or in excedent.
136+
*/
137+
public function testBadSpaces() {
138+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
139+
140+
$phpcheckstyle->processFiles(array(
141+
'./test/sample/bad_spaces.php'
142+
));
143+
144+
$errorCounts = $phpcheckstyle->getErrorCounts();
145+
146+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors of naming');
147+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
148+
$this->assertEquals(2, $errorCounts['info'], 'We expect 2 info');
149+
$this->assertEquals(7, $errorCounts['warning'], 'We expect 7 warnings');
150+
}
151+
152+
/**
153+
* Test for space after control statement (no space after control statement).
154+
*/
155+
public function testBadSpaceAfterControl() {
156+
$phpcheckstyle = $GLOBALS['PHPCheckstyle'];
157+
158+
// Change the configuration to check for spaces instead of tabs
159+
$phpcheckstyle->getConfig()->setTestProperty('spaceaftercontrolstmt', 'type', 'spaces');
160+
161+
// Change the configuration to check for no spaces after control statement
162+
$config = $phpcheckstyle->getConfig();
163+
$refl = new \ReflectionProperty($config, 'config');
164+
$refl->setAccessible(true);
165+
$value = $refl->getValue($config);
166+
unset($value['spaceaftercontrolstmt']);
167+
$value['nospaceaftercontrolstmt'] = array();
168+
$refl->setValue($config, $value);
169+
170+
$phpcheckstyle->processFiles(array(
171+
'./test/sample/bad_space_after_control.php'
172+
));
173+
174+
$errorCounts = $phpcheckstyle->getErrorCounts();
175+
176+
$this->assertEquals(0, $errorCounts['error'], 'We expect 0 errors');
177+
$this->assertEquals(0, $errorCounts['ignore'], 'We expect 0 ignored checks');
178+
$this->assertEquals(0, $errorCounts['info'], 'We expect 0 info');
179+
$this->assertEquals(1, $errorCounts['warning'], 'We expect 1 warning');
180+
}
181+
}
154182
?>

0 commit comments

Comments
 (0)