diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml new file mode 100644 index 00000000..adacd735 --- /dev/null +++ b/.github/workflows/rector.yml @@ -0,0 +1,21 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'psalm.xml' + +name: rector + +jobs: + rector: + uses: yiisoft/actions/.github/workflows/rector.yml@master + with: + os: >- + ['ubuntu-latest'] + php: >- + ['8.0'] diff --git a/composer.json b/composer.json index ac1d9ccc..e4f4250c 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", + "rector/rector": "^0.15.7", "roave/infection-static-analysis-plugin": "^1.18", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^4.23", diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..f55daaed --- /dev/null +++ b/rector.php @@ -0,0 +1,27 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80, + ]); + + $rectorConfig->skip([ + ClosureToArrowFunctionRector::class, + ]); +}; diff --git a/src/Field.php b/src/Field.php index 01c9fcac..3361153a 100644 --- a/src/Field.php +++ b/src/Field.php @@ -252,7 +252,7 @@ public static function field(string $class, array $config = []): object public static function getFactory(?string $name = null): FieldFactory { - $name = $name ?? self::$defaultConfigName; + $name ??= self::$defaultConfigName; if (!array_key_exists($name, self::$factories)) { if (!array_key_exists($name, self::$configs)) { diff --git a/src/Field/Checkbox.php b/src/Field/Checkbox.php index b65c9835..067977f4 100644 --- a/src/Field/Checkbox.php +++ b/src/Field/Checkbox.php @@ -98,8 +98,6 @@ public function enclosedByLabel(bool $value): self * * @param string|null $value * - * @return self - * * @link https://www.w3.org/TR/html52/sec-forms.html#the-label-element */ public function inputLabel(?string $value): self @@ -235,9 +233,9 @@ protected function generateInput(): string $inputAttributes = $this->getInputAttributes(); $inputValue = $this->inputValue; - $inputValue = $inputValue ?? $this->prepareValue($inputAttributes['value'] ?? null); + $inputValue ??= $this->prepareValue($inputAttributes['value'] ?? null); unset($inputAttributes['value']); - $inputValue = $inputValue ?? '1'; + $inputValue ??= '1'; $checkbox = Html::checkbox($this->getInputName(), $inputValue, $inputAttributes); diff --git a/src/Field/CheckboxList.php b/src/Field/CheckboxList.php index 1a53b640..9f452e95 100644 --- a/src/Field/CheckboxList.php +++ b/src/Field/CheckboxList.php @@ -133,8 +133,6 @@ public function separator(string $separator): self } /** - * @param Closure|null $formatter - * * @psalm-param Closure(CheckboxItem):string|null $formatter */ public function itemFormatter(?Closure $formatter): self diff --git a/src/Field/ErrorSummary.php b/src/Field/ErrorSummary.php index 349757b0..eaaa58d0 100644 --- a/src/Field/ErrorSummary.php +++ b/src/Field/ErrorSummary.php @@ -67,10 +67,6 @@ public function onlyAttributes(string ...$names): self /** * Set the footer text for the error summary - * - * @param string $value - * - * @return self */ public function footer(string $value): self { @@ -84,8 +80,6 @@ public function footer(string $value): self * * @param array $values Attribute values indexed by attribute names. * - * @return self - * * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. */ public function footerAttributes(array $values): self @@ -97,10 +91,6 @@ public function footerAttributes(array $values): self /** * Set the header text for the error summary - * - * @param string $value - * - * @return self */ public function header(string $value): self { @@ -114,8 +104,6 @@ public function header(string $value): self * * @param array $values Attribute values indexed by attribute names. * - * @return self - * * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. */ public function headerAttributes(array $values): self diff --git a/src/Field/Part/Error.php b/src/Field/Part/Error.php index 1137d569..647917c8 100644 --- a/src/Field/Part/Error.php +++ b/src/Field/Part/Error.php @@ -106,8 +106,6 @@ public function class(?string ...$class): self /** * Whether content should be HTML-encoded. - * - * @param bool $value */ public function encode(bool $value): self { @@ -128,8 +126,6 @@ public function message(?string $value): self /** * Callback that will be called to obtain an error message. - * - * @param callable|null $value */ public function messageCallback(?callable $value): self { diff --git a/src/Field/Part/Hint.php b/src/Field/Part/Hint.php index 4f75a2fa..68fdcf55 100644 --- a/src/Field/Part/Hint.php +++ b/src/Field/Part/Hint.php @@ -105,8 +105,6 @@ public function content(string|Stringable|null $content): self /** * Whether content should be HTML-encoded. - * - * @param bool $value */ public function encode(bool $value): self { diff --git a/src/Field/RadioList.php b/src/Field/RadioList.php index 324ed9d8..8dba28ff 100644 --- a/src/Field/RadioList.php +++ b/src/Field/RadioList.php @@ -131,8 +131,6 @@ public function separator(string $separator): self } /** - * @param Closure|null $formatter - * * @psalm-param Closure(RadioItem):string|null $formatter */ public function itemFormatter(?Closure $formatter): self diff --git a/src/Field/Select.php b/src/Field/Select.php index 25d297e8..4244c72e 100644 --- a/src/Field/Select.php +++ b/src/Field/Select.php @@ -86,8 +86,6 @@ public function options(Option ...$options): self * @param array[] $groupsAttributes Array of group attribute sets indexed by group labels from {@see $data}. * * @psalm-param array> $data - * - * @return self */ public function optionsData( array $data, diff --git a/src/FormErrors.php b/src/FormErrors.php index 03048dc0..3cdfb310 100644 --- a/src/FormErrors.php +++ b/src/FormErrors.php @@ -14,13 +14,12 @@ */ final class FormErrors implements FormErrorsInterface { - /** @psalm-var array> */ - private array $attributesErrors; - - public function __construct(array $attributesErrors = []) - { - /** @psalm-var array> */ - $this->attributesErrors = $attributesErrors; + /** + * @psalm-param array> $attributesErrors + */ + public function __construct( + private array $attributesErrors = [], + ) { } public function addError(string $attribute, string $error): void diff --git a/src/Helper/HtmlForm.php b/src/Helper/HtmlForm.php index 7bc1eb45..93622f3c 100644 --- a/src/Helper/HtmlForm.php +++ b/src/Helper/HtmlForm.php @@ -18,8 +18,6 @@ final class HtmlForm * * @param FormModelInterface $formModel the form object. * @param string $attribute the attribute name or expression. - * - * @return string */ public static function getAttributeHint(FormModelInterface $formModel, string $attribute): string { @@ -33,8 +31,6 @@ public static function getAttributeHint(FormModelInterface $formModel, string $a * @param string $attribute the attribute name or expression. * * @throws InvalidArgumentException if the attribute name contains non-word characters. - * - * @return string */ public static function getAttributeLabel(FormModelInterface $formModel, string $attribute): string { @@ -72,8 +68,6 @@ public static function getAttributeName(FormModelInterface $formModel, string $a * @param string $attribute the attribute name or expression. * * @throws InvalidArgumentException if the attribute name contains non-word characters. - * - * @return string */ public static function getAttributePlaceHolder(FormModelInterface $formModel, string $attribute): string { diff --git a/tests/Field/CheckboxListTest.php b/tests/Field/CheckboxListTest.php index bb24b339..c75aa0ce 100644 --- a/tests/Field/CheckboxListTest.php +++ b/tests/Field/CheckboxListTest.php @@ -321,9 +321,7 @@ public function testItemFormatter(): void 'blue' => 'Blue', ]) ->formAttribute(new CheckboxListForm(), 'color') - ->itemFormatter(static function (CheckboxItem $item) { - return Html::checkbox($item->name, $item->value) . ' — ' . $item->label; - }) + ->itemFormatter(static fn (CheckboxItem $item) => Html::checkbox($item->name, $item->value) . ' — ' . $item->label) ->render(); $expected = <<formAttribute($this->createValidatedErrorForm(), 'name') ->messageCallback( - static function (string $message, ?ErrorForm $form, ?string $attribute): string { - return 'Attribute "' . $attribute . '" error: ' . $message; - } + static fn (string $message, ?ErrorForm $form, ?string $attribute): string => 'Attribute "' . $attribute . '" error: ' . $message ) ->render(); @@ -246,9 +244,7 @@ public function testMessageCallbackWithCustomMessage(): void ->formAttribute($this->createValidatedErrorForm(), 'name') ->message('Invalid value.') ->messageCallback( - static function (string $message, ?ErrorForm $form, ?string $attribute): string { - return 'Attribute "' . $attribute . '" error: ' . $message; - } + static fn (string $message, ?ErrorForm $form, ?string $attribute): string => 'Attribute "' . $attribute . '" error: ' . $message ) ->render(); @@ -261,9 +257,7 @@ public function testMessageCallbackWithMessageAndWithoutError(): void ->formAttribute($this->createValidatedErrorForm(), 'age') ->message('Invalid value.') ->messageCallback( - static function (string $message, ?ErrorForm $form, ?string $attribute): string { - return 'Attribute "' . $attribute . '" error: ' . $message; - } + static fn (string $message, ?ErrorForm $form, ?string $attribute): string => 'Attribute "' . $attribute . '" error: ' . $message ) ->render(); diff --git a/tests/Field/RadioListTest.php b/tests/Field/RadioListTest.php index cc8de6d2..7a3c7e97 100644 --- a/tests/Field/RadioListTest.php +++ b/tests/Field/RadioListTest.php @@ -455,15 +455,13 @@ public function testItemFormatter(): void ->hideLabel() ->items([1 => 'One', 2 => 'Two']) ->itemFormatter( - static function (RadioItem $item): string { - return '
' . - $item->index . ') ' . - Html::radio($item->radioAttributes['name'], $item->radioAttributes['value']) - ->attributes($item->radioAttributes) - ->checked($item->checked) - ->label($item->label) . - '
'; - } + static fn (RadioItem $item): string => '
' . + $item->index . ') ' . + Html::radio($item->radioAttributes['name'], $item->radioAttributes['value']) + ->attributes($item->radioAttributes) + ->checked($item->checked) + ->label($item->label) . + '
' ) ->render(); diff --git a/tests/Field/RangeTest.php b/tests/Field/RangeTest.php index 2f6e84a3..5ee6b77b 100644 --- a/tests/Field/RangeTest.php +++ b/tests/Field/RangeTest.php @@ -47,13 +47,13 @@ public function testAddOutputAttributes(): void ->addOutputAttributes(['id' => 'UID']) ->render(); - $expected = << - - - 23 - - HTML; + $expected = << + + +23 + +HTML_WRAP; $this->assertSame($expected, $result); } @@ -67,13 +67,13 @@ public function testOutputAttributes(): void ->outputAttributes(['id' => 'UID']) ->render(); - $expected = << - - - 23 - - HTML; + $expected = << + + +23 + +HTML_WRAP; $this->assertSame($expected, $result); } @@ -88,13 +88,13 @@ public function testWithOutput(): void ->outputAttributes(['id' => 'UID']) ->render(); - $expected = << - - - 23 - - HTML; + $expected = << + + +23 + +HTML_WRAP; $this->assertSame($expected, $result); } @@ -108,13 +108,13 @@ public function testCustomOutputTag(): void ->outputAttributes(['id' => 'UID']) ->render(); - $expected = << - - -
23
- - HTML; + $expected = << + + +
23
+ +HTML_WRAP; $this->assertSame($expected, $result); } diff --git a/tests/Helper/HtmlFormTest.php b/tests/Helper/HtmlFormTest.php index e1a2df65..4d0a6871 100644 --- a/tests/Helper/HtmlFormTest.php +++ b/tests/Helper/HtmlFormTest.php @@ -84,10 +84,6 @@ public function dataGetInputName(): array /** * @dataProvider dataGetInputName - * - * @param FormModelInterface $form - * @param string $attribute - * @param string $expected */ public function testGetInputName(FormModelInterface $form, string $attribute, string $expected): void { diff --git a/tests/Support/StringableObject.php b/tests/Support/StringableObject.php index 8dfc0230..331a17cf 100644 --- a/tests/Support/StringableObject.php +++ b/tests/Support/StringableObject.php @@ -4,13 +4,13 @@ namespace Yiisoft\Form\Tests\Support; -final class StringableObject -{ - private string $string; +use Stringable; - public function __construct(string $string) - { - $this->string = $string; +final class StringableObject implements Stringable +{ + public function __construct( + private string $string + ) { } public function __toString(): string diff --git a/tests/TestSupport/CustomFormErrors.php b/tests/TestSupport/CustomFormErrors.php index 56975b76..db764d68 100644 --- a/tests/TestSupport/CustomFormErrors.php +++ b/tests/TestSupport/CustomFormErrors.php @@ -8,13 +8,9 @@ final class CustomFormErrors implements FormErrorsInterface { - /** @psalm-var array> */ - private array $attributesErrors; - - public function __construct(array $attributesErrors = []) - { - /** @psalm-var array> */ - $this->attributesErrors = $attributesErrors; + public function __construct( + private array $attributesErrors = [], + ) { } public function addError(string $attribute, string $error): void diff --git a/tests/TestSupport/TestTrait.php b/tests/TestSupport/TestTrait.php index d27f37a4..0efb53cc 100644 --- a/tests/TestSupport/TestTrait.php +++ b/tests/TestSupport/TestTrait.php @@ -17,10 +17,6 @@ trait TestTrait { /** * Asserting two strings equality ignoring line endings. - * - * @param string $expected - * @param string $actual - * @param string $message */ protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void { @@ -38,9 +34,6 @@ private function createValidatorMock(): ValidatorInterface /** * Invokes an inaccessible method. * - * @param object $object - * @param string $method - * @param array $args * @param bool $revoke whether to make method inaccessible after execution. * * @throws ReflectionException @@ -64,12 +57,9 @@ protected function invokeMethod(object $object, string $method, array $args = [] /** * Sets an inaccessible object property to a designated value. * - * @param object $object - * @param string $propertyName - * @param mixed $value * @param bool $revoke whether to make property inaccessible after setting */ - protected function setInaccessibleProperty(object $object, string $propertyName, $value, bool $revoke = true): void + protected function setInaccessibleProperty(object $object, string $propertyName, mixed $value, bool $revoke = true): void { $class = new ReflectionClass($object);