Skip to content

Commit

Permalink
Increase Psalm to level 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 8, 2022
1 parent c7b1889 commit 65e21ed
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 70 deletions.
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" errorLevel="3" findUnusedPsalmSuppress="true" resolveFromConfigFile="true" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" errorLevel="2" findUnusedPsalmSuppress="true" resolveFromConfigFile="true" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
Expand Down
4 changes: 3 additions & 1 deletion src/Admin/BaseBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ abstract class BaseBlockAdmin extends AbstractAdmin
*/
protected $containerBlockTypes = [];

public function setBlockManager(BlockServiceManagerInterface $blockManager): void
public function __construct(BlockServiceManagerInterface $blockManager)
{
parent::__construct();

$this->blockManager = $blockManager;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap;
use Sonata\BlockBundle\Block\BlockServiceManagerInterface;
use Sonata\BlockBundle\Block\Service\BlockServiceInterface;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Form\Type\ServiceListType;
Expand Down Expand Up @@ -55,9 +56,9 @@ final class BlockAdmin extends BaseBlockAdmin
* }>,
* }> $blocks
*/
public function __construct(array $blocks = [])
public function __construct(BlockServiceManagerInterface $blockManager, array $blocks = [])
{
parent::__construct();
parent::__construct($blockManager);

$this->blocks = $blocks;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Admin/PageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\PageBundle\Exception\InternalErrorException;
use Sonata\PageBundle\Exception\PageNotFoundException;
Expand Down Expand Up @@ -196,10 +197,12 @@ protected function configureDatagridFilters(DatagridMapper $filter): void
->add('parent')
->add('edited')
->add('hybrid', CallbackFilter::class, [
'callback' => static function ($queryBuilder, $alias, $field, $data): void {
'callback' => static function (ProxyQueryInterface $queryBuilder, string $alias, string $field, array $data): void {
$builder = $queryBuilder->getQueryBuilder();

if (\in_array($data['value'], ['hybrid', 'cms'], true)) {
$queryBuilder->andWhere(sprintf('%s.routeName %s :routeName', $alias, 'cms' === $data['value'] ? '=' : '!='));
$queryBuilder->setParameter('routeName', PageInterface::PAGE_ROUTE_CMS_NAME);
$builder->andWhere(sprintf('%s.routeName %s :routeName', $alias, 'cms' === $data['value'] ? '=' : '!='));
$builder->setParameter('routeName', PageInterface::PAGE_ROUTE_CMS_NAME);
}
},
'field_options' => [
Expand Down
4 changes: 2 additions & 2 deletions src/CmsManager/BaseCmsPageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
abstract class BaseCmsPageManager implements CmsManagerInterface
{
/**
* @var PageInterface
* @var PageInterface|null
*/
protected $currentPage;
protected $currentPage = null;

/**
* @var array<PageBlockInterface|null>
Expand Down
2 changes: 0 additions & 2 deletions src/CmsManager/CmsManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public function getPageById($id);
public function getBlock($id);

/**
* Returns the current page.
*
* @return PageInterface|null
*/
public function getCurrentPage();
Expand Down
5 changes: 4 additions & 1 deletion src/CmsManager/DecoratorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function __construct(

public function isDecorable(Request $request, $requestType, Response $response)
{
if (HttpKernelInterface::MASTER_REQUEST !== $requestType) {
// TODO: Simplify this when dropping support for Symfony < 5.3
$mainRequestType = \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : 1;

if ($mainRequestType !== $requestType) {
return false;
}

Expand Down
10 changes: 4 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultFalse()
->end()
->scalarNode('multisite')
->info('For more information, see https://sonata-project.org/bundles/page/master/doc/reference/multisite.html')
->info('For more information, see https://docs.sonata-project.org/projects/SonataPageBundle/en/4.x/reference/multisite/')
->isRequired()
->validate()
->ifNotInArray(['host', 'host_by_locale', 'host_with_path', 'host_with_path_by_locale'])
->thenInvalid('Invalid multisite configuration %s. For more information, see https://sonata-project.org/bundles/page/master/doc/reference/multisite.html')
->thenInvalid('Invalid multisite configuration %s. For more information, see https://docs.sonata-project.org/projects/SonataPageBundle/en/4.x/reference/multisite/')
->end()
->end()
->arrayNode('router_auto_register')
Expand Down Expand Up @@ -187,16 +187,14 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->validate()
->always()
->then(static function ($matrix) {
return Parser::parse($matrix['layout'], $matrix['mapping']);
})
->then(static fn (array $matrix) => Parser::parse($matrix['layout'], $matrix['mapping']))
->end()
->end()
->end()
->end()
->validate()
->always()
->then(static function ($templates) {
->then(static function (array $templates): array {
foreach ($templates as $id => &$template) {
if (0 === \count($template['containers'])) {
continue;
Expand Down
10 changes: 8 additions & 2 deletions src/Entity/SnapshotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ final class SnapshotManager extends BaseEntityManager implements SnapshotManager
{
private SnapshotPageProxyFactoryInterface $snapshotPageProxyFactory;

public function __construct($class, ManagerRegistry $registry, SnapshotPageProxyFactoryInterface $snapshotPageProxyFactory)
{
/**
* @param class-string<SnapshotInterface> $class
*/
public function __construct(
string $class,
ManagerRegistry $registry,
SnapshotPageProxyFactoryInterface $snapshotPageProxyFactory
) {
parent::__construct($class, $registry);

$this->snapshotPageProxyFactory = $snapshotPageProxyFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/PageSelectorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults([
'page' => null,
'site' => null,
'choices' => static fn (Options $opts, $previousValue) => $that->getChoices($opts),
'choices' => static fn (Options $opts) => $that->getChoices($opts),
'choice_translation_domain' => false,
'filter_choice' => [
'current_page' => false,
Expand Down
4 changes: 4 additions & 0 deletions src/Model/SnapshotChildrenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* @extends AbstractLazyCollection<array-key, PageInterface>
*
* @author Thomas Rabaix <[email protected]>
*
* @psalm-suppress PropertyNotSetInConstructor
*
* @see $collection property is initialized in the doInitialize method
*/
final class SnapshotChildrenCollection extends AbstractLazyCollection
{
Expand Down
23 changes: 14 additions & 9 deletions src/Request/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ private static function configureFactory($type): void
return;
}

Request::setFactory(static fn (
array $query = [],
array $request = [],
array $attributes = [],
array $cookies = [],
array $files = [],
array $server = [],
$content = null
) => new SiteRequest($query, $request, $attributes, $cookies, $files, $server, $content));
Request::setFactory(
/**
* @param string|resource|null $content
*/
static fn (
array $query = [],
array $request = [],
array $attributes = [],
array $cookies = [],
array $files = [],
array $server = [],
$content = null
) => new SiteRequest($query, $request, $attributes, $cookies, $files, $server, $content)
);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/Resources/config/admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@
<service id="%sonata.page.admin.page.class%" alias="sonata.page.admin.page"/>
<service id="sonata.page.admin.block" class="%sonata.page.admin.block.class%" public="true">
<tag name="sonata.admin" model_class="%sonata.page.admin.block.entity%" controller="%sonata.page.admin.block.controller%" manager_type="orm" show_in_dashboard="false" group="%sonata.page.admin.groupname%" translation_domain="%sonata.page.admin.page.translation_domain%" label="block" label_translator_strategy="sonata.admin.label.strategy.underscore" icon="%sonata.page.admin.groupicon%"/>
<argument type="service" id="sonata.block.manager"/>
<argument>%sonata_block.blocks%</argument>
<call method="setBlockManager">
<argument type="service" id="sonata.block.manager"/>
</call>
<call method="setTranslationDomain">
<argument>%sonata.page.admin.block.translation_domain%</argument>
</call>
Expand All @@ -58,9 +56,7 @@
<service id="%sonata.page.admin.block.class%" alias="sonata.page.admin.block"/>
<service id="sonata.page.admin.shared_block" class="%sonata.page.admin.shared_block.class%" public="true">
<tag name="sonata.admin" model_class="%sonata.page.admin.block.entity%" controller="%sonata.page.admin.shared_block.controller%" manager_type="orm" show_in_dashboard="true" group="%sonata.page.admin.groupname%" translation_domain="%sonata.page.admin.page.translation_domain%" label="shared_block" label_translator_strategy="sonata.admin.label.strategy.underscore" icon="%sonata.page.admin.groupicon%"/>
<call method="setBlockManager">
<argument type="service" id="sonata.block.manager"/>
</call>
<argument type="service" id="sonata.block.manager"/>
<call method="setTranslationDomain">
<argument>%sonata.page.admin.shared_block.translation_domain%</argument>
</call>
Expand Down
8 changes: 8 additions & 0 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Security\Http\Authentication\AuthenticatorManager;

/**
* @psalm-suppress PropertyNotSetInConstructor
*
* @see https://github.com/psalm/psalm-plugin-symfony/pull/220
*/
final class AppKernel extends Kernel
{
use MicroKernelTrait;
Expand Down Expand Up @@ -91,6 +96,9 @@ protected function configureRoutes($routes): void
$routes->import($this->getProjectDir().'/config/routes.yaml');
}

/**
* @psalm-suppress DeprecatedClass
*/
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$loader->load($this->getProjectDir().'/config/config.yaml');
Expand Down
8 changes: 3 additions & 5 deletions tests/CmsManager/CmsPageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,13 @@ public function testGetPageWithoutParamException(): void
*/
private function getMockBlockInteractor(): BlockInteractorInterface
{
$callback = static function ($options) {
$mock = $this->createMock(BlockInteractorInterface::class);
$mock->method('createNewContainer')->willReturnCallback(static function (array $options) {
$block = new CmsBlock();
$block->setSettings($options);

return $block;
};

$mock = $this->createMock(BlockInteractorInterface::class);
$mock->method('createNewContainer')->willReturnCallback($callback);
});

return $mock;
}
Expand Down
13 changes: 8 additions & 5 deletions tests/CmsManager/DecoratorStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,30 @@ public function testIsDecorable(): void
$response->headers = new ResponseHeaderBag();
$response->headers->set('Content-Type', 'foo/test');

static::assertFalse($strategy->isDecorable($request, HttpKernelInterface::MASTER_REQUEST, $response));
// TODO: Simplify this when dropping support for Symfony < 5.3
$mainRequestType = \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : 1;

static::assertFalse($strategy->isDecorable($request, $mainRequestType, $response));

$response->headers->set('Content-Type', 'text/html');
$response->setStatusCode(404);
static::assertFalse($strategy->isDecorable($request, HttpKernelInterface::MASTER_REQUEST, $response));
static::assertFalse($strategy->isDecorable($request, $mainRequestType, $response));

$response->setStatusCode(200);

$request->headers->set('x-requested-with', 'XMLHttpRequest');
static::assertFalse($strategy->isDecorable($request, HttpKernelInterface::MASTER_REQUEST, $response));
static::assertFalse($strategy->isDecorable($request, $mainRequestType, $response));

$request->headers->set('x-requested-with', null);

$response->headers->set('x-sonata-page-not-decorable', '1');
static::assertFalse($strategy->isDecorable($request, HttpKernelInterface::MASTER_REQUEST, $response));
static::assertFalse($strategy->isDecorable($request, $mainRequestType, $response));

$response->headers->remove('x-sonata-page-not-decorable');

$request->headers->set('x-requested-with', 'XMLHttpRequest');
$response->headers->set('x-sonata-page-decorable', '1');
static::assertTrue($strategy->isDecorable($request, HttpKernelInterface::MASTER_REQUEST, $response));
static::assertTrue($strategy->isDecorable($request, $mainRequestType, $response));
}

public function testIgnoreRouteNameMatch(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/Entity/SnapshotManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function testEnableSnapshots(): void
$platform->method('getDateTimeFormatString')->willReturn('Y-m-d H:i:s');
$connection->method('getDatabasePlatform')->willReturn($platform);
$connection->method('getParams')->willReturn([]);
$unit->method('getSingleIdentifierValue')->willReturnCallback(static function ($entity) {
$unit->method('getSingleIdentifierValue')->willReturnCallback(static function (object $entity) {
if ($entity instanceof \DateTimeInterface) {
throw new MappingException();
}
Expand All @@ -125,7 +125,7 @@ public function testEnableSnapshots(): void
});

$metaDataFactory->method('hasMetadataFor')
->willReturnCallback(static fn ($class) => SonataPageSnapshot::class === $class);
->willReturnCallback(static fn (string $class) => SonataPageSnapshot::class === $class);
$metaDataFactory->method('getMetadataFor')->with(SonataPageSnapshot::class)->willReturn($classMetadata);

$this->entityManager->expects(static::once())->method('persist')->with($snapshot);
Expand All @@ -134,7 +134,7 @@ public function testEnableSnapshots(): void
$this->entityManager->expects(static::once())->method('createQueryBuilder')->willReturn(new QueryBuilder($this->entityManager));
$this->entityManager->method('getConfiguration')->willReturn(new Configuration());
$this->entityManager->method('getExpressionBuilder')->willReturn(new Expr());
$this->entityManager->expects(static::once())->method('createQuery')->willReturnCallback(function ($dql) {
$this->entityManager->expects(static::once())->method('createQuery')->willReturnCallback(function (string $dql) {
$query = new Query($this->entityManager);
$query->setDQL($dql);

Expand Down
5 changes: 4 additions & 1 deletion tests/Listener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ private function getMockEvent(\Exception $exception): ExceptionEvent
$kernel = $this->createMock(HttpKernelInterface::class);
$request = new Request();

return new ExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $exception);
// TODO: Simplify this when dropping support for Symfony < 5.3
$mainRequestType = \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : 1;

return new ExceptionEvent($kernel, $request, $mainRequestType, $exception);
}
}
10 changes: 8 additions & 2 deletions tests/Listener/RequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public function testValidSite(): void
$kernel = $this->createMock(HttpKernelInterface::class);
$request = new Request();

$event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
// TODO: Simplify this when dropping support for Symfony < 5.3
$mainRequestType = \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : 1;

$event = new RequestEvent($kernel, $request, $mainRequestType);

$listener = new RequestListener($cmsSelector, $siteSelector, $decoratorStrategy);
$listener->onCoreRequest($event);
Expand All @@ -74,7 +77,10 @@ public function testNoSite(): void
$kernel = $this->createMock(HttpKernelInterface::class);
$request = new Request();

$event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
// TODO: Simplify this when dropping support for Symfony < 5.3
$mainRequestType = \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : 1;

$event = new RequestEvent($kernel, $request, $mainRequestType);

$listener = new RequestListener($cmsSelector, $siteSelector, $decoratorStrategy);
$listener->onCoreRequest($event);
Expand Down
5 changes: 4 additions & 1 deletion tests/Listener/ResponseListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ private function getMockEvent(string $content): ResponseEvent
$request = new Request();
$response = new Response($content);

return new ResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);
// TODO: Simplify this when dropping support for Symfony < 5.3
$mainRequestType = \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : 1;

return new ResponseEvent($kernel, $request, $mainRequestType, $response);
}
}
2 changes: 1 addition & 1 deletion tests/Route/RoutePageGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function getRoutePageGenerator(): RoutePageGenerator

$pageManager->expects(static::atLeastOnce())
->method('findOneBy')
->willReturnCallback(static function ($criteria) use ($hybridPageWithBadHost) {
->willReturnCallback(static function (array $criteria) use ($hybridPageWithBadHost) {
if ($criteria === ['routeName' => 'test_hybrid_page_with_bad_host', 'site' => 1]) {
return $hybridPageWithBadHost;
}
Expand Down
Loading

0 comments on commit 65e21ed

Please sign in to comment.