Skip to content

Commit 94d69cd

Browse files
authored
Merge pull request #266 from simPod/union
feat: add CS for unions
2 parents f86c16a + 05f2f54 commit 94d69cd

File tree

5 files changed

+102
-19
lines changed

5 files changed

+102
-19
lines changed

lib/Doctrine/ruleset.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@
419419
</property>
420420
</properties>
421421
</rule>
422+
<!-- Define unions style -->
423+
<rule ref="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat">
424+
<properties>
425+
<property name="withSpaces" value="no" />
426+
<property name="shortNullable" value="no" />
427+
<property name="nullPosition" value="last" />
428+
</properties>
429+
</rule>
422430
<!-- Forbid useless @var for constants -->
423431
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
424432
<!-- Require One Line Doc Comment where there's only 1 annotation present -->

tests/fixed/type-hints.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ public function get(Iterator $iterator): Traversable
2222
return $this->parameter;
2323
}
2424
}
25+
26+
class UnionTypeHints
27+
{
28+
/** @var int|string|null */
29+
private $x = 1;
30+
}

tests/input/type-hints.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ public function get(Iterator $iterator): Traversable
2222
return $this->parameter;
2323
}
2424
}
25+
26+
class UnionTypeHints
27+
{
28+
/** @var null|int|string */
29+
private $x = 1;
30+
}

tests/php74-compatibility.patch

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index fd5432c..233e24d 100644
2+
index c644926..1854dbb 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
55
@@ -15,7 +15,7 @@ tests/input/ControlStructures.php 28 0
@@ -28,22 +28,22 @@ index fd5432c..233e24d 100644
2828
tests/input/trailing_comma_on_array.php 1 0
2929
tests/input/traits-uses.php 11 0
3030
-tests/input/type-hints.php 4 0
31-
+tests/input/type-hints.php 5 0
31+
+tests/input/type-hints.php 8 0
3232
tests/input/UnusedVariables.php 1 0
3333
tests/input/use-ordering.php 1 0
3434
tests/input/useless-semicolon.php 2 0
3535
tests/input/UselessConditions.php 20 0
3636
----------------------------------------------------------------------
3737
-A TOTAL OF 377 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
38-
+A TOTAL OF 386 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
38+
+A TOTAL OF 389 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
3939
----------------------------------------------------------------------
4040
-PHPCBF CAN FIX 313 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
41-
+PHPCBF CAN FIX 322 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
41+
+PHPCBF CAN FIX 323 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
4242
----------------------------------------------------------------------
4343

4444

4545
diff --git a/tests/fixed/example-class.php b/tests/fixed/example-class.php
46-
index dbec9cb..dca22ed 100644
46+
index 998e51d..7866379 100644
4747
--- a/tests/fixed/example-class.php
4848
+++ b/tests/fixed/example-class.php
4949
@@ -25,14 +25,12 @@ class Example implements IteratorAggregate
@@ -112,7 +112,7 @@ index 8846dd1..51c361c 100644
112112
$bar = $bar['baz'] ?? 'baz';
113113

114114
diff --git a/tests/fixed/type-hints.php b/tests/fixed/type-hints.php
115-
index 0e952fc..9824fb0 100644
115+
index 10e6f34..bfa6d4f 100644
116116
--- a/tests/fixed/type-hints.php
117117
+++ b/tests/fixed/type-hints.php
118118
@@ -10,7 +10,7 @@ use Traversable;

tests/php80-compatibility.patch

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index c644926..d0f0a44 100644
2+
index c644926..d2640a7 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -15,18 +15,19 @@ tests/input/ControlStructures.php 28 0
5+
@@ -14,21 +14,22 @@ tests/input/constants-var.php 6 0
6+
tests/input/ControlStructures.php 28 0
67
tests/input/doc-comment-spacing.php 11 0
78
tests/input/duplicate-assignment-variable.php 1 0
8-
tests/input/EarlyReturn.php 6 0
9+
-tests/input/EarlyReturn.php 6 0
910
-tests/input/example-class.php 38 0
10-
+tests/input/example-class.php 42 0
11+
+tests/input/EarlyReturn.php 7 0
12+
+tests/input/example-class.php 43 0
1113
tests/input/forbidden-comments.php 14 0
1214
tests/input/forbidden-functions.php 6 0
1315
tests/input/inline_type_hint_assertions.php 7 0
@@ -24,8 +26,11 @@ index c644926..d0f0a44 100644
2426
tests/input/null_coalesce_operator.php 3 0
2527
+tests/input/null_safe_operator.php 1 0
2628
tests/input/optimized-functions.php 1 0
27-
tests/input/PropertyTypeHintSpacing.php 6 0
29+
-tests/input/PropertyTypeHintSpacing.php 6 0
30+
+tests/input/PropertyTypeHintSpacing.php 7 0
2831
tests/input/return_type_on_closures.php 21 0
32+
tests/input/return_type_on_methods.php 17 0
33+
tests/input/semicolon_spacing.php 3 0
2934
@@ -39,15 +40,15 @@ tests/input/superfluous-naming.php 11 0
3035
tests/input/test-case.php 8 0
3136
tests/input/trailing_comma_on_array.php 1 0
@@ -35,16 +40,30 @@ index c644926..d0f0a44 100644
3540
tests/input/UnusedVariables.php 1 0
3641
tests/input/use-ordering.php 1 0
3742
tests/input/useless-semicolon.php 2 0
38-
tests/input/UselessConditions.php 20 0
43+
-tests/input/UselessConditions.php 20 0
44+
+tests/input/UselessConditions.php 21 0
3945
----------------------------------------------------------------------
4046
-A TOTAL OF 377 ERRORS AND 0 WARNINGS WERE FOUND IN 41 FILES
41-
+A TOTAL OF 391 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
47+
+A TOTAL OF 395 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
4248
----------------------------------------------------------------------
4349
-PHPCBF CAN FIX 313 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
44-
+PHPCBF CAN FIX 327 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
50+
+PHPCBF CAN FIX 331 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
4551
----------------------------------------------------------------------
4652

4753

54+
diff --git a/tests/fixed/EarlyReturn.php b/tests/fixed/EarlyReturn.php
55+
index 5a82a93..7d5eb01 100644
56+
--- a/tests/fixed/EarlyReturn.php
57+
+++ b/tests/fixed/EarlyReturn.php
58+
@@ -11,7 +11,7 @@ class EarlyReturn
59+
return $bar === 'bar';
60+
}
61+
62+
- public function foo(): ?string
63+
+ public function foo(): string|null
64+
{
65+
foreach ($itens as $item) {
66+
if (! $item->isItem()) {
4867
diff --git a/tests/fixed/NamingCamelCase.php b/tests/fixed/NamingCamelCase.php
4968
index 57d9f2b..5493471 100644
5069
--- a/tests/fixed/NamingCamelCase.php
@@ -67,17 +86,41 @@ index 57d9f2b..5493471 100644
6786

6887
public function fcn(string $A): void
6988
{
89+
diff --git a/tests/fixed/PropertyTypeHintSpacing.php b/tests/fixed/PropertyTypeHintSpacing.php
90+
index 1421913..d52353c 100644
91+
--- a/tests/fixed/PropertyTypeHintSpacing.php
92+
+++ b/tests/fixed/PropertyTypeHintSpacing.php
93+
@@ -9,5 +9,5 @@ final class PropertyTypeHintSpacing
94+
public bool $boolPropertyWithDefaultValue = false;
95+
public string $stringProperty;
96+
public int $intProperty;
97+
- public ?string $nullableString = null;
98+
+ public string|null $nullableString = null;
99+
}
100+
diff --git a/tests/fixed/UselessConditions.php b/tests/fixed/UselessConditions.php
101+
index 2151b17..71e0cfb 100644
102+
--- a/tests/fixed/UselessConditions.php
103+
+++ b/tests/fixed/UselessConditions.php
104+
@@ -95,7 +95,7 @@ class UselessConditions
105+
return false;
106+
}
107+
108+
- public function nullShouldNotBeTreatedAsFalse(): ?bool
109+
+ public function nullShouldNotBeTreatedAsFalse(): bool|null
110+
{
111+
if (! $this->isAdmin) {
112+
return null;
70113
diff --git a/tests/fixed/example-class.php b/tests/fixed/example-class.php
71-
index 998e51d..b47d358 100644
114+
index 998e51d..56cd902 100644
72115
--- a/tests/fixed/example-class.php
73116
+++ b/tests/fixed/example-class.php
74-
@@ -25,17 +25,14 @@ class Example implements IteratorAggregate
117+
@@ -25,19 +25,16 @@ class Example implements IteratorAggregate
75118
{
76119
private const VERSION = PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;
77120

78121
- /** @var int|null */
79122
- private $foo;
80-
+ private ?int $foo = null;
123+
+ private int|null $foo = null;
81124

82125
/** @var string[] */
83126
- private $bar;
@@ -91,8 +134,20 @@ index 998e51d..b47d358 100644
91134
- private $baxBax;
92135
+ private ControlStructureSniff|int|string|null $baxBax = null;
93136

94-
public function __construct(?int $foo = null, array $bar = [], bool $baz = false, $baxBax = 'unused')
137+
- public function __construct(?int $foo = null, array $bar = [], bool $baz = false, $baxBax = 'unused')
138+
+ public function __construct(int|null $foo = null, array $bar = [], bool $baz = false, $baxBax = 'unused')
95139
{
140+
$this->foo = $foo;
141+
$this->bar = $bar;
142+
@@ -48,7 +45,7 @@ class Example implements IteratorAggregate
143+
/**
144+
* Description
145+
*/
146+
- public function getFoo(): ?int
147+
+ public function getFoo(): int|null
148+
{
149+
return $this->foo;
150+
}
96151
diff --git a/tests/fixed/new_with_parentheses.php b/tests/fixed/new_with_parentheses.php
97152
index 6e81bbe..47a06ec 100644
98153
--- a/tests/fixed/new_with_parentheses.php
@@ -151,7 +206,7 @@ index 5bbb636..7ce8a3d 100644
151206
-$var = $object === null ? null : $object->property;
152207
+$var = $object?->property;
153208
diff --git a/tests/fixed/type-hints.php b/tests/fixed/type-hints.php
154-
index 0e952fc..9824fb0 100644
209+
index 6033eca..5e26ed8 100644
155210
--- a/tests/fixed/type-hints.php
156211
+++ b/tests/fixed/type-hints.php
157212
@@ -10,7 +10,7 @@ use Traversable;
@@ -163,3 +218,11 @@ index 0e952fc..9824fb0 100644
163218

164219
/**
165220
* @param Iterator $iterator
221+
@@ -25,6 +25,5 @@ class TraversableTypeHints
222+
223+
class UnionTypeHints
224+
{
225+
- /** @var int|string|null */
226+
- private $x = 1;
227+
+ private int|string|null $x = 1;
228+
}

0 commit comments

Comments
 (0)