Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Libero\CodingStandard\Libero\Sniffs\ControlStructures;

use Exception;
use PHP_CodeSniffer\Files\File;
use SlevomatCodingStandard\Sniffs\ControlStructures\RequireTernaryOperatorSniff as BaseRequireTernaryOperatorSniff;
use function strpos;

// This is only present to catch exceptions.

final class RequireTernaryOperatorSniff extends BaseRequireTernaryOperatorSniff
{
public function process(File $phpcsFile, $stackPtr) : void
{
try {
parent::process($phpcsFile, $stackPtr);
} catch (Exception $e) {
if (false === strpos($e->getMessage(), 'without curly braces is not supported')) {
throw $e;
}
}
}
}
2 changes: 2 additions & 0 deletions src/Libero/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<rule ref="SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses"/>
<rule ref="SlevomatCodingStandard.ControlStructures.NewWithParentheses"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.UselessTernaryOperator"/>
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
Expand Down
6 changes: 1 addition & 5 deletions tests/cases/php/null-coalesce
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ $bar = $bar ?? 'bar';

$bar = $bar['baz'] ?? 'baz';

if (isset($foo)) {
$bar = $foo;
} else {
$bar = 'foo';
}
$bar = $foo ?? 'foo';

$fooBar = isset($foo, $bar) ? 'foo' : 'bar';

Expand Down
4 changes: 2 additions & 2 deletions tests/cases/php/parentheses-useless
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Parentheses must not be used unnecessarily
declare(strict_types=1);

$foo = ('bar');
$baz = ($qux !== null) ? true : false;
$baz = ($qux !== null) ? true : null;
$quux = ($quuz) ? 1 : 0;
$corge = (true || false) ? 0 : 1;

Expand All @@ -22,7 +22,7 @@ switch ($grault) {
declare(strict_types=1);

$foo = 'bar';
$baz = $qux !== null ? true : false;
$baz = $qux !== null ? true : null;
$quux = $quuz ? 1 : 0;
$corge = (true || false) ? 0 : 1;

Expand Down
37 changes: 37 additions & 0 deletions tests/cases/php/ternary
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---DESCRIPTION---
Ternary expressions should be used
---CONTENTS---
<?php

declare(strict_types=1);

$foo = static function ($bar) : string {
if ($bar) {
return $bar;
} else {
return 'baz';
}
};

$qux = static function ($quux) : string {
if ($quux) {
return 'quuz';
} else {
return 'corge';
}
};

---FIXED---
<?php

declare(strict_types=1);

$foo = static function ($bar) : string {
return $bar ?: 'baz';
};

$qux = static function ($quux) : string {
return $quux ? 'quuz' : 'corge';
};

---
29 changes: 29 additions & 0 deletions tests/cases/php/ternary-useless
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---DESCRIPTION---
Ternary expressions must not be used unnecessarily
---CONTENTS---
<?php

declare(strict_types=1);

$foo = static function ($bar) : bool {
return $bar ? $bar : false;
};

$baz = static function ($qux) : bool {
return $qux === false ? false : true;
};

---FIXED---
<?php

declare(strict_types=1);

$foo = static function ($bar) : bool {
return $bar ?: false;
};

$baz = static function ($qux) : bool {
return $qux !== false;
};

---