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

New Rule: Ensure there is no redundant parenthesis on attribute #1742

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
25 changes: 23 additions & 2 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* [no_admin_yaml](#no_admin_yaml)
* [no_app_bundle](#no_app_bundle)
* [no_app_console](#no_app_console)
* [no_attribute_redundant_parenthesis](#no_attribute_redundant_parenthesis)
* [no_bash_prompt](#no_bash_prompt)
* [no_blank_line_after_filepath_in_code_block](#no_blank_line_after_filepath_in_code_block)
* [no_blank_line_after_filepath_in_php_code_block](#no_blank_line_after_filepath_in_php_code_block)
Expand Down Expand Up @@ -132,8 +133,6 @@ This is a nice behaviour...

> _Make sure argument variable name match for type_

#### Groups [`@Symfony`]

#### Configuration options

Name | Required | Allowed Types | Default
Expand Down Expand Up @@ -618,6 +617,28 @@ composer require --dev symfony/var-dumper

#### Groups [`@Sonata`, `@Symfony`]

## `no_attribute_redundant_parenthesis`

> _Make sure there is no redundant parenthesis on attribute_

#### Groups [`@Symfony`]

##### Valid Examples :+1:

```rst
#[Bar]
```

```rst
#[Bar('foo')]
```

##### Invalid Examples :-1:

```rst
#[Bar()]
```

## `no_bash_prompt`

> _Ensure no bash prompt `$` is used before commands in `bash`, `shell` or `terminal` code blocks._
Expand Down
52 changes: 52 additions & 0 deletions src/Rule/NoAttributeRedundantParenthesis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* This file is part of DOCtor-RST.
*
* (c) Oskar Stark <oskarstark@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Rule;

use App\Attribute\Rule\Description;
use App\Attribute\Rule\InvalidExample;
use App\Attribute\Rule\ValidExample;
use App\Value\Lines;
use App\Value\NullViolation;
use App\Value\RuleGroup;
use App\Value\Violation;
use App\Value\ViolationInterface;

#[Description('Make sure there is no redundant parenthesis on attribute')]
#[InvalidExample('#[Bar()]')]
#[ValidExample('#[Bar]')]
#[ValidExample('#[Bar(\'foo\')]')]
class NoAttributeRedundantParenthesis extends AbstractRule implements LineContentRule
{
public static function getGroups(): array
{
return [RuleGroup::Symfony()];
}

public function check(Lines $lines, int $number, string $filename): ViolationInterface
{
$lines->seek($number);
$line = $lines->current();

if ($line->clean()->match('/#\[.+\(\)\]/')) {
return Violation::from(
'Please remove redundant parenthesis on attribute',
$filename,
$number + 1,
$line,
);
}

return NullViolation::create();
}
}
69 changes: 69 additions & 0 deletions tests/Rule/NoAttributeRedundantParenthesisTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/**
* This file is part of DOCtor-RST.
*
* (c) Oskar Stark <oskarstark@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Tests\Rule;

use App\Rule\NoAttributeRedundantParenthesis;
use App\Tests\RstSample;
use App\Tests\UnitTestCase;
use App\Value\NullViolation;
use App\Value\Violation;
use App\Value\ViolationInterface;

final class NoAttributeRedundantParenthesisTest extends UnitTestCase
{
/**
* @test
*
* @dataProvider checkProvider
*/
public function check(ViolationInterface $expected, RstSample $sample): void
{
self::assertEquals(
$expected,
(new NoAttributeRedundantParenthesis())->check($sample->lines, $sample->lineNumber, 'filename'),
);
}

public static function checkProvider(): iterable
{
yield from [
[
Violation::from(
"Please remove redundant parenthesis on attribute",
'filename',
1,
'#[Bar()]',
),
new RstSample('#[Bar()]'),
],
[
Violation::from(
"Please remove redundant parenthesis on attribute",
'filename',
1,
'Attribute #[Bar()] in my text',
),
new RstSample('Attribute #[Bar()] in my text'),
],
[
NullViolation::create(),
new RstSample('#[Bar]'),
],
[
NullViolation::create(),
new RstSample("#[Bar('foo')]"),
],
];
}
}
Loading