Skip to content

Commit 8c36392

Browse files
authored
Merge pull request #43 from kubawerlos/feature/no-nullable-booleans
Add NoNullableBooleanTypeFixer
2 parents 055d6f4 + 8adfeda commit 8c36392

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

3+
## [Unreleased]
4+
- Add NoNullableBooleanTypeFixer
5+
36
## v1.8.0 - *2018-08-09*
47
- Add PhpdocSelfAccessorFixer
58

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ In your PHP CS Fixer configuration register fixers and use them:
8383
$y = new \Baz();
8484
```
8585

86+
- **NoNullableBooleanTypeFixer** - there must be no nullable boolean type.
87+
88+
*Risky: when the null is used.*
89+
```diff
90+
<?php
91+
-function foo(?bool $bar) : ?bool
92+
+function foo(bool $bar) : bool
93+
{
94+
return $bar;
95+
}
96+
```
97+
8698
- **NoPhpStormGeneratedCommentFixer** - there must be no comment generated by PhpStorm.
8799
```diff
88100
<?php
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PhpCsFixerCustomFixers\Fixer;
6+
7+
use PhpCsFixer\FixerDefinition\CodeSample;
8+
use PhpCsFixer\FixerDefinition\FixerDefinition;
9+
use PhpCsFixer\Tokenizer\Tokens;
10+
11+
final class NoNullableBooleanTypeFixer extends AbstractFixer
12+
{
13+
public function getDefinition(): FixerDefinition
14+
{
15+
return new FixerDefinition(
16+
'There must be no nullable boolean type.',
17+
[new CodeSample('<?php
18+
function foo(?bool $bar) : ?bool
19+
{
20+
return $bar;
21+
}
22+
')],
23+
null,
24+
'When the null is used.'
25+
);
26+
}
27+
28+
public function isCandidate(Tokens $tokens): bool
29+
{
30+
return $tokens->isTokenKindFound(T_STRING);
31+
}
32+
33+
public function isRisky(): bool
34+
{
35+
return true;
36+
}
37+
38+
public function fix(\SplFileInfo $file, Tokens $tokens): void
39+
{
40+
foreach ($tokens as $index => $token) {
41+
if ($token->getContent() !== '?') {
42+
continue;
43+
}
44+
45+
$nextIndex = $tokens->getNextMeaningfulToken($index);
46+
if (!$tokens[$nextIndex]->equals([T_STRING, 'bool'], false) && !$tokens[$nextIndex]->equals([T_STRING, 'boolean'], false)) {
47+
continue;
48+
}
49+
50+
$nextNextIndex = $tokens->getNextMeaningfulToken($nextIndex);
51+
if (!$tokens[$nextNextIndex]->isGivenKind(T_VARIABLE) && $tokens[$nextNextIndex]->getContent() !== '{') {
52+
continue;
53+
}
54+
55+
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
56+
}
57+
}
58+
59+
public function getPriority(): int
60+
{
61+
return 0;
62+
}
63+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Tests\Fixer;
6+
7+
/**
8+
* @internal
9+
*
10+
* @covers \PhpCsFixerCustomFixers\Fixer\NoNullableBooleanTypeFixer
11+
*/
12+
final class NoNullableBooleanTypeFixerTest extends AbstractFixerTestCase
13+
{
14+
public function testPriority(): void
15+
{
16+
static::assertSame(0, $this->fixer->getPriority());
17+
}
18+
19+
public function testIsRisky(): void
20+
{
21+
static::assertTrue($this->fixer->isRisky());
22+
}
23+
24+
/**
25+
* @param string $expected
26+
* @param string|null $input
27+
*
28+
* @dataProvider provideFixCases
29+
*/
30+
public function testFix(string $expected, string $input = null): void
31+
{
32+
$this->doTest($expected, $input);
33+
}
34+
35+
public function provideFixCases(): \Iterator
36+
{
37+
yield [
38+
'<?php function foo(bool $b) {}',
39+
'<?php function foo(?bool $b) {}',
40+
];
41+
42+
yield [
43+
'<?php function foo(Bool $b) {}',
44+
'<?php function foo(?Bool $b) {}',
45+
];
46+
47+
yield [
48+
'<?php function foo(boolean $b) {}',
49+
'<?php function foo(?boolean $b) {}',
50+
];
51+
52+
yield [
53+
'<?php function foo() : bool {}',
54+
'<?php function foo() : ?bool {}',
55+
];
56+
57+
yield [
58+
'<?php function foo(bool $a, bool $b, bool $c, bool $d) {}',
59+
'<?php function foo(?bool $a, ?bool $b, ?bool $c, ?bool $d) {}',
60+
];
61+
yield [
62+
'<?php function foo( bool $b ) {}',
63+
'<?php function foo( ? bool $b ) {}',
64+
];
65+
66+
yield [
67+
'<?php function foo(?int $b) : ?string {}',
68+
];
69+
70+
yield [
71+
'<?php FOO ? BOOL : BAR;',
72+
];
73+
74+
yield [
75+
'<?php FOO ?: bool;',
76+
];
77+
}
78+
}

0 commit comments

Comments
 (0)