Skip to content

Commit f50b15e

Browse files
Xaver Lopenstedtmmoll
Xaver Lopenstedt
authored andcommitted
Add Constant Spacing Sniff, fixes #182
1 parent 2592077 commit f50b15e

8 files changed

+222
-1
lines changed

Diff for: CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
44
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55

6-
## [8.0.0] -
6+
## [8.0.0] -
7+
### Added
8+
- Add MO4.WhiteSpace.ConstantSpacing
79
### Changed
810
- refactored tests
911

Diff for: MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the mo4-coding-standard (phpcs standard)
5+
*
6+
* @author Xaver Loppenstedt <[email protected]>
7+
*
8+
* @license http://spdx.org/licenses/MIT MIT License
9+
*
10+
* @link https://github.com/mayflower/mo4-coding-standard
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace MO4\Sniffs\WhiteSpace;
16+
17+
use PHP_CodeSniffer\Files\File;
18+
use PHP_CodeSniffer\Sniffs\Sniff;
19+
20+
/**
21+
* Multi Line Array sniff.
22+
*
23+
* @author Xaver Loppenstedt <[email protected]>
24+
*
25+
* @copyright 2013-2021 Xaver Loppenstedt, some rights reserved.
26+
*
27+
* @license http://spdx.org/licenses/MIT MIT License
28+
*
29+
* @link https://github.com/mayflower/mo4-coding-standard
30+
*/
31+
class ConstantSpacingSniff implements Sniff
32+
{
33+
/**
34+
* Define all types of arrays.
35+
*
36+
* @var array
37+
*/
38+
protected $arrayTokens = [
39+
T_CONST,
40+
];
41+
42+
/**
43+
* Registers the tokens that this sniff wants to listen for.
44+
*
45+
* @return array<int, int>
46+
*
47+
* @see Tokens.php
48+
*/
49+
public function register(): array
50+
{
51+
return $this->arrayTokens;
52+
}
53+
54+
/**
55+
* Processes this test, when one of its tokens is encountered.
56+
*
57+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
58+
*
59+
* @param File $phpcsFile The file being scanned.
60+
* @param int $stackPtr The position of the current token in
61+
* the stack passed in $tokens.
62+
*
63+
* @return void
64+
*/
65+
public function process(File $phpcsFile, $stackPtr): void
66+
{
67+
$tokens = $phpcsFile->getTokens();
68+
$nextPtr = $stackPtr + 1;
69+
$next = $tokens[$nextPtr]['content'];
70+
71+
if (T_WHITESPACE !== $tokens[$nextPtr]['code']) {
72+
return;
73+
}
74+
75+
if (' ' === $next) {
76+
return;
77+
}
78+
79+
$fix = $phpcsFile->addFixableError(
80+
'Keyword const must be followed by a single space, but found %s',
81+
$stackPtr,
82+
'Incorrect',
83+
[\strlen($next)]
84+
);
85+
86+
if (true !== $fix) {
87+
return;
88+
}
89+
90+
$phpcsFile->fixer->replaceToken($nextPtr, ' ');
91+
}
92+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class X {
4+
protected const C1 = 'c1';
5+
public const C2 = 'c2';
6+
const C3 = 'c3';
7+
8+
public const NO_FAILURE = 'ok';
9+
public const FAILURE_REASON_CUSTOMER_NOT_FOUND = 'customer_not_found';
10+
public const FAILURE_REASON_NO_AUTH = 'no_auth';
11+
public const FAILURE_REASON_MAIL_MISSING = 'mail_missing';
12+
public const FAILURE_REASON_PASSWORD_MISSING = 'password_missing';
13+
public const FAILURE_REASON_MAIL_PASSWORD_MISSING = 'mail_password_missing';
14+
public const FAILURE_REASON_CUSTOMER_DELETED = 'customer_deleted';
15+
public const FAILURE_REASON_COMMUNICATION = 'communication';
16+
public const FAILURE_REASON_UNKNOWN = 'unknown';
17+
18+
const
19+
W00T = 'w00t';
20+
}
21+
22+
const NO = 0;
23+
const O = '0';
24+
const N = '1';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
class X {
4+
protected const C1 = 'c1';
5+
public const C2 = 'c2';
6+
const C3 = 'c3';
7+
8+
public const NO_FAILURE = 'ok';
9+
public const FAILURE_REASON_CUSTOMER_NOT_FOUND = 'customer_not_found';
10+
public const FAILURE_REASON_NO_AUTH = 'no_auth';
11+
public const FAILURE_REASON_MAIL_MISSING = 'mail_missing';
12+
public const FAILURE_REASON_PASSWORD_MISSING = 'password_missing';
13+
public const FAILURE_REASON_MAIL_PASSWORD_MISSING = 'mail_password_missing';
14+
public const FAILURE_REASON_CUSTOMER_DELETED = 'customer_deleted';
15+
public const FAILURE_REASON_COMMUNICATION = 'communication';
16+
public const FAILURE_REASON_UNKNOWN = 'unknown';
17+
18+
const W00T = 'w00t';
19+
}
20+
21+
const NO = 0;
22+
const O = '0';
23+
const N = '1';
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class X {
4+
protected const C1 = 'c1';
5+
public const C2 = 'c2';
6+
const C3 = 'c3';
7+
8+
public const NO_FAILURE = 'ok';
9+
public const FAILURE_REASON_CUSTOMER_NOT_FOUND = 'customer_not_found';
10+
public const FAILURE_REASON_NO_AUTH = 'no_auth';
11+
public const FAILURE_REASON_MAIL_MISSING = 'mail_missing';
12+
public const FAILURE_REASON_PASSWORD_MISSING = 'password_missing';
13+
public const FAILURE_REASON_MAIL_PASSWORD_MISSING = 'mail_password_missing';
14+
public const FAILURE_REASON_CUSTOMER_DELETED = 'customer_deleted';
15+
public const FAILURE_REASON_COMMUNICATION = 'communication';
16+
public const FAILURE_REASON_UNKNOWN = 'unknown';
17+
18+
const;
19+
}

Diff for: MO4/Tests/WhiteSpace/ConstantSpacingUnitTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the mo4-coding-standard (phpcs standard)
5+
*
6+
* @author Xaver Loppenstedt <[email protected]>
7+
*
8+
* @license http://spdx.org/licenses/MIT MIT License
9+
*
10+
* @link https://github.com/mayflower/mo4-coding-standard
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace MO4\Tests\WhiteSpace;
16+
17+
use MO4\Tests\AbstractMo4SniffUnitTest;
18+
19+
/**
20+
* Unit test class for the VariableInDoubleQuotedString sniff.
21+
*
22+
* A sniff unit test checks a .inc file for expected violations of a single
23+
* coding standard. Expected errors and warnings are stored in this class.
24+
*
25+
* @author Xaver Loppenstedt <[email protected]>
26+
*
27+
* @copyright 2013-2021 Xaver Loppenstedt, some rights reserved.
28+
*
29+
* @license http://spdx.org/licenses/MIT MIT License
30+
*
31+
* @link https://github.com/mayflower/mo4-coding-standard
32+
*/
33+
class ConstantSpacingUnitTest extends AbstractMo4SniffUnitTest
34+
{
35+
protected $expectedErrorList = [
36+
'ConstantSpacingUnitTest.pass.inc' => [],
37+
'ConstantSpacingUnitTest.fail.inc' => [
38+
4 => 1,
39+
5 => 1,
40+
6 => 1,
41+
10 => 1,
42+
12 => 1,
43+
13 => 1,
44+
14 => 1,
45+
15 => 1,
46+
18 => 1,
47+
22 => 1,
48+
23 => 1,
49+
24 => 1,
50+
],
51+
];
52+
}

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ To change the sorting order for your project, add this snippet to your custom `r
7777
### MO4.Strings.VariableInDoubleQuotedString
7878
* Interpolated variables in double quoted strings must be surrounded by `{ }`, e.g. `{$VAR}` instead of `$VAR`.
7979

80+
## MO4.WhiteSpace.ConstantSpacing
81+
82+
* const must be followed by a single space.
83+
8084
### Further rules (imported from other standards)
8185
* See `MO4/ruleset.xml`, which has each imported rule commented.
8286

Diff for: integrationtests/testfile.php

+5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313

1414
namespace Acme;
1515

16+
const BAM = 1;
17+
1618
/**
1719
* Coding standards demonstration.
1820
*/
1921
class FooBar
2022
{
2123
public const SOME_CONST = 42;
24+
public const STR_CONST = '43';
25+
protected const PROTECT = 0;
26+
public const LALA = 'lala';
2227

2328
/**
2429
* @var string

0 commit comments

Comments
 (0)