Skip to content

Commit

Permalink
Add rector (#1441)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jul 23, 2022
1 parent 01a2e04 commit 0d34cca
Show file tree
Hide file tree
Showing 48 changed files with 153 additions and 199 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
bin/console export-ignore
phpstan.neon.dist export-ignore
phpstan-baseline.neon export-ignore
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/qa.yaml
Expand Up @@ -66,3 +66,28 @@ jobs:

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

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.0'
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 @@ -119,3 +119,7 @@ phpstan:
psalm:
vendor/bin/psalm --php-version=8.0
.PHONY: psalm

rector:
vendor/bin/rector
.PHONY: rector
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -65,6 +65,7 @@
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-symfony": "^3.0",
"rector/rector": "dev-main",
"sonata-project/cache": "^2.0",
"sonata-project/cache-bundle": "^3.3",
"symfony/browser-kit": "^4.4",
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,
]);
};
14 changes: 6 additions & 8 deletions src/Admin/BlockAdmin.php
Expand Up @@ -146,14 +146,12 @@ protected function configureFormFields(FormMapper $form)
if ($isStandardBlock && $page && !empty($containerBlockTypes)) {
$form->add('parent', EntityType::class, [
'class' => $this->getClass(),
'query_builder' => static function (EntityRepository $repository) use ($page, $containerBlockTypes) {
return $repository->createQueryBuilder('a')
->andWhere('a.page = :page AND a.type IN (:types)')
->setParameters([
'page' => $page,
'types' => $containerBlockTypes,
]);
},
'query_builder' => static fn (EntityRepository $repository) => $repository->createQueryBuilder('a')
->andWhere('a.page = :page AND a.type IN (:types)')
->setParameters([
'page' => $page,
'types' => $containerBlockTypes,
]),
], [
'admin_code' => $this->getCode(),
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/PageAdmin.php
Expand Up @@ -121,7 +121,7 @@ public function getSite()
$siteId = $values['site'] ?? null;
}

$siteId = (null !== $siteId) ? $siteId : $this->getRequest()->get('siteId');
$siteId ??= $this->getRequest()->get('siteId');

if ($siteId) {
$site = $this->siteManager->findOneBy(['id' => $siteId]);
Expand Down
2 changes: 1 addition & 1 deletion src/Block/BreadcrumbBlockService.php
Expand Up @@ -55,7 +55,7 @@ public function getName()

public function getBlockMetadata($code = null)
{
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataPageBundle', [
return new Metadata($this->getName(), ($code ?? $this->getName()), false, 'SonataPageBundle', [
'class' => 'fa fa-bars',
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Block/ContainerBlockService.php
Expand Up @@ -43,7 +43,7 @@ public function configureSettings(OptionsResolver $resolver)

public function getBlockMetadata($code = null)
{
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataPageBundle', [
return new Metadata($this->getName(), ($code ?? $this->getName()), false, 'SonataPageBundle', [
'class' => 'fa fa-square-o',
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Block/PageListBlockService.php
Expand Up @@ -113,7 +113,7 @@ public function configureSettings(OptionsResolver $resolver)

public function getBlockMetadata($code = null)
{
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataPageBundle', [
return new Metadata($this->getName(), ($code ?? $this->getName()), false, 'SonataPageBundle', [
'class' => 'fa fa-home',
]);
}
Expand Down
10 changes: 2 additions & 8 deletions src/Block/SharedBlockBlockService.php
Expand Up @@ -45,15 +45,9 @@ class SharedBlockBlockService extends AbstractAdminBlockService
*/
private $sharedBlockAdmin;

/**
* @var ContainerInterface
*/
private $container;
private ContainerInterface $container;

/**
* @var BlockManagerInterface
*/
private $blockManager;
private BlockManagerInterface $blockManager;

/**
* @param string $name
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/BlockJsCache.php
Expand Up @@ -150,7 +150,7 @@ public function cacheAction(Request $request)
}
}
})();
', $block->getId(), json_encode($response->getContent())));
', $block->getId(), json_encode($response->getContent(), \JSON_THROW_ON_ERROR)));

$response->headers->set('Content-Type', 'application/javascript');

Expand Down Expand Up @@ -236,7 +236,7 @@ private function validateKeys(array $keys)
{
foreach (['block_id', 'page_id', 'manager', 'updated_at'] as $key) {
if (!isset($keys[$key])) {
throw new \RuntimeException(sprintf('Please define a `%s` key, provided: %s', $key, json_encode(array_keys($keys))));
throw new \RuntimeException(sprintf('Please define a `%s` key, provided: %s', $key, json_encode(array_keys($keys), \JSON_THROW_ON_ERROR)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BaseCommand.php
Expand Up @@ -117,7 +117,7 @@ public function run(InputInterface $input, OutputInterface $output)
@trigger_error(
sprintf(
'The %s class is deprecate since sonata-project/page-bundle 3.27.0 and it will be remove in 4.0',
__CLASS__
self::class
),
\E_USER_DEPRECATED
);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CreateSnapshotsCommand.php
Expand Up @@ -47,7 +47,7 @@ public function __construct($createSnapshot = null)
@trigger_error(sprintf(
'The %s class is final since sonata-project/page-bundle 3.27.0 and and it will be removed in 4.0'
.'release you won\'t be able to extend this class anymore.',
__CLASS__
self::class
), \E_USER_DEPRECATED);
parent::__construct($createSnapshot);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/MigrateToJsonTypeCommand.php
Expand Up @@ -43,7 +43,7 @@ public function execute(InputInterface $input, OutputInterface $output)
foreach ($blocks as $block) {
// if the row need to migrate
if (0 !== strpos($block['settings'], '{') && '[]' !== $block['settings']) {
$block['settings'] = json_encode(unserialize($block['settings']));
$block['settings'] = json_encode(unserialize($block['settings']), \JSON_THROW_ON_ERROR);
$connection->update($table, ['settings' => $block['settings']], ['id' => $block['id']]);
++$count;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Command/RenderBlockCommand.php
Expand Up @@ -69,14 +69,14 @@ public function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf(' > Id: %d - type: %s - name: %s', $block->getId(), $block->getType(), $block->getName()));

foreach ($block->getSettings() as $name => $value) {
$output->writeln(sprintf(' >> %s: %s', $name, json_encode($value)));
$output->writeln(sprintf(' >> %s: %s', $name, json_encode($value, \JSON_THROW_ON_ERROR)));
}

$context = $this->getContainer()->get('sonata.block.context_manager')->get($block);

$output->writeln("\n<info>BlockContext Information</info>");
foreach ($context->getSettings() as $name => $value) {
$output->writeln(sprintf(' >> %s: %s', $name, json_encode($value)));
$output->writeln(sprintf(' >> %s: %s', $name, json_encode($value, \JSON_THROW_ON_ERROR)));
}

$output->writeln("\n<info>Response Output</info>");
Expand Down
4 changes: 1 addition & 3 deletions src/Form/Type/PageSelectorType.php
Expand Up @@ -48,9 +48,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'page' => null,
'site' => null,
'choices' => static function (Options $opts, $previousValue) use ($that) {
return $that->getChoices($opts);
},
'choices' => static fn (Options $opts, $previousValue) => $that->getChoices($opts),
'choice_translation_domain' => false,
'filter_choice' => [
'current_page' => false,
Expand Down
4 changes: 1 addition & 3 deletions src/Generator/Mustache.php
Expand Up @@ -47,9 +47,7 @@ public function __construct()
*/
public static function replace($string, array $parameters)
{
$replacer = static function ($match) use ($parameters) {
return $parameters[$match[1]] ?? $match[0];
};
$replacer = static fn ($match) => $parameters[$match[1]] ?? $match[0];

return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Listener/ExceptionListener.php
Expand Up @@ -17,6 +17,7 @@
use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;
use Sonata\PageBundle\CmsManager\DecoratorStrategyInterface;
use Sonata\PageBundle\Exception\InternalErrorException;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Page\PageServiceManagerInterface;
use Sonata\PageBundle\Site\SiteSelectorInterface;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -131,7 +132,7 @@ public function hasErrorCode($statusCode)
* @throws \RuntimeException When site is not found, check your state database
* @throws InternalErrorException When you do not configure page for http error code
*
* @return \Sonata\PageBundle\Model\PageInterface
* @return PageInterface
*/
public function getErrorCodePage($statusCode)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Listener/ResponseListener.php
Expand Up @@ -52,10 +52,7 @@ class ResponseListener
*/
protected $templating;

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

/**
* @param CmsManagerSelectorInterface $cmsSelector CMS manager selector
Expand Down
5 changes: 1 addition & 4 deletions src/Mapper/PageFormMapper.php
Expand Up @@ -19,10 +19,7 @@

final class PageFormMapper implements FormMapper
{
/**
* @var AdminFormMapper
*/
private $adminFormMapper;
private AdminFormMapper $adminFormMapper;

public function __construct(AdminFormMapper $adminFormMapper)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Page.php
Expand Up @@ -627,7 +627,7 @@ public static function slugify($text)
$text = strtolower($text);

// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
$text = preg_replace('~[^\\-\w]+~', '', $text);

return $text;
}
Expand Down
24 changes: 6 additions & 18 deletions src/Model/SnapshotPageProxy.php
Expand Up @@ -22,20 +22,11 @@
*/
class SnapshotPageProxy implements SnapshotPageProxyInterface
{
/**
* @var SnapshotManagerInterface
*/
private $manager;
private SnapshotManagerInterface $manager;

/**
* @var SnapshotInterface
*/
private $snapshot;
private SnapshotInterface $snapshot;

/**
* @var PageInterface
*/
private $page;
private ?PageInterface $page = null;

/**
* @var pageInterface|null
Expand All @@ -47,14 +38,11 @@ class SnapshotPageProxy implements SnapshotPageProxyInterface
private $target;

/**
* @var PageInterface[]
* @var PageInterface[]|null
*/
private $parents;
private ?array $parents = null;

/**
* @var TransformerInterface
*/
private $transformer;
private TransformerInterface $transformer;

/**
* @param SnapshotManagerInterface $manager Snapshot manager
Expand Down
5 changes: 1 addition & 4 deletions src/Model/SnapshotPageProxyFactory.php
Expand Up @@ -15,10 +15,7 @@

final class SnapshotPageProxyFactory implements SnapshotPageProxyFactoryInterface
{
/**
* @var string
*/
private $snapshotPageProxyClass;
private string $snapshotPageProxyClass;

/**
* @param string $snapshotPageProxyClass class name
Expand Down
8 changes: 3 additions & 5 deletions src/Request/RequestFactory.php
Expand Up @@ -20,7 +20,7 @@
*/
class RequestFactory
{
private static $types = [
private static array $types = [
'host' => Request::class,
'host_with_path' => SiteRequest::class,
'host_with_path_by_locale' => SiteRequest::class,
Expand Down Expand Up @@ -66,17 +66,15 @@ private static function configureFactory($type)
return;
}

Request::setFactory(static function (
Request::setFactory(static fn (
array $query = [],
array $request = [],
array $attributes = [],
array $cookies = [],
array $files = [],
array $server = [],
$content = null
) {
return new SiteRequest($query, $request, $attributes, $cookies, $files, $server, $content);
});
) => new SiteRequest($query, $request, $attributes, $cookies, $files, $server, $content));
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Request/SiteRequestContext.php
Expand Up @@ -31,10 +31,7 @@ class SiteRequestContext extends RequestContext implements SiteRequestContextInt
*/
protected $selector;

/**
* @var SiteInterface
*/
private $site;
private ?SiteInterface $site = null;

/**
* @param string $baseUrl
Expand Down

0 comments on commit 0d34cca

Please sign in to comment.