Skip to content

Commit 23404e5

Browse files
committed
Adopt CS
1 parent 045e241 commit 23404e5

14 files changed

+335
-167
lines changed

lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ final class ControlStructureSniff implements Sniff
2222
/**
2323
* Returns an array of tokens this test wants to listen for.
2424
*
25-
* @return array
25+
* @return int[]
2626
*/
27-
public function register()
27+
public function register() : array
2828
{
2929
return [
3030
\T_IF,
@@ -40,7 +40,11 @@ public function register()
4040
];
4141
}
4242

43-
public function process(File $phpcsFile, $stackPtr)
43+
/**
44+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
45+
* @param int $stackPtr
46+
*/
47+
public function process(File $phpcsFile, $stackPtr) : void
4448
{
4549
$tokens = $phpcsFile->getTokens();
4650

@@ -55,6 +59,9 @@ public function process(File $phpcsFile, $stackPtr)
5559
$this->validateParenthesisCloser($phpcsFile, $tokens, $stackPtr, $openerPosition, $closerPosition);
5660
}
5761

62+
/**
63+
* @param mixed[] $tokens
64+
*/
5865
private function validateParenthesisOpener(File $file, array $tokens, int $position, int $openerPosition) : void
5966
{
6067
$nextTokenPosition = $openerPosition + 1;
@@ -83,6 +90,9 @@ private function validateParenthesisOpener(File $file, array $tokens, int $posit
8390
$file->fixer->replaceToken($nextTokenPosition, '');
8491
}
8592

93+
/**
94+
* @param mixed[][] $tokens
95+
*/
8696
private function validateParenthesisCloser(
8797
File $file,
8898
array $tokens,

lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,86 @@
44

55
namespace Doctrine\Sniffs\Spacing;
66

7-
use PHP_CodeSniffer\Sniffs\Sniff;
87
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
99

1010
final class SpaceOnNotSniff implements Sniff
1111
{
12-
use EnsureSpaces;
13-
1412
private const MESSAGE = 'There must be a single space %s a NOT operator; %d found';
1513

16-
public function register()
14+
/**
15+
* @return int[]
16+
*/
17+
public function register() : array
1718
{
1819
return [\T_BOOLEAN_NOT];
1920
}
2021

21-
public function process(File $phpcsFile, $stackPtr)
22+
/**
23+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
24+
* @param int $stackPtr
25+
*/
26+
public function process(File $phpcsFile, $stackPtr) : void
2227
{
2328
$tokens = $phpcsFile->getTokens();
2429

2530
$this->ensureSpaceBefore($phpcsFile, $tokens, $stackPtr, self::MESSAGE);
2631
$this->ensureSpaceAfter($phpcsFile, $tokens, $stackPtr, self::MESSAGE);
2732
}
33+
34+
/**
35+
* @param mixed[][] $tokens
36+
*/
37+
protected function ensureSpaceBefore(File $file, array $tokens, int $position, string $message) : void
38+
{
39+
$spacing = $this->numberOfSpaces($tokens, $position - 1);
40+
41+
if ($spacing === 1) {
42+
return;
43+
}
44+
45+
if ( ! $file->addFixableError($message, $position, 'before', ['before', $spacing])) {
46+
return;
47+
}
48+
49+
if ($spacing === 0) {
50+
$file->fixer->addContentBefore($position, ' ');
51+
return;
52+
}
53+
54+
$file->fixer->replaceToken($position - 1, ' ');
55+
}
56+
57+
/**
58+
* @param mixed[][] $tokens
59+
*/
60+
protected function ensureSpaceAfter(File $file, array $tokens, int $position, string $message) : void
61+
{
62+
$spacing = $this->numberOfSpaces($tokens, $position + 1);
63+
64+
if ($spacing === 1) {
65+
return;
66+
}
67+
68+
if ( ! $file->addFixableError($message, $position, 'after', ['after', $spacing])) {
69+
return;
70+
}
71+
72+
if ($spacing === 0) {
73+
$file->fixer->addContent($position, ' ');
74+
return;
75+
}
76+
77+
$file->fixer->replaceToken($position + 1, ' ');
78+
}
79+
80+
/**
81+
* @param mixed[][] $tokens
82+
*/
83+
private function numberOfSpaces(array $tokens, int $position) : int
84+
{
85+
$token = $tokens[$position];
86+
87+
return $token['code'] === T_WHITESPACE ? $token['length'] : 0;
88+
}
2889
}

tests/expected_report.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ PHP CODE SNIFFER REPORT SUMMARY
44
FILE ERRORS WARNINGS
55
----------------------------------------------------------------------
66
tests/input/concatenation_spacing.php 19 0
7+
tests/input/example-class.php 13 0
78
tests/input/not_spacing.php 7 0
89
tests/input/return_type_on_closures.php 21 0
910
tests/input/return_type_on_methods.php 16 0
1011
----------------------------------------------------------------------
11-
A TOTAL OF 63 ERRORS AND 0 WARNINGS WERE FOUND IN 4 FILES
12+
A TOTAL OF 76 ERRORS AND 0 WARNINGS WERE FOUND IN 5 FILES
1213
----------------------------------------------------------------------
13-
PHPCBF CAN FIX 63 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
14+
PHPCBF CAN FIX 73 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
1415
----------------------------------------------------------------------
1516

1617

tests/fixed/concatenation_spacing.php

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

3+
declare(strict_types=1);
4+
35
$string = 'Doctrine' . 'rulez';
46
$string = 'Doctrine' . 'rulez';
57
$string = 'Doctrine' . 'rulez';

tests/fixed/example-class.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Example;
6+
7+
use Doctrine\Sniffs\Spacing\ControlStructureSniff;
8+
use function strlen;
9+
use function substr;
10+
11+
/**
12+
* Description
13+
*/
14+
class Example implements \IteratorAggregate
15+
{
16+
/** @var int|null */
17+
private $foo;
18+
19+
/** @var string[] */
20+
private $bar;
21+
22+
/** @var bool */
23+
private $baz;
24+
25+
/** @var ControlStructureSniff|int|string|null */
26+
private $baxBax;
27+
28+
public function __construct(?int $foo = null, array $bar = [], bool $baz = false, $baxBax = 'unused')
29+
{
30+
$this->foo = $foo;
31+
$this->bar = $bar;
32+
$this->baz = $baz;
33+
$this->baxBax = $baxBax;
34+
}
35+
36+
/**
37+
* Description
38+
*/
39+
public function getFoo() : ?int
40+
{
41+
return $this->foo;
42+
}
43+
44+
/**
45+
* @return iterable
46+
*/
47+
public function getIterator() : array
48+
{
49+
assert($this->bar !== null);
50+
return new \ArrayIterator($this->bar);
51+
}
52+
53+
public function isBaz() : bool
54+
{
55+
return $this->baz;
56+
}
57+
58+
public function mangleBar(int $length) : void
59+
{
60+
if ( ! $this->baz) {
61+
throw new \InvalidArgumentException();
62+
}
63+
64+
$this->bar = (string) $this->baxBax ?? substr($this->bar, strlen($this->bar - $length));
65+
}
66+
}

tests/fixed/not_spacing.php

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

3+
declare(strict_types=1);
4+
35
$test = 1;
46

57
if ( ! $test > 0) {
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
function () : void {
46
}
57

@@ -16,46 +18,46 @@ function () : void {
1618
}
1719

1820
function (
19-
$a,
20-
$c,
21-
$d,
22-
$e,
23-
$b
21+
int $a,
22+
int $c,
23+
int $d,
24+
int $e,
25+
int $b
2426
) : void {
2527
}
2628

2729
function (
28-
$a,
29-
$c,
30-
$d,
31-
$e,
32-
$b
30+
int $a,
31+
int $c,
32+
int $d,
33+
int $e,
34+
int $b
3335
) : void {
3436
}
3537

3638
function (
37-
$a,
38-
$c,
39-
$d,
40-
$e,
41-
$b
39+
int $a,
40+
int $c,
41+
int $d,
42+
int $e,
43+
int $b
4244
) : void {
4345
}
4446

4547
function (
46-
$a,
47-
$c,
48-
$d,
49-
$e,
50-
$b
48+
int $a,
49+
int $c,
50+
int $d,
51+
int $e,
52+
int $b
5153
) : void {
5254
}
5355

5456
function (
55-
$a,
56-
$c,
57-
$d,
58-
$e,
59-
$b
57+
int $a,
58+
int $c,
59+
int $d,
60+
int $e,
61+
int $b
6062
) : void {
6163
}

0 commit comments

Comments
 (0)