From ee60899be87c0523a00b4349e9340fe1c8f1fd27 Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sat, 20 Jun 2020 23:52:59 +0300 Subject: [PATCH 1/6] Extra test scenarios --- tests/fixed/array_indentation.php | 2 +- tests/fixed/doc-comment-spacing.php | 37 ++++++++++++++++++++++++- tests/fixed/forbidden-functions.php | 3 ++ tests/fixed/optimized-functions.php | 1 + tests/fixed/semicolon_spacing.php | 3 ++ tests/fixed/superfluous-naming.php | 4 +++ tests/fixed/trailing_comma_on_array.php | 5 ++++ tests/fixed/useless-semicolon.php | 7 +++++ tests/input/UselessConditions.php | 11 ++++++++ tests/input/array_indentation.php | 2 +- tests/input/doc-comment-spacing.php | 32 ++++++++++++++++++++- tests/input/forbidden-functions.php | 2 ++ tests/input/optimized-functions.php | 1 + tests/input/semicolon_spacing.php | 4 +++ tests/input/superfluous-naming.php | 4 +++ tests/input/trailing_comma_on_array.php | 5 ++++ tests/input/useless-semicolon.php | 7 +++++ 17 files changed, 126 insertions(+), 4 deletions(-) diff --git a/tests/fixed/array_indentation.php b/tests/fixed/array_indentation.php index 626a7c37..d3f62c3b 100644 --- a/tests/fixed/array_indentation.php +++ b/tests/fixed/array_indentation.php @@ -3,7 +3,7 @@ declare(strict_types=1); $array = [ - 0, + 0, // this is the first index 1, 2, 3, diff --git a/tests/fixed/doc-comment-spacing.php b/tests/fixed/doc-comment-spacing.php index ad1dee4a..3cabe63f 100644 --- a/tests/fixed/doc-comment-spacing.php +++ b/tests/fixed/doc-comment-spacing.php @@ -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 @@ -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 + */ + public function f(iterable $foo, iterable $bar): iterable + { + } } diff --git a/tests/fixed/forbidden-functions.php b/tests/fixed/forbidden-functions.php index f951ca36..c92daf3f 100644 --- a/tests/fixed/forbidden-functions.php +++ b/tests/fixed/forbidden-functions.php @@ -4,6 +4,7 @@ namespace Test; +use function array_map; use function chop; use function compact; use function extract; @@ -28,3 +29,5 @@ extract($bar); compact('foo', 'bar'); + +array_map('is_null', ['1', '2', null]); // forbidden function will not be detected diff --git a/tests/fixed/optimized-functions.php b/tests/fixed/optimized-functions.php index 0e69dbc5..c3d0174b 100644 --- a/tests/fixed/optimized-functions.php +++ b/tests/fixed/optimized-functions.php @@ -4,3 +4,4 @@ $args = [123, [123], true]; in_array(...$args); +in_array(...$args); diff --git a/tests/fixed/semicolon_spacing.php b/tests/fixed/semicolon_spacing.php index fa6c43ab..31f1dacb 100644 --- a/tests/fixed/semicolon_spacing.php +++ b/tests/fixed/semicolon_spacing.php @@ -9,3 +9,6 @@ $qb->select() ->from() ->where(); + +$qb->select() + ->from('mytable'); // we select from my table diff --git a/tests/fixed/superfluous-naming.php b/tests/fixed/superfluous-naming.php index cab66237..1e4612f9 100644 --- a/tests/fixed/superfluous-naming.php +++ b/tests/fixed/superfluous-naming.php @@ -12,6 +12,10 @@ class FooException extends RuntimeException { } +class FooError extends \Error +{ +} + interface FooInterface { } diff --git a/tests/fixed/trailing_comma_on_array.php b/tests/fixed/trailing_comma_on_array.php index b2eba5b8..5f258930 100644 --- a/tests/fixed/trailing_comma_on_array.php +++ b/tests/fixed/trailing_comma_on_array.php @@ -6,3 +6,8 @@ 'key1' => 'value', 'key2' => 'value', ]; + +$arrayWithComment = [ + 'key1' => 'value', + 'key2' => 'value', // comment +]; diff --git a/tests/fixed/useless-semicolon.php b/tests/fixed/useless-semicolon.php index 358277bd..050c334c 100644 --- a/tests/fixed/useless-semicolon.php +++ b/tests/fixed/useless-semicolon.php @@ -13,3 +13,10 @@ for (;;) { echo 'To infity and beyond'; } + +for ($i = 0 ; $i < 10; $i++); +{ + echo 'This will not be executed inside the for-loop'; +} + +$myvar = 3;; \ No newline at end of file diff --git a/tests/input/UselessConditions.php b/tests/input/UselessConditions.php index bcb94341..564e23f7 100644 --- a/tests/input/UselessConditions.php +++ b/tests/input/UselessConditions.php @@ -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 + return false; + } + public function uselessNegativeCondition(): bool { if ($foo !== 'foo') { diff --git a/tests/input/array_indentation.php b/tests/input/array_indentation.php index fa6e10ab..df373b32 100644 --- a/tests/input/array_indentation.php +++ b/tests/input/array_indentation.php @@ -3,7 +3,7 @@ declare(strict_types=1); $array = [ -0, +0, // this is the first index 1, 2, 3, diff --git a/tests/input/doc-comment-spacing.php b/tests/input/doc-comment-spacing.php index 7b1df121..016bca4b 100644 --- a/tests/input/doc-comment-spacing.php +++ b/tests/input/doc-comment-spacing.php @@ -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 + { + } } diff --git a/tests/input/forbidden-functions.php b/tests/input/forbidden-functions.php index f951ca36..5c06a377 100644 --- a/tests/input/forbidden-functions.php +++ b/tests/input/forbidden-functions.php @@ -28,3 +28,5 @@ extract($bar); compact('foo', 'bar'); + +array_map('is_null', ['1', '2', null]); // forbidden function will not be detected diff --git a/tests/input/optimized-functions.php b/tests/input/optimized-functions.php index 0e69dbc5..2b0ab10a 100644 --- a/tests/input/optimized-functions.php +++ b/tests/input/optimized-functions.php @@ -4,3 +4,4 @@ $args = [123, [123], true]; in_array(...$args); +\in_array(...$args); diff --git a/tests/input/semicolon_spacing.php b/tests/input/semicolon_spacing.php index 169f8006..712351c6 100644 --- a/tests/input/semicolon_spacing.php +++ b/tests/input/semicolon_spacing.php @@ -11,3 +11,7 @@ ->from() ->where() ; + +$qb->select() + ->from('mytable') // we select from my table +; \ No newline at end of file diff --git a/tests/input/superfluous-naming.php b/tests/input/superfluous-naming.php index cab66237..1e4612f9 100644 --- a/tests/input/superfluous-naming.php +++ b/tests/input/superfluous-naming.php @@ -12,6 +12,10 @@ class FooException extends RuntimeException { } +class FooError extends \Error +{ +} + interface FooInterface { } diff --git a/tests/input/trailing_comma_on_array.php b/tests/input/trailing_comma_on_array.php index 3b39e357..431e2551 100644 --- a/tests/input/trailing_comma_on_array.php +++ b/tests/input/trailing_comma_on_array.php @@ -6,3 +6,8 @@ 'key1' => 'value', 'key2' => 'value' ]; + +$arrayWithComment = [ + 'key1' => 'value', + 'key2' => 'value' // comment +]; diff --git a/tests/input/useless-semicolon.php b/tests/input/useless-semicolon.php index 66149f36..81dddbe3 100644 --- a/tests/input/useless-semicolon.php +++ b/tests/input/useless-semicolon.php @@ -13,3 +13,10 @@ for (;;) { echo 'To infity and beyond'; }; + +for ($i = 0 ; $i < 10; $i++); +{ + echo 'This will not be executed inside the for-loop'; +}; + +$myvar = 3;; \ No newline at end of file From 20a05676ab2ed41907b3da467f4fa4f4039ca81a Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sun, 21 Jun 2020 00:04:46 +0300 Subject: [PATCH 2/6] Fix superfluous and useless --- tests/fixed/superfluous-naming.php | 2 +- tests/input/superfluous-naming.php | 2 +- tests/input/useless-semicolon.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/fixed/superfluous-naming.php b/tests/fixed/superfluous-naming.php index 1e4612f9..0105b602 100644 --- a/tests/fixed/superfluous-naming.php +++ b/tests/fixed/superfluous-naming.php @@ -12,7 +12,7 @@ class FooException extends RuntimeException { } -class FooError extends \Error +class FooError extends Error { } diff --git a/tests/input/superfluous-naming.php b/tests/input/superfluous-naming.php index 1e4612f9..0105b602 100644 --- a/tests/input/superfluous-naming.php +++ b/tests/input/superfluous-naming.php @@ -12,7 +12,7 @@ class FooException extends RuntimeException { } -class FooError extends \Error +class FooError extends Error { } diff --git a/tests/input/useless-semicolon.php b/tests/input/useless-semicolon.php index 81dddbe3..05dc46a1 100644 --- a/tests/input/useless-semicolon.php +++ b/tests/input/useless-semicolon.php @@ -14,9 +14,9 @@ echo 'To infity and beyond'; }; -for ($i = 0 ; $i < 10; $i++); +for ($i = 0 ; $i < 10; $i++) { echo 'This will not be executed inside the for-loop'; -}; +} -$myvar = 3;; \ No newline at end of file +$myvar = 3;; From 9f66c9f7c44637c4a44f62dbfffc6825158fd323 Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sun, 21 Jun 2020 00:09:45 +0300 Subject: [PATCH 3/6] add the semicolumn back --- tests/input/useless-semicolon.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/input/useless-semicolon.php b/tests/input/useless-semicolon.php index 05dc46a1..917d5519 100644 --- a/tests/input/useless-semicolon.php +++ b/tests/input/useless-semicolon.php @@ -14,7 +14,7 @@ echo 'To infity and beyond'; }; -for ($i = 0 ; $i < 10; $i++) +for ($i = 0 ; $i < 10; $i++); { echo 'This will not be executed inside the for-loop'; } From 768a7776e7e1c38367f811c37c55842b8d8429ae Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sun, 21 Jun 2020 13:09:06 +0300 Subject: [PATCH 4/6] Added more cases --- tests/fixed/UselessConditions.php | 5 +++++ tests/fixed/useless-semicolon.php | 7 +++++-- tests/input/useless-semicolon.php | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/fixed/UselessConditions.php b/tests/fixed/UselessConditions.php index 95e2012a..cceb555e 100644 --- a/tests/fixed/UselessConditions.php +++ b/tests/fixed/UselessConditions.php @@ -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'; diff --git a/tests/fixed/useless-semicolon.php b/tests/fixed/useless-semicolon.php index 050c334c..6220ec3f 100644 --- a/tests/fixed/useless-semicolon.php +++ b/tests/fixed/useless-semicolon.php @@ -14,9 +14,12 @@ echo 'To infity and beyond'; } -for ($i = 0 ; $i < 10; $i++); +for ( + $i = 0; $i < 10; + $i++ +); { echo 'This will not be executed inside the for-loop'; } -$myvar = 3;; \ No newline at end of file +$myvar = 3; diff --git a/tests/input/useless-semicolon.php b/tests/input/useless-semicolon.php index 917d5519..0a681916 100644 --- a/tests/input/useless-semicolon.php +++ b/tests/input/useless-semicolon.php @@ -7,7 +7,7 @@ }; do { - echo 1; + ;echo 1; } while (! false); for (;;) { @@ -19,4 +19,8 @@ echo 'This will not be executed inside the for-loop'; } +{ + $var = 'This is useless'; +}; + $myvar = 3;; From 11c71d8fb573c48762177061675e9bbfd514597a Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sun, 21 Jun 2020 13:12:33 +0300 Subject: [PATCH 5/6] Fix more cases --- tests/fixed/doc-comment-spacing.php | 4 ++-- tests/fixed/useless-semicolon.php | 4 ++++ tests/input/doc-comment-spacing.php | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/fixed/doc-comment-spacing.php b/tests/fixed/doc-comment-spacing.php index 3cabe63f..b115eded 100644 --- a/tests/fixed/doc-comment-spacing.php +++ b/tests/fixed/doc-comment-spacing.php @@ -46,8 +46,8 @@ public function c(iterable $foo): void * @deprecated * * @link https://example.com - * @see Something else and make this see annotation - * multiline + * @see Something else and make this see annotation + * multiline * @uses other * * @ORM\Id diff --git a/tests/fixed/useless-semicolon.php b/tests/fixed/useless-semicolon.php index 6220ec3f..710682c6 100644 --- a/tests/fixed/useless-semicolon.php +++ b/tests/fixed/useless-semicolon.php @@ -22,4 +22,8 @@ echo 'This will not be executed inside the for-loop'; } +{ + $var = 'This is useless'; +}; + $myvar = 3; diff --git a/tests/input/doc-comment-spacing.php b/tests/input/doc-comment-spacing.php index 016bca4b..24f1c3b8 100644 --- a/tests/input/doc-comment-spacing.php +++ b/tests/input/doc-comment-spacing.php @@ -57,8 +57,8 @@ public function c(iterable $foo): void * @PHPCR\Field * @ODM\Column * @ORM\Column - * @see Something else and make this see annotation - * multiline + * @see Something else and make this see annotation + * multiline * */ public function d(iterable $foo, iterable $bar): iterable From 3d5a35d5cc33b2bcd6f899fa68bf01c50607faeb Mon Sep 17 00:00:00 2001 From: Mponos George Date: Mon, 22 Jun 2020 17:53:24 +0300 Subject: [PATCH 6/6] Add extra line that should've been fixed --- tests/input/example-class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/input/example-class.php b/tests/input/example-class.php index 8e48cbb8..e6728ab0 100644 --- a/tests/input/example-class.php +++ b/tests/input/example-class.php @@ -23,6 +23,7 @@ class Example implements \IteratorAggregate { private const VERSION = \PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION; + /** @var null|int */ private $foo;