Skip to content

Commit ce8aa8c

Browse files
Merge pull request #1 from VincentLanglet/staging
Add dimabarbul works
2 parents 240ceed + 1bed08e commit ce8aa8c

39 files changed

+960
-442
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
cache.properties
2-
phpunit.xml
3-
composer.lock
1+
.idea/
42
composer.phar
53
vendor/
6-
build/

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
11
# Symfony3 Custom PHP CodeSniffer Coding Standard
22

3-
This is a fork of https://github.com/endouble/Symfony3-coding-standard
4-
These are the Symfony2 standards, but tweaked to meet some needs we have in our CSB project, for example to comply with
5-
[PSR-12](https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md) for PHP 7
3+
This is a fork of [endouble/Symfony3-coding-standard](https://github.com/endouble/Symfony3-coding-standard).
4+
These are the Symfony standards, but tweaked to meet some needs I have in my project, for example to comply with
5+
[PSR-12](https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md) for PHP 7.
66

77
## Installation
88

99
### Composer
1010

1111
This standard can be installed with the [Composer](https://getcomposer.org/) dependency manager.
1212

13-
1. Add the repository to your composer.json:
13+
1. Add the repository to your composer.json:
14+
1415
```json
15-
"repositories": [
16+
"repositories": [
1617
{
1718
"type": "vcs",
1819
"url": "git@github.com:VincentLanglet/Symfony3-custom-coding-standard"
1920
}
21+
]
2022
```
2123

2224
2. Add the coding standard as a dependency of your project
2325

2426
```json
25-
"require-dev": {
26-
"vincentlanglet/symfony3-custom-coding-standard": "^2.10"
27+
"require-dev": {
28+
"vincentlanglet/symfony3-custom-coding-standard": "^2.18"
2729
},
2830
```
2931

3032
3. Add the coding standard to the PHP_CodeSniffer install path
3133

3234
The path is relative to the php_codesniffer install path. This is important to make it work both in your vagrant, local machine and PHPStorm
3335

34-
bin/phpcs --config-set installed_paths ../../vincentlanglet/symfony3-custom-coding-standard
36+
```
37+
bin/phpcs --config-set installed_paths ../../vincentlanglet/symfony3-custom-coding-standard
38+
```
3539

3640
4. Check the installed coding standards
3741

38-
bin/phpcs -i
42+
```
43+
bin/phpcs -i
44+
```
3945

4046
5. Done!
4147

42-
bin/phpcs --standard=Symfony3Custom /path/to/code
43-
48+
```
49+
bin/phpcs --standard=Symfony3Custom /path/to/code
50+
```
51+
4452
6. (optional) Set up PHPStorm
4553

4654
- configure code sniffer under Languages & Frameworks -> PHP -> Code Sniffer

Symfony3Custom/Sniffs/Arrays/MultiLineArrayCommaSniff.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
<?php
2-
/**
3-
* This file is part of the Symfony3Custom-coding-standard (phpcs standard)
4-
*
5-
* PHP version 5
6-
*
7-
* @category PHP
8-
* @package PHP_CodeSniffer-Symfony3Custom
9-
* @author wicliff wolda <dev@bloody-wicked.com>
10-
* @license http://spdx.org/licenses/MIT MIT License
11-
* @version GIT: master
12-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
13-
*/
142

153
/**
16-
* Symfony3Custom_Sniffs_WhiteSpace_MultiLineArrayCommaSniff.
17-
*
18-
* Throws warnings if the last item in a multi line array does not have a
19-
* trailing comma
20-
*
21-
* @category PHP
22-
* @package PHP_CodeSniffer-Symfony3Custom
23-
* @author wicliff wolda <dev@bloody-wicked.com>
24-
* @license http://spdx.org/licenses/MIT MIT License
25-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
4+
* Throws warnings if the last item in a multi line array does not have a trailing comma
265
*/
276
class Symfony3Custom_Sniffs_Arrays_MultiLineArrayCommaSniff
287
implements PHP_CodeSniffer_Sniff

Symfony3Custom/Sniffs/Classes/MultipleClassesOneFileSniff.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
11
<?php
22

3-
/**
4-
* This file is part of the Symfony3Custom-coding-standard (phpcs standard)
5-
*
6-
* PHP version 5
7-
*
8-
* @category PHP
9-
* @package Symfony3Custom-coding-standard
10-
* @author Authors <Symfony3Custom-coding-standard@escapestudios.github.com>
11-
* @license http://spdx.org/licenses/MIT MIT License
12-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
13-
*/
14-
153
/**
164
* Throws errors if multiple classes are defined in a single file.
17-
*
18-
* PHP version 5
19-
*
205
* Symfony coding standard specifies: "Define one class per file;"
21-
*
22-
* @category PHP
23-
* @package Symfony3Custom-coding-standard
24-
* @author Authors <Symfony3Custom-coding-standard@escapestudios.github.com>
25-
* @license http://spdx.org/licenses/MIT MIT License
26-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
276
*/
287
class Symfony3Custom_Sniffs_Classes_MultipleClassesOneFileSniff
298
implements PHP_CodeSniffer_Sniff

Symfony3Custom/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,7 @@
1313
*/
1414

1515
/**
16-
* Symfony3Custom_Sniffs_Classes_PropertyDeclarationSniff.
17-
*
1816
* Throws warnings if properties are declared after methods
19-
*
20-
* PHP version 5
21-
*
22-
* @category PHP
23-
* @package Symfony3Custom-coding-standard
24-
* @author Authors <Symfony3Custom-coding-standard@escapestudios.github.com>
25-
* @license http://spdx.org/licenses/MIT MIT License
26-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
2717
*/
2818
class Symfony3Custom_Sniffs_Classes_PropertyDeclarationSniff
2919
implements PHP_CodeSniffer_Sniff

Symfony3Custom/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
<?php
22

3-
/**
4-
* This file is part of the Symfony3Custom-coding-standard (phpcs standard)
5-
*
6-
* PHP version 5
7-
*
8-
* @category PHP
9-
* @package Symfony3Custom-coding-standard
10-
* @author Authors <Symfony3Custom-coding-standard@escapestudios.github.com>
11-
* @license http://spdx.org/licenses/MIT MIT License
12-
* @link https://github.com/escapestudios/Symfony3Custom-coding-standard
13-
*/
14-
153
if (class_exists('PHP_CodeSniffer_Tokenizers_Comment', true) === false) {
164
$error = 'Class PHP_CodeSniffer_Tokenizers_Comment not found';
175
throw new PHP_CodeSniffer_Exception($error);
@@ -35,14 +23,6 @@
3523
* <li>Check the indentation of each tag.</li>
3624
* <li>Check required and optional tags and the format of their content.</li>
3725
* </ul>
38-
*
39-
* PHP version 5
40-
*
41-
* @category PHP
42-
* @package Symfony3Custom-coding-standard
43-
* @author Authors <Symfony3Custom-coding-standard@escapestudios.github.com>
44-
* @license http://spdx.org/licenses/MIT MIT License
45-
* @link http://pear.php.net/package/PHP_CodeSniffer
4626
*/
4727
class Symfony3Custom_Sniffs_Commenting_ClassCommentSniff
4828
extends PEAR_Sniffs_Commenting_ClassCommentSniff
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Throws errors if forbidden tags are met.
5+
*/
6+
class Symfony3Custom_Sniffs_Commenting_DocCommentForbiddenTagsSniff
7+
implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* A list of PHPDoc tags that are forbidden.
11+
*
12+
* @var array
13+
*/
14+
public $tags = array('@package', '@subpackage');
15+
16+
/**
17+
* A list of tokenizers this sniff supports.
18+
*
19+
* @return array
20+
*/
21+
public function register()
22+
{
23+
return array(T_DOC_COMMENT_TAG);
24+
}
25+
26+
/**
27+
* Processes this test, when one of its tokens is encountered.
28+
*
29+
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
30+
* @param int $stackPtr The position of the current token in
31+
* the stack passed in $tokens.
32+
*
33+
* @return void
34+
*/
35+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
36+
{
37+
$tokens = $phpcsFile->getTokens();
38+
if (in_array(
39+
$tokens[$stackPtr]['content'],
40+
$this->tags
41+
)
42+
) {
43+
$phpcsFile->addError(
44+
'The %s annotation is forbidden to use',
45+
$stackPtr,
46+
'',
47+
array($tokens[$stackPtr]['content'])
48+
);
49+
}
50+
}
51+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
/**
4+
* Throws errors if comments are not grouped by type with one blank line between them.
5+
*/
6+
class Symfony3Custom_Sniffs_Commenting_DocCommentGroupSameTypeSniff
7+
implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* A list of tokenizers this sniff supports.
11+
*
12+
* @return array
13+
*/
14+
public function register()
15+
{
16+
return array(T_DOC_COMMENT_OPEN_TAG);
17+
}
18+
19+
/**
20+
* Processes this test, when one of its tokens is encountered.
21+
*
22+
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
23+
* @param int $stackPtr The position of the current token in
24+
* the stack passed in $tokens.
25+
*
26+
* @return void
27+
*/
28+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
29+
{
30+
$tokens = $phpcsFile->getTokens();
31+
32+
$previousType = '';
33+
foreach ($tokens[$stackPtr]['comment_tags'] as $commentTag) {
34+
$currentType = $tokens[$commentTag]['content'];
35+
36+
$previousString = $phpcsFile->findPrevious(
37+
T_DOC_COMMENT_STRING,
38+
$commentTag,
39+
$stackPtr
40+
);
41+
42+
if (false !== $previousString) {
43+
$previousStringLine = $tokens[$previousString]['line'];
44+
$commentTagLine = $tokens[$commentTag]['line'];
45+
if ($previousType === $currentType) {
46+
if ($previousStringLine !== $commentTagLine - 1) {
47+
$fix = $phpcsFile->addFixableError(
48+
'Expected no empty lines '
49+
.'between annotations of the same type',
50+
$commentTag,
51+
'SameType'
52+
);
53+
if ($fix) {
54+
$phpcsFile->fixer->beginChangeset();
55+
$this->removeLines(
56+
$phpcsFile,
57+
$previousString,
58+
$previousStringLine + 1,
59+
$commentTagLine - 1
60+
);
61+
$phpcsFile->fixer->endChangeset();
62+
}
63+
}
64+
} else {
65+
if ($previousStringLine !== $commentTagLine - 2) {
66+
$fix = $phpcsFile->addFixableError(
67+
'Expected exactly one empty line '
68+
.'between annotations of different types',
69+
$commentTag,
70+
'DifferentType'
71+
);
72+
if ($fix) {
73+
$phpcsFile->fixer->beginChangeset();
74+
if ($previousStringLine === $commentTagLine - 1) {
75+
$firstOnLine = $phpcsFile->findFirstOnLine(
76+
array(),
77+
$commentTag,
78+
true
79+
);
80+
$star = $phpcsFile->findNext(
81+
T_DOC_COMMENT_STAR,
82+
$firstOnLine
83+
);
84+
$content = $phpcsFile->getTokensAsString(
85+
$firstOnLine,
86+
$star - $firstOnLine + 1
87+
);
88+
$phpcsFile->fixer->addContentBefore(
89+
$firstOnLine,
90+
$content.$phpcsFile->eolChar
91+
);
92+
} else {
93+
$this->removeLines(
94+
$phpcsFile,
95+
$previousString,
96+
$previousStringLine + 2,
97+
$commentTagLine - 1
98+
);
99+
}
100+
$phpcsFile->fixer->endChangeset();
101+
}
102+
}
103+
}
104+
}
105+
106+
$previousType = $currentType;
107+
}
108+
}
109+
110+
/**
111+
* Remove all tokens on lines (inclusively).
112+
*
113+
* Note: this method does not start or end changeset.
114+
*
115+
* @param PHP_CodeSniffer_File $phpcsFile File to make changes in
116+
* @param int $fromPtr Start searching tokens from here
117+
* @param int $fromLine First line to delete tokens from
118+
* @param int $toLine Last line to delete tokens from
119+
*
120+
* @return void
121+
*/
122+
protected function removeLines(
123+
PHP_CodeSniffer_File $phpcsFile, $fromPtr, $fromLine, $toLine
124+
) {
125+
$tokens = $phpcsFile->getTokens();
126+
for ($i = $fromPtr; ; $i++) {
127+
if ($tokens[$i]['line'] > $toLine) {
128+
break;
129+
}
130+
131+
if ($fromLine <= $tokens[$i]['line']) {
132+
$phpcsFile->fixer->replaceToken($i, '');
133+
}
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)