Skip to content

Commit

Permalink
Add rector (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed May 5, 2022
1 parent cc2cb97 commit 3a65b6c
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -8,6 +8,7 @@ tests export-ignore
docs export-ignore
Makefile export-ignore
phpunit.xml.dist export-ignore
rector.php export-ignore
phpstan.neon.dist export-ignore
phpstan-baseline.neon export-ignore
phpstan-console-application.php export-ignore
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/qa.yaml
Expand Up @@ -62,3 +62,28 @@ jobs:

- name: Psalm
run: vendor/bin/psalm --no-progress --show-info=false --stats --output-format=github --threads=$(nproc) --shepherd --php-version=8.1

rector:
name: Rector

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: none
tools: composer:v2

- name: Install Composer dependencies (highest)
uses: ramsey/composer-install@v2
with:
dependency-versions: highest
composer-options: --prefer-dist --prefer-stable

- name: Rector
run: vendor/bin/rector --no-progress-bar --dry-run
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -100,3 +100,7 @@ phpstan:
psalm:
vendor/bin/psalm --php-version=8.1
.PHONY: psalm

rector:
vendor/bin/rector
.PHONY: rector
5 changes: 5 additions & 0 deletions composer.json
Expand Up @@ -47,6 +47,7 @@
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-symfony": "^3.0",
"rector/rector": "^0.12",
"symfony/config": "^4.4 || ^5.3 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.3 || ^6.0",
"symfony/framework-bundle": "^4.4 || ^5.3 || ^6.0",
Expand Down Expand Up @@ -75,6 +76,10 @@
}
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"phpstan/extension-installer": true
},
"sort-packages": true
},
"extra": {
Expand Down
41 changes: 41 additions & 0 deletions rector.php
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/*
* DO NOT EDIT THIS FILE!
*
* It's auto-generated by sonata-project/dev-kit package.
*/

use Rector\Config\RectorConfig;
use Rector\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector;
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__.'/src',
__DIR__.'/tests',
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
]);

$rectorConfig->importNames();
$rectorConfig->disableImportShortClasses();
$rectorConfig->skip([
CountOnNullRector::class,
ExceptionHandlerTypehintRector::class,
]);
};
2 changes: 1 addition & 1 deletion src/Date/MomentFormatConverter.php
Expand Up @@ -28,7 +28,7 @@ class MomentFormatConverter
* For ICU formats see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
* For Moment formats see http://momentjs.com/docs/#/displaying/format/
*/
private static $formatConvertRules = [
private static array $formatConvertRules = [
// year
'yyyy' => 'YYYY', 'yy' => 'YY', 'y' => 'YYYY',
// month
Expand Down
14 changes: 4 additions & 10 deletions src/EventListener/ResizeFormListener.php
Expand Up @@ -25,25 +25,19 @@
*/
final class ResizeFormListener implements EventSubscriberInterface
{
/**
* @var string
*/
private $type;
private string $type;

/**
* @var bool
*/
private $resizeOnSubmit;
private bool $resizeOnSubmit;

/**
* @var array<string, mixed>
*/
private $typeOptions;
private array $typeOptions;

/**
* @var string[]
*/
private $removed = [];
private array $removed = [];

/**
* @var \Closure|null
Expand Down
25 changes: 12 additions & 13 deletions src/Test/AbstractWidgetTestCase.php
Expand Up @@ -32,10 +32,7 @@
*/
abstract class AbstractWidgetTestCase extends TypeTestCase
{
/**
* @var FormRenderer
*/
private $renderer;
private FormRenderer $renderer;

protected function setUp(): void
{
Expand All @@ -49,9 +46,7 @@ protected function setUp(): void
);

$environment->addRuntimeLoader(new FactoryRuntimeLoader([
FormRenderer::class => function (): FormRenderer {
return $this->renderer;
},
FormRenderer::class => fn (): FormRenderer => $this->renderer,
]));
$environment->addExtension(new FormExtension());
}
Expand Down Expand Up @@ -117,15 +112,19 @@ final protected function renderWidget(FormView $view, array $vars = []): string
*/
final protected function cleanHtmlWhitespace(string $html): string
{
return preg_replace_callback('/\s*>([^<]+)</', static function (array $value): string {
return '>'.trim($value[1]).'<';
}, $html) ?? '';
return preg_replace_callback(
'/\s*>([^<]+)</',
static fn (array $value): string => '>'.trim($value[1]).'<',
$html
) ?? '';
}

final protected function cleanHtmlAttributeWhitespace(string $html): string
{
return preg_replace_callback('~<([A-Z0-9]+) \K(.*?)>~i', static function (array $m): string {
return preg_replace('~\s*~', '', $m[0]) ?? '';
}, $html) ?? '';
return preg_replace_callback(
'~<([A-Z0-9]+) \K(.*?)>~i',
static fn (array $m): string => preg_replace('~\s*~', '', $m[0]) ?? '',
$html
) ?? '';
}
}
13 changes: 6 additions & 7 deletions src/Type/BasePickerType.php
Expand Up @@ -41,10 +41,7 @@ abstract class BasePickerType extends AbstractType implements LocaleAwareInterfa
*/
protected $locale;

/**
* @var MomentFormatConverter
*/
private $formatConverter;
private MomentFormatConverter $formatConverter;

/**
* NEXT_MAJOR: Add "string" typehint to $requestStackOrDefaultLocale and change the name to defaultLocale.
Expand Down Expand Up @@ -135,9 +132,11 @@ public function finishView(FormView $view, FormInterface $form, array $options):
if (false !== strpos($key, 'dp_')) {
// We remove 'dp_' and camelize the options names
$dpKey = substr($key, 3);
$dpKey = preg_replace_callback('/_([a-z])/', static function (array $c): string {
return strtoupper($c[1]);
}, $dpKey);
$dpKey = preg_replace_callback(
'/_([a-z])/',
static fn (array $c): string => strtoupper($c[1]),
$dpKey
);

$dpOptions[$dpKey] = $value;
}
Expand Down
26 changes: 7 additions & 19 deletions src/Validator/ErrorElement.php
Expand Up @@ -81,45 +81,33 @@ final class ErrorElement
{
private const DEFAULT_TRANSLATION_DOMAIN = 'validators';

/**
* @var ExecutionContextInterface
*/
private $context;
private ExecutionContextInterface $context;

/**
* @var string|null
*/
private $group;
private ?string $group;

/**
* @var string[]
*/
private $stack = [];
private array $stack = [];

/**
* @var PropertyPathInterface[]
*/
private $propertyPaths = [];
private array $propertyPaths = [];

/**
* @var mixed
*/
private $subject;

/**
* @var string
*/
private $current = '';
private string $current = '';

/**
* @var string
*/
private $basePropertyPath;
private string $basePropertyPath;

/**
* @var array<array{string, array<string, mixed>, mixed}>
*/
private $errors = [];
private array $errors = [];

/**
* NEXT_MAJOR: Remove `$constraintValidatorFactory` from the signature.
Expand Down
10 changes: 2 additions & 8 deletions src/Validator/InlineValidator.php
Expand Up @@ -22,15 +22,9 @@

final class InlineValidator extends ConstraintValidator
{
/**
* @var ContainerInterface
*/
protected $container;
protected ContainerInterface $container;

/**
* @var ConstraintValidatorFactoryInterface
*/
protected $constraintValidatorFactory;
protected ConstraintValidatorFactoryInterface $constraintValidatorFactory;

/**
* @psalm-suppress ContainerDependency
Expand Down
2 changes: 1 addition & 1 deletion tests/EventListener/FixCheckboxDataListenerTest.php
Expand Up @@ -45,7 +45,7 @@ public function testFixCheckbox(
->addExtensions([])
->getFormFactory();

$formBuilder = new FormBuilder('checkbox', 'stdClass', $dispatcher, $formFactory);
$formBuilder = new FormBuilder('checkbox', \stdClass::class, $dispatcher, $formFactory);
$formBuilder->addViewTransformer($transformer);

$form = $formBuilder->getForm();
Expand Down
4 changes: 1 addition & 3 deletions tests/EventListener/ResizeFormListenerTest.php
Expand Up @@ -221,9 +221,7 @@ public function testPreSubmitDataWithClosure(): void

$data = ['baz' => 'caz'];

$closure = static function () use ($data): string {
return $data['baz'];
};
$closure = static fn (): string => $data['baz'];

$listener = new ResizeFormListener('form', $typeOptions, true, $closure);

Expand Down
5 changes: 1 addition & 4 deletions tests/Type/BasePickerTypeTest.php
Expand Up @@ -34,10 +34,7 @@ final class BasePickerTypeTest extends TestCase
*/
private $translator;

/**
* @var MomentFormatConverter
*/
private $momentFormatConverter;
private MomentFormatConverter $momentFormatConverter;

protected function setUp(): void
{
Expand Down
12 changes: 3 additions & 9 deletions tests/Type/ImmutableArrayTypeTest.php
Expand Up @@ -49,15 +49,9 @@ public function testCallback(): void

$builder = $this->createMock(TestFormBuilderInterface::class);
$builder->expects(static::once())->method('add')->with(
static::callback(static function ($name): bool {
return 'ttl' === $name;
}),
static::callback(static function ($name): bool {
return TextType::class === $name;
}),
static::callback(static function ($name): bool {
return $name === [1 => '1'];
})
static::callback(static fn ($name): bool => 'ttl' === $name),
static::callback(static fn ($name): bool => TextType::class === $name),
static::callback(static fn ($name): bool => $name === [1 => '1'])
);

$optionsCallback = static function ($builder, $name, $type, $extra): array {
Expand Down
4 changes: 1 addition & 3 deletions tests/Validator/Constraints/InlineConstraintTest.php
Expand Up @@ -39,9 +39,7 @@ public function testIsClosure(): void

public function testGetClosure(): void
{
$closure = static function (): string {
return 'FOO';
};
$closure = static fn (): string => 'FOO';

$constraint = new InlineConstraint(['service' => 'foo', 'method' => $closure, 'serializingWarning' => true]);
static::assertSame($closure, $constraint->getClosure());
Expand Down
10 changes: 2 additions & 8 deletions tests/Validator/ErrorElementTest.php
Expand Up @@ -29,10 +29,7 @@
*/
final class ErrorElementTest extends TestCase
{
/**
* @var ErrorElement
*/
private $errorElement;
private ErrorElement $errorElement;

/**
* @var ExecutionContextInterface&MockObject
Expand All @@ -44,10 +41,7 @@ final class ErrorElementTest extends TestCase
*/
private $contextualValidator;

/**
* @var Foo
*/
private $subject;
private Foo $subject;

protected function setUp(): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Validator/InlineValidatorTest.php
Expand Up @@ -49,6 +49,8 @@ protected function setUp(): void
$this->container = $this->createMock(ContainerInterface::class);
$this->constraintValidatorFactory = $this->createMock(ConstraintValidatorFactoryInterface::class);
$this->context = $this->createMock(ExecutionContextInterface::class);

$this->context->method('getPropertyPath')->willReturn('propertyPath');
}

public function testGetErrorElement(): void
Expand Down

0 comments on commit 3a65b6c

Please sign in to comment.