Skip to content

Commit

Permalink
✨ Add `SlevomatCodingStandard.ControlStructures.RequireNullSafeObject…
Browse files Browse the repository at this point in the history
…Operator`

Fixes #76
  • Loading branch information
dpi committed Jun 4, 2024
1 parent 06ad6e4 commit 545f391
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PreviousNextDrupal/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<!-- SlevomatCodingStandard.Commenting -->
<rule ref="SlevomatCodingStandard.Commenting.UselessInheritDocComment" />

<!-- SlevomatCodingStandard.ControlStructures -->
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator" />

<!-- SlevomatCodingStandard.Functions -->
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall" />
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration" />
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function doesAThing(array $data): int {
#### SlevomatCodingStandard.Classes.ClassStructure
#### SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature
#### SlevomatCodingStandard.Commenting.UselessInheritDocComment
#### SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInCall
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration
#### SlevomatCodingStandard.Functions.StaticClosure
Expand Down
29 changes: 29 additions & 0 deletions tests/Sniffs/RequireNullSafeObjectOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PreviousNext\CodingStandard\Tests\Sniffs;

use SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullSafeObjectOperatorSniff;

/**
* @covers \SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullSafeObjectOperatorSniff
*/
final class RequireNullSafeObjectOperatorTest extends Base {

public function testNoError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireNullSafeObjectOperatorNoError.php');
self::assertNoSniffErrorInFile($report);
}

public function testMissing(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireNullSafeObjectOperatorError.php');
self::assertSame(1, $report->getErrorCount());
self::assertSniffError($report, 6, RequireNullSafeObjectOperatorSniff::CODE_REQUIRED_NULL_SAFE_OBJECT_OPERATOR);
}

protected static function getSniffName(): string {
return 'SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator';
}

}
8 changes: 8 additions & 0 deletions tests/Sniffs/fixtures/RequireNullSafeObjectOperatorError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

$a = new \stdClass();
if ($a === NULL || $a->data === NULL) {
$c = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

$a = new \stdClass();
if ($a?->data === NULL) {
$c = 1;
}

0 comments on commit 545f391

Please sign in to comment.