33namespace SlevomatCodingStandard \Sniffs \TypeHints ;
44
55use SlevomatCodingStandard \Sniffs \TestCase ;
6+ use const PHP_VERSION_ID ;
67
78class DNFTypeHintFormatSniffTest extends TestCase
89{
@@ -188,7 +189,14 @@ public function testShortNullableNotSetNoErrors(): void
188189 $ report = self ::checkFile (__DIR__ . '/data/dnfTypeHintFormatShortNullableNotSetNoErrors.php ' , [
189190 'enable ' => true ,
190191 ], [DNFTypeHintFormatSniff::CODE_REQUIRED_SHORT_NULLABLE , DNFTypeHintFormatSniff::CODE_DISALLOWED_SHORT_NULLABLE ]);
191- self ::assertNoSniffErrorInFile ($ report );
192+
193+ // in tests there is no phpcs config "php_version" value set, which means it will use the runner PHP version
194+ if (PHP_VERSION_ID >= 80000 ) {
195+ self ::assertNoSniffErrorInFile ($ report );
196+ } else {
197+ self ::assertSame (2 , $ report ->getErrorCount ());
198+ self ::assertSniffError ($ report , 8 , DNFTypeHintFormatSniff::CODE_REQUIRED_SHORT_NULLABLE );
199+ }
192200 }
193201
194202 public function testShortNullableRequiredNoErrors (): void
@@ -232,6 +240,7 @@ public function testShortNullableDisallowedNoErrors(): void
232240 'enable ' => true ,
233241 'shortNullable ' => 'no ' ,
234242 ], [DNFTypeHintFormatSniff::CODE_DISALLOWED_SHORT_NULLABLE ]);
243+
235244 self ::assertNoSniffErrorInFile ($ report );
236245 }
237246
@@ -242,6 +251,11 @@ public function testShortNullableDisallowedErrors(): void
242251 'shortNullable ' => 'no ' ,
243252 ], [DNFTypeHintFormatSniff::CODE_DISALLOWED_SHORT_NULLABLE ]);
244253
254+ if (PHP_VERSION_ID < 80000 ) {
255+ self ::assertNoSniffErrorInFile ($ report );
256+ return ;
257+ }
258+
245259 self ::assertSame (3 , $ report ->getErrorCount ());
246260
247261 self ::assertSniffError ($ report , 6 , DNFTypeHintFormatSniff::CODE_DISALLOWED_SHORT_NULLABLE );
@@ -285,9 +299,14 @@ public function testNullPositionFirstErrors(): void
285299 'nullPosition ' => 'first ' ,
286300 ], [DNFTypeHintFormatSniff::CODE_NULL_TYPE_HINT_NOT_ON_FIRST_POSITION ]);
287301
288- self ::assertSame (3 , $ report ->getErrorCount ());
302+ if (PHP_VERSION_ID >= 80000 ) {
303+ self ::assertSame (3 , $ report ->getErrorCount ());
304+
305+ self ::assertSniffError ($ report , 6 , DNFTypeHintFormatSniff::CODE_NULL_TYPE_HINT_NOT_ON_FIRST_POSITION );
306+ } else {
307+ self ::assertSame (2 , $ report ->getErrorCount ());
308+ }
289309
290- self ::assertSniffError ($ report , 6 , DNFTypeHintFormatSniff::CODE_NULL_TYPE_HINT_NOT_ON_FIRST_POSITION );
291310 self ::assertSniffError (
292311 $ report ,
293312 8 ,
@@ -301,7 +320,10 @@ public function testNullPositionFirstErrors(): void
301320 'Null type hint should be on first position in "string|null|\Anything". ' ,
302321 );
303322
304- self ::assertAllFixedInFile ($ report );
323+ // in PHP < 8 it would be changed to short nullable, which is not tested in this test
324+ if (PHP_VERSION_ID >= 80000 ) {
325+ self ::assertAllFixedInFile ($ report );
326+ }
305327 }
306328
307329 public function testNullPositionLastNoErrors (): void
@@ -320,9 +342,14 @@ public function testNullPositionLastErrors(): void
320342 'nullPosition ' => 'last ' ,
321343 ], [DNFTypeHintFormatSniff::CODE_NULL_TYPE_HINT_NOT_ON_LAST_POSITION ]);
322344
323- self ::assertSame (3 , $ report ->getErrorCount ());
345+ if (PHP_VERSION_ID >= 80000 ) {
346+ self ::assertSame (3 , $ report ->getErrorCount ());
347+
348+ self ::assertSniffError ($ report , 6 , DNFTypeHintFormatSniff::CODE_NULL_TYPE_HINT_NOT_ON_LAST_POSITION );
349+ } else {
350+ self ::assertSame (2 , $ report ->getErrorCount ());
351+ }
324352
325- self ::assertSniffError ($ report , 6 , DNFTypeHintFormatSniff::CODE_NULL_TYPE_HINT_NOT_ON_LAST_POSITION );
326353 self ::assertSniffError (
327354 $ report ,
328355 8 ,
@@ -336,7 +363,10 @@ public function testNullPositionLastErrors(): void
336363 'Null type hint should be on last position in "string|null|\Anything". ' ,
337364 );
338365
339- self ::assertAllFixedInFile ($ report );
366+ // in PHP < 8 it would be changed to short nullable, which is not tested in this test
367+ if (PHP_VERSION_ID >= 80000 ) {
368+ self ::assertAllFixedInFile ($ report );
369+ }
340370 }
341371
342372 public function testShouldNotReportIfSniffIsDisabled (): void
0 commit comments