Skip to content

Commit b9926e5

Browse files
committed
Switch to PHPCS coding standard
- add travis testing - change some sniffs to comply to PHPCS standard - update README.md - split out LICENSE - closes GH-8
1 parent a60d541 commit b9926e5

10 files changed

+302
-164
lines changed

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sudo: false
2+
language: php
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
8+
before_script:
9+
- composer install --dev
10+
- mkdir -p vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Symfony
11+
- cp -R Sniffs/ vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Symfony/Sniffs/
12+
- cp -R Tests/ vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Symfony/Tests/
13+
14+
script:
15+
- (cd vendor/squizlabs/php_codesniffer ; phpunit)
16+
- ./vendor/squizlabs/php_codesniffer/scripts/phpcs ./vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Symfony/Sniffs --standard=PHPCS --report=summary -np

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Xaver Loppenstedt
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# MO4 CodeSniffer ruleset
1+
# MO4 CodeSniffer ruleset <a href="https://travis-ci.org/Mayflower/mo4-coding-standard/"><img src="https://secure.travis-ci.org/Mayflower/mo4-coding-standard.png?branch=master"
22

33
Provides a CodeSniffer ruleset
44

@@ -51,11 +51,11 @@ The MO4 Coding Standard is an extension of the [Symfony Coding Standard](http://
5151

5252
## Contributing
5353

54-
If you contribute code to these sniffs, please make sure it conforms to the PEAR coding standard and that the unit tests still pass.
54+
If you contribute code to these sniffs, please make sure it conforms to the PHPCS coding standard and that the unit tests still pass.
5555

5656
To check the coding standard, run from the Symfony-coding-standard source root:
5757

58-
phpcs --ignore=Tests --standard=PEAR . -n
58+
phpcs --ignore=Tests --standard=PHPCS . -n
5959

6060
The unit-tests are run from within the PHP_CodeSniffer directory
6161

@@ -69,23 +69,4 @@ The unit-tests are run from within the PHP_CodeSniffer directory
6969

7070
## Licence
7171

72-
Copyright (c) 2013 Xaver Loppenstedt
73-
74-
Permission is hereby granted, free of charge, to any person obtaining a copy
75-
of this software and associated documentation files (the "Software"), to deal
76-
in the Software without restriction, including without limitation the rights
77-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78-
copies of the Software, and to permit persons to whom the Software is furnished
79-
to do so, subject to the following conditions:
80-
81-
The above copyright notice and this permission notice shall be included in all
82-
copies or substantial portions of the Software.
83-
84-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
87-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
88-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
89-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
90-
THE SOFTWARE.
91-
72+
This project is licensed under the MIT license.

Sniffs/Commenting/PropertyCommentSniff.php

+32-31
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ class MO4_Sniffs_Commenting_PropertyCommentSniff
3535
*/
3636
public $supportedTokenizers = array('PHP');
3737

38+
3839
/**
3940
* Construct PropertyCommentSniff
4041
*/
4142
function __construct()
4243
{
43-
$scopes = array(
44-
T_CLASS,
45-
);
44+
$scopes = array(T_CLASS);
4645

47-
$listen = array(
48-
T_VARIABLE,
49-
);
46+
$listen = array(T_VARIABLE);
5047

5148
parent::__construct($scopes, $listen, true);
52-
}
49+
50+
}//end __construct()
51+
5352

5453
/**
5554
* Processes a token that is found within the scope that this test is
@@ -70,22 +69,22 @@ protected function processTokenWithinScope(
7069
$currScope
7170
) {
7271
$find = array(
73-
T_COMMENT,
74-
T_DOC_COMMENT_CLOSE_TAG,
75-
T_CLASS,
76-
T_CONST,
77-
T_FUNCTION,
78-
T_VARIABLE,
79-
T_OPEN_TAG,
80-
);
72+
T_COMMENT,
73+
T_DOC_COMMENT_CLOSE_TAG,
74+
T_CLASS,
75+
T_CONST,
76+
T_FUNCTION,
77+
T_VARIABLE,
78+
T_OPEN_TAG,
79+
);
8180
$tokens = $phpcsFile->getTokens();
8281

8382
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1));
8483
if ($commentEnd === false) {
8584
return;
8685
}
8786

88-
$conditions = $tokens[$commentEnd]['conditions'];
87+
$conditions = $tokens[$commentEnd]['conditions'];
8988
$lastCondition = array_pop($conditions);
9089
if ($lastCondition !== T_CLASS) {
9190
return;
@@ -97,20 +96,20 @@ protected function processTokenWithinScope(
9796

9897
$isCommentOneLiner = $tokens[$commentStart]['line'] === $tokens[$commentEnd]['line'];
9998

100-
$length = $commentEnd - $commentStart + 1;
99+
$length = ($commentEnd - $commentStart + 1);
101100
$tokensAsString = $phpcsFile->getTokensAsString(
102101
$commentStart,
103102
$length
104103
);
105104

106-
$varCount = count(preg_split('/\s+@var\s+/', $tokensAsString)) - 1;
105+
$varCount = (count(preg_split('/\s+@var\s+/', $tokensAsString)) - 1);
107106
if ($varCount === 0) {
108107
$phpcsFile->addError(
109108
'property doc comment must have one @var annotation',
110109
$commentStart,
111110
'NoVarDefined'
112111
);
113-
} elseif ($varCount > 1) {
112+
} else if ($varCount > 1) {
114113
$phpcsFile->addError(
115114
'property doc comment must no multiple @var annotations',
116115
$commentStart,
@@ -119,41 +118,43 @@ protected function processTokenWithinScope(
119118
}
120119

121120
if ($varCount === 1) {
122-
if ($isCommentOneLiner) {
121+
if ($isCommentOneLiner === true) {
123122
$fix = $phpcsFile->addFixableError(
124123
'property doc comment must be multi line',
125124
$commentEnd,
126125
'NotMultiLineDocBlock'
127126
);
128127

129-
if ($fix) {
128+
if ($fix === true) {
130129
$phpcsFile->fixer->beginChangeset();
131130
$phpcsFile->fixer->addContent($commentStart, "\n *");
132131
$phpcsFile->fixer->replaceToken(
133-
$commentEnd - 1,
134-
rtrim($tokens[$commentEnd - 1]['content'])
132+
($commentEnd - 1),
133+
rtrim($tokens[($commentEnd - 1)]['content'])
135134
);
136135
$phpcsFile->fixer->addContentBefore($commentEnd, "\n ");
137136
$phpcsFile->fixer->endChangeset();
138137
}
139138
}
140139
} else {
141-
if ($isCommentOneLiner) {
140+
if ($isCommentOneLiner === true) {
142141
$phpcsFile->addError(
143142
'property doc comment must be multi line',
144143
$commentEnd,
145144
'NotMultiLineDocBlock'
146145
);
147146
}
148-
}
149-
} elseif ($code === T_COMMENT) {
147+
}//end if
148+
} else if ($code === T_COMMENT) {
150149
$commentStart = $phpcsFile->findPrevious(T_COMMENT, $commentEnd, null, true);
151150
$phpcsFile->addError(
152151
'property doc comment must begin with /**',
153-
$commentStart + 1,
152+
($commentStart + 1),
154153
'NotADocBlock'
155154
);
156-
}
157-
}
158-
}
159-
155+
}//end if
156+
157+
}//end processTokenWithinScope()
158+
159+
160+
}//end class

Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

+21-14
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@ class MO4_Sniffs_Formatting_AlphabeticalUseStatementsSniff
3131
extends PSR2_Sniffs_Namespaces_UseDeclarationSniff
3232
{
3333
/**
34-
* last use statement seen in group
34+
* Last use statement seen in group
3535
*
3636
* @var string
3737
*/
3838
private $_lastUseStatement = '';
3939

4040
/**
41-
* line number of the last seen use statement
41+
* Line number of the last seen use statement
4242
*
4343
* @var int
4444
*/
4545
private $_lastLine = -1;
4646

4747
/**
48-
* current file
48+
* Current file
4949
*
5050
* @var string
5151
*/
5252
private $_currentFile = null;
5353

54+
5455
/**
5556
* Processes this test, when one of its tokens is encountered.
5657
*
@@ -65,18 +66,22 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6566
parent::process($phpcsFile, $stackPtr);
6667

6768
if ($this->_currentFile !== $phpcsFile->getFilename()) {
68-
$this->_lastLine = -1;
69+
$this->_lastLine = -1;
6970
$this->_lastUseStatement = '';
70-
$this->_currentFile = $phpcsFile->getFilename();
71+
$this->_currentFile = $phpcsFile->getFilename();
7172
}
7273

7374
$tokens = $phpcsFile->getTokens();
7475
$line = $tokens[$stackPtr]['line'];
7576

76-
// ignore function () use () {...}
77+
// Ignore function () use () {...}.
7778
$prev = $phpcsFile->findPrevious(
78-
[T_WHITESPACE, T_COMMENT, T_DOC_COMMENT],
79-
$stackPtr - 1,
79+
[
80+
T_WHITESPACE,
81+
T_COMMENT,
82+
T_DOC_COMMENT,
83+
],
84+
($stackPtr - 1),
8085
null,
8186
true,
8287
null,
@@ -91,14 +96,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9196
}
9297
}
9398

94-
$start = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
99+
$start = $phpcsFile->findNext(T_STRING, ($stackPtr + 1));
95100
if ($start === false) {
96101
return;
97102
}
98103

99-
$end = $phpcsFile->findNext([T_AS, T_SEMICOLON, T_COMMA], $stackPtr + 1);
104+
$end = $phpcsFile->findNext([T_AS, T_SEMICOLON, T_COMMA], ($stackPtr + 1));
100105

101-
$currentUseStatement = $phpcsFile->getTokensAsString($start, $end - $start);
106+
$currentUseStatement = $phpcsFile->getTokensAsString($start, ($end - $start));
102107

103108
if (($this->_lastLine + 1) < $line) {
104109
$this->_lastLine = $line;
@@ -117,6 +122,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
117122

118123
$this->_lastUseStatement = $currentUseStatement;
119124
$this->_lastLine = $line;
120-
}
121-
}
122-
125+
126+
}//end process()
127+
128+
129+
}//end class

0 commit comments

Comments
 (0)