Skip to content

Commit

Permalink
Merge pull request #146 from simPod/do-not-require-space-after-case
Browse files Browse the repository at this point in the history
Do not require space after case
  • Loading branch information
Ocramius committed Dec 11, 2019
2 parents 9056d24 + e91edf0 commit d8a60ec
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 127 deletions.
12 changes: 12 additions & 0 deletions lib/Doctrine/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing">
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeControlStructure" />
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeFirstControlStructure" />
<properties>
<property name="tokensToCheck" type="array">
<element value="T_IF" />
<element value="T_DO" />
<element value="T_WHILE" />
<element value="T_FOR" />
<element value="T_FOREACH" />
<element value="T_SWITCH" />
<element value="T_TRY" />
<element value="T_DEFAULT" />
</property>
</properties>
</rule>
<!-- Forbid fancy yoda conditions -->
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
Expand Down
7 changes: 4 additions & 3 deletions tests/expected_report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ tests/input/class-references.php 10 0
tests/input/concatenation_spacing.php 24 0
tests/input/constants-no-lsb.php 2 0
tests/input/constants-var.php 4 0
tests/input/ControlStructures.php 13 0
tests/input/doc-comment-spacing.php 10 0
tests/input/duplicate-assignment-variable.php 1 0
tests/input/EarlyReturn.php 6 0
tests/input/example-class.php 45 0
tests/input/example-class.php 34 0
tests/input/forbidden-comments.php 8 0
tests/input/forbidden-functions.php 6 0
tests/input/inline_type_hint_assertions.php 7 0
Expand All @@ -38,9 +39,9 @@ tests/input/use-ordering.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
A TOTAL OF 290 ERRORS AND 0 WARNINGS WERE FOUND IN 34 FILES
A TOTAL OF 292 ERRORS AND 0 WARNINGS WERE FOUND IN 35 FILES
----------------------------------------------------------------------
PHPCBF CAN FIX 229 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX 231 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


115 changes: 115 additions & 0 deletions tests/fixed/ControlStructures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace ControlStructures;

use InvalidArgumentException;
use Throwable;
use const PHP_VERSION;

class ControlStructures
{
private const VERSION = PHP_VERSION;

/**
* @return iterable<int>
*/
public function varAndIfNoSpaceBetween() : iterable
{
$var = 1;
if (self::VERSION === 0) {
yield 0;
}
}

/**
* @return iterable<int>
*/
public function ifAndYieldSpaceBetween() : iterable
{
if (self::VERSION === 0) {
yield 0;
}

yield 1;
}

/**
* @return iterable<int>
*/
public function ifAndYieldFromSpaceBetween() : iterable
{
if (self::VERSION === 0) {
yield 0;
}

yield from [];
}

public function ifAndThrowSpaceBetween() : void
{
if (self::VERSION === 0) {
return;
}

throw new InvalidArgumentException();
}

public function ifAndReturnSpaceBetween() : int
{
if (self::VERSION === 0) {
return 0;
}

return 1;
}

public function noSpaceAroundCase() : void
{
switch (self::VERSION) {
case 1:
case 2:
// do something
break;
case 3:
// do something else
break;
default:
}
}

public function spaceBelowBlocks() : void
{
if (true) {
echo 1;
}

do {
echo 2;
} while (true);

while (true) {
echo 3;
}

for ($i = 0; $i < 1; $i++) {
echo $i;
}

foreach ([] as $item) {
echo $item;
}

switch (true) {
default:
}

try {
echo 4;
} catch (Throwable $throwable) {
}

echo 5;
}
}
61 changes: 0 additions & 61 deletions tests/fixed/example-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Fancy\TestCase;
use InvalidArgumentException;
use IteratorAggregate;
use Throwable;
use function assert;
use function strlen as stringLength;
use function substr;
Expand Down Expand Up @@ -90,64 +89,4 @@ public static function getTestCase() : TestCase
{
return new TestCase();
}

/**
* @return iterable<int>
*/
public function yieldSomething() : iterable
{
if (self::VERSION === 0) {
yield 0;
}

yield 1;
}

/**
* @return iterable<int>
*/
public function yieldFromSomething() : iterable
{
if (self::VERSION === 0) {
yield 0;
}

yield from [];
}

public function throwWhenInvalid() : void
{
if (self::VERSION === 0) {
return;
}

throw new InvalidArgumentException();
}

public function trySwitchSpace() : void
{
try {
$var = 1;
switch (self::VERSION) {
default:
}

foreach ([] as $item) {
echo $item;
}

while (true) {
echo 2;
}

if (true) {
echo 3;
}

echo 1;
} catch (Throwable $throwable) {
}

echo 2;
}
}
105 changes: 105 additions & 0 deletions tests/input/ControlStructures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace ControlStructures;

use InvalidArgumentException;
use Throwable;
use const PHP_VERSION;

class ControlStructures
{
private const VERSION = PHP_VERSION;

/**
* @return iterable<int>
*/
public function varAndIfNoSpaceBetween() : iterable
{
$var = 1;
if (self::VERSION === 0) {
yield 0;
}
}

/**
* @return iterable<int>
*/
public function ifAndYieldSpaceBetween() : iterable
{
if (self::VERSION === 0) {
yield 0;
}
yield 1;
}

/**
* @return iterable<int>
*/
public function ifAndYieldFromSpaceBetween() : iterable
{
if (self::VERSION === 0) {
yield 0;
}
yield from [];
}

public function ifAndThrowSpaceBetween() : void
{
if (self::VERSION === 0) {
return;
}
throw new InvalidArgumentException();
}

public function ifAndReturnSpaceBetween() : int
{
if (self::VERSION === 0) {
return 0;
}

return 1;
}

public function noSpaceAroundCase() : void
{
switch (self::VERSION) {
case 1:
case 2:
// do something
break;
case 3:
// do something else
break;
default:
}
}

public function spaceBelowBlocks() : void
{
if (true) {
echo 1;
}
do {
echo 2;
} while (true);
while (true) {
echo 3;
}
for ($i = 0; $i < 1; $i++) {
echo $i;
}
foreach ([] as $item) {
echo $item;
}
switch (true) {
default:
}
try {
echo 4;
} catch (Throwable $throwable) {
}
echo 5;
}
}
52 changes: 0 additions & 52 deletions tests/input/example-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,56 +89,4 @@ public static function getTestCase() : TestCase
return new TestCase();
}

/**
* @return iterable<int>
*/
public function yieldSomething() : iterable
{
if (self::VERSION === 0) {
yield 0;
}
yield 1;
}

/**
* @return iterable<int>
*/
public function yieldFromSomething() : iterable
{
if (self::VERSION === 0) {
yield 0;
}
yield from [];
}

public function throwWhenInvalid() : void
{
if (self::VERSION === 0) {
return;
}
throw new \InvalidArgumentException();
}

public function trySwitchSpace() : void
{
try {
$var = 1;
switch (self::VERSION) {
default:
}
foreach ([] as $item) {
echo $item;
}
while (true) {
echo 2;
}
if (true) {
echo 3;
}
echo 1;
} catch (Throwable $throwable) {
}
echo 2;
}

}

0 comments on commit d8a60ec

Please sign in to comment.