Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra test scenarios #208

Draft
wants to merge 6 commits into
base: 8.2.x
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions tests/fixed/UselessConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function uselessIfCondition(): bool
return $bar === 'bar';
}

public function uselessIfConditionWithComment(): bool
{
return $bar === 'bar';
}

public function uselessNegativeCondition(): bool
{
return $foo === 'foo';
Expand Down
2 changes: 1 addition & 1 deletion tests/fixed/array_indentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

$array = [
0,
0, // this is the first index
1,
2,
3,
Expand Down
37 changes: 36 additions & 1 deletion tests/fixed/doc-comment-spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function c(iterable $foo): void
* @deprecated
*
* @link https://example.com
* @see other
* @see Something else and make this see annotation
* multiline
* @uses other
*
* @ORM\Id
Expand All @@ -67,4 +68,38 @@ public function c(iterable $foo): void
public function d(iterable $foo, iterable $bar): iterable
{
}

/**
* Description
* More Description
*
* @internal
* @deprecated
*
* @link https://example.com
* @see other
* @uses other
*
* @ORM\Id
* @ORM\Column
* @ODM\Id
* @ODM\Column
* @PHPCR\Uuid
* @PHPCR\Field
*
* @param int[] $foo
* @param int[] $bar
*
* @return int[]
*
* @throws FooException
* @throws BarException
*
* @phpstan-param string $foo
* @psalm-param string $foo
* @phpstan-return true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to handle these?

*/
public function f(iterable $foo, iterable $bar): iterable
{
}
}
3 changes: 3 additions & 0 deletions tests/fixed/forbidden-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Test;

use function array_map;
use function chop;
use function compact;
use function extract;
Expand All @@ -28,3 +29,5 @@
extract($bar);

compact('foo', 'bar');

array_map('is_null', ['1', '2', null]); // forbidden function will not be detected
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI codesniffer can not detect these.. is this fine?

1 change: 1 addition & 0 deletions tests/fixed/optimized-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

$args = [123, [123], true];
in_array(...$args);
in_array(...$args);
3 changes: 3 additions & 0 deletions tests/fixed/semicolon_spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
$qb->select()
->from()
->where();

$qb->select()
->from('mytable'); // we select from my table
4 changes: 4 additions & 0 deletions tests/fixed/superfluous-naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class FooException extends RuntimeException
{
}

class FooError extends Error
{
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is superfluous but not reported.. i guess there is no existing rule for this

interface FooInterface
{
}
Expand Down
5 changes: 5 additions & 0 deletions tests/fixed/trailing_comma_on_array.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
'key1' => 'value',
'key2' => 'value',
];

$arrayWithComment = [
'key1' => 'value',
'key2' => 'value', // comment
];
14 changes: 14 additions & 0 deletions tests/fixed/useless-semicolon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@
for (;;) {
echo 'To infity and beyond';
}

for (
$i = 0; $i < 10;
$i++
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
echo 'This will not be executed inside the for-loop';
}

{
$var = 'This is useless';
};

$myvar = 3;
11 changes: 11 additions & 0 deletions tests/input/UselessConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public function uselessIfCondition(): bool
return false;
}

public function uselessIfConditionWithComment(): bool
{
if ($bar === 'bar') {
// Return true here in case $bar is bar
return true;
}

// Return true here in case $bar is bar
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false;
}

public function uselessNegativeCondition(): bool
{
if ($foo !== 'foo') {
Expand Down
2 changes: 1 addition & 1 deletion tests/input/array_indentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

$array = [
0,
0, // this is the first index
1,
2,
3,
Expand Down
32 changes: 31 additions & 1 deletion tests/input/doc-comment-spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,40 @@ public function c(iterable $foo): void
* @PHPCR\Field
* @ODM\Column
* @ORM\Column
* @see other
* @see Something else and make this see annotation
* multiline
*
*/
public function d(iterable $foo, iterable $bar): iterable
{
}

/**
*
* Description
* More Description
* @phpstan-param string $foo
* @psalm-param string $foo
* @phpstan-return true
* @throws FooException
* @param int[] $foo
* @uses other
* @throws BarException
* @return int[]
* @ORM\Id
* @internal
* @link https://example.com
* @ODM\Id
* @deprecated
* @PHPCR\Uuid
* @param int[] $bar
* @PHPCR\Field
* @ODM\Column
* @ORM\Column
* @see other
*
*/
public function f(iterable $foo, iterable $bar): iterable
{
}
}
1 change: 1 addition & 0 deletions tests/input/example-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Example implements \IteratorAggregate
{
private const VERSION = \PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI currently there is no sniff included that fixes the extra lines inside a class.. it does fix the extra lines between functions but it does not look inside the whole class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SlevomatCodingStandard.Classes.ClassMemberSpacing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.. will create a new PR with that..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also:

  • MethodSpacing
  • PropertySpacing
  • ConstantSpacing

/** @var null|int */
private $foo;

Expand Down
2 changes: 2 additions & 0 deletions tests/input/forbidden-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
extract($bar);

compact('foo', 'bar');

array_map('is_null', ['1', '2', null]); // forbidden function will not be detected
1 change: 1 addition & 0 deletions tests/input/optimized-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

$args = [123, [123], true];
in_array(...$args);
\in_array(...$args);
4 changes: 4 additions & 0 deletions tests/input/semicolon_spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
->from()
->where()
;

$qb->select()
->from('mytable') // we select from my table
;
4 changes: 4 additions & 0 deletions tests/input/superfluous-naming.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class FooException extends RuntimeException
{
}

class FooError extends Error
{
}

interface FooInterface
{
}
Expand Down
5 changes: 5 additions & 0 deletions tests/input/trailing_comma_on_array.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
'key1' => 'value',
'key2' => 'value'
];

$arrayWithComment = [
'key1' => 'value',
'key2' => 'value' // comment
];
13 changes: 12 additions & 1 deletion tests/input/useless-semicolon.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@
};

do {
echo 1;
;echo 1;
} while (! false);

for (;;) {
echo 'To infity and beyond';
};

for ($i = 0 ; $i < 10; $i++);
{
echo 'This will not be executed inside the for-loop';
}

{
$var = 'This is useless';
};

$myvar = 3;;