Skip to content

Commit

Permalink
Add rector and apply suggestions (#230)
Browse files Browse the repository at this point in the history
* Add rector files yiisoft/yii-dev-tool#232

* Add rector/rector dependecy

* [rector] Apply fixes

* Apply fixes from StyleCI

* Use predefined rector action

* fix

Co-authored-by: rector-bot <rector@yiiframework.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
4 people authored Jan 18, 2023
1 parent fd232fa commit ff93592
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 119 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -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']
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->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,
]);
};
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 2 additions & 4 deletions src/Field/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 0 additions & 2 deletions src/Field/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions src/Field/ErrorSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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
{
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/Field/Part/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down
2 changes: 0 additions & 2 deletions src/Field/Part/Hint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 0 additions & 2 deletions src/Field/RadioList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Field/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, string|array<array-key,string>> $data
*
* @return self
*/
public function optionsData(
array $data,
Expand Down
13 changes: 6 additions & 7 deletions src/FormErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
*/
final class FormErrors implements FormErrorsInterface
{
/** @psalm-var array<string, array<array-key, string>> */
private array $attributesErrors;

public function __construct(array $attributesErrors = [])
{
/** @psalm-var array<string, array<array-key, string>> */
$this->attributesErrors = $attributesErrors;
/**
* @psalm-param array<string, array<array-key, string>> $attributesErrors
*/
public function __construct(
private array $attributesErrors = [],
) {
}

public function addError(string $attribute, string $error): void
Expand Down
6 changes: 0 additions & 6 deletions src/Helper/HtmlForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 1 addition & 3 deletions tests/Field/CheckboxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<HTML
Expand Down
12 changes: 3 additions & 9 deletions tests/Field/Part/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ public function testMessageCallback(): void
$result = Error::widget()
->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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down
16 changes: 7 additions & 9 deletions tests/Field/RadioListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,13 @@ public function testItemFormatter(): void
->hideLabel()
->items([1 => 'One', 2 => 'Two'])
->itemFormatter(
static function (RadioItem $item): string {
return '<div>' .
$item->index . ') ' .
Html::radio($item->radioAttributes['name'], $item->radioAttributes['value'])
->attributes($item->radioAttributes)
->checked($item->checked)
->label($item->label) .
'</div>';
}
static fn (RadioItem $item): string => '<div>' .
$item->index . ') ' .
Html::radio($item->radioAttributes['name'], $item->radioAttributes['value'])
->attributes($item->radioAttributes)
->checked($item->checked)
->label($item->label) .
'</div>'
)
->render();

Expand Down
56 changes: 28 additions & 28 deletions tests/Field/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public function testAddOutputAttributes(): void
->addOutputAttributes(['id' => 'UID'])
->render();

$expected = <<<HTML
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<span id="UID" class="red">23</span>
</div>
HTML;
$expected = <<<HTML_WRAP
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<span id="UID" class="red">23</span>
</div>
HTML_WRAP;

$this->assertSame($expected, $result);
}
Expand All @@ -67,13 +67,13 @@ public function testOutputAttributes(): void
->outputAttributes(['id' => 'UID'])
->render();

$expected = <<<HTML
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<span id="UID">23</span>
</div>
HTML;
$expected = <<<HTML_WRAP
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<span id="UID">23</span>
</div>
HTML_WRAP;

$this->assertSame($expected, $result);
}
Expand All @@ -88,13 +88,13 @@ public function testWithOutput(): void
->outputAttributes(['id' => 'UID'])
->render();

$expected = <<<HTML
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" min="1" max="100" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<span id="UID">23</span>
</div>
HTML;
$expected = <<<HTML_WRAP
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" min="1" max="100" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<span id="UID">23</span>
</div>
HTML_WRAP;

$this->assertSame($expected, $result);
}
Expand All @@ -108,13 +108,13 @@ public function testCustomOutputTag(): void
->outputAttributes(['id' => 'UID'])
->render();

$expected = <<<HTML
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<div id="UID">23</div>
</div>
HTML;
$expected = <<<HTML_WRAP
<div>
<label for="rangeform-volume">Volume level</label>
<input type="range" id="rangeform-volume" name="RangeForm[volume]" value="23" oninput="document.getElementById(&quot;UID&quot;).innerHTML=this.value">
<div id="UID">23</div>
</div>
HTML_WRAP;

$this->assertSame($expected, $result);
}
Expand Down
4 changes: 0 additions & 4 deletions tests/Helper/HtmlFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading

0 comments on commit ff93592

Please sign in to comment.