Skip to content

Commit

Permalink
Bump phpstan (#7580)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Nov 18, 2021
1 parent 280815b commit 0ccf024
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 53 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
"matthiasnoback/symfony-config-test": "^4.2",
"matthiasnoback/symfony-dependency-injection-test": "^4.2",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.84",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpstan/phpstan-strict-rules": "^0.12.10",
"phpstan/phpstan-symfony": "^0.12.21",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpstan/phpstan-symfony": "^1.0",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"psalm/plugin-symfony": "^3.0",
Expand Down
8 changes: 4 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ parameters:
message: '#^Method Sonata\\AdminBundle\\Tests\\Fixtures\\Entity\\FooToStringNull\:\:__toString\(\) should return string but returns null\.$#'
path: tests/Fixtures/Entity/FooToStringNull.php
- # The phpstan-param is less precise than the psalm-param https://github.com/phpstan/phpstan/issues/4703
message: '#^Parameter \#3 \$idx of method Sonata\\AdminBundle\\Model\\ModelManagerInterface\<T of object\>\:\:addIdentifiersToQuery\(\) expects array\<int\|string\>\&nonEmpty\, array\<array\<string\>\|int\|string\>\&nonEmpty given\.$#'
message: '#^Parameter \#3 \$idx of method Sonata\\AdminBundle\\Model\\ModelManagerInterface\<T of object\>\:\:addIdentifiersToQuery\(\) expects non-empty-array\<int\|string\>, non-empty-array\<array\<string\>\|int\|string\> given\.$#'
path: src/Form/DataTransformer/ModelToIdPropertyTransformer.php
- # `treatPhpdocAsCertain: false` should not report this https://github.com/phpstan/phpstan/issues/5333
message: '#^Instanceof between Symfony\\Component\\Routing\\Route and Symfony\\Component\\Routing\\Route will always evaluate to true\.$#'
path: src/Route/RouteCollection.php
- # DataTransformer contravariance is not really mandatory
message: '#(reverseTransform|transform)\(\) should be contravariant#'
path: src/Form/DataTransformer
- # Generic with intersection is buggy https://github.com/phpstan/phpstan/issues/5336
message: '#Sonata\\AdminBundle\\Datagrid\\(Datagrid|PagerInterface)<PHPUnit\\Framework\\MockObject\\Stub&Sonata\\AdminBundle\\Datagrid\\ProxyQueryInterface>#'
path: tests/Datagrid/DatagridTest.php
- # https://github.com/phpstan/phpstan/issues/5893
message: '#^Parameter \#1 \$object of static method Doctrine\\Common\\Util\\ClassUtils\:\:getClass\(\) expects Doctrine\\Persistence\\Proxy\<T of object\>\|T of object, T of object|T of object given\.$#'
path: src/Admin/AbstractAdmin.php
5 changes: 3 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ includes:
- phpstan-baseline.neon

parameters:
level: max
level: 8
symfony:
console_application_loader: ./phpstan-console-application.php
paths:
- src
- tests
excludePaths:
analyseAndScan:
# temporarily ignore template files
- src/Resources/**.tpl.php
treatPhpDocTypesAsCertain: false
checkGenericClassInNonGenericObjectType: true
checkInternalClassCaseSensitivity: true
checkMissingIterableValueType: true
checkMissingVarTagTypehint: true
checkMissingTypehints: true
checkUninitializedProperties: true
32 changes: 15 additions & 17 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct(
* label: string,
* label_catalogue: string,
* icon: string,
* item_adds: array,
* item_adds: Item[],
* items: array<array-key, AdminInterface<object>>,
* keep_open: bool,
* on_top: bool,
Expand All @@ -105,23 +105,21 @@ public function getDashboardGroups(): array
$groups = [];

foreach ($this->adminGroups as $name => $adminGroup) {
if (isset($adminGroup['items'])) {
$items = array_filter(array_map(function (array $item): ?AdminInterface {
if (!isset($item['admin']) || '' === $item['admin']) {
return null;
}

$admin = $this->getInstance($item['admin']);
if (!$admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)) {
return null;
}

return $admin;
}, $adminGroup['items']));

if ([] !== $items) {
$groups[$name] = ['items' => $items] + $adminGroup;
$items = array_filter(array_map(function (array $item): ?AdminInterface {
if (!isset($item['admin']) || '' === $item['admin']) {
return null;
}

$admin = $this->getInstance($item['admin']);
if (!$admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)) {
return null;
}

return $admin;
}, $adminGroup['items']));

if ([] !== $items) {
$groups[$name] = ['items' => $items] + $adminGroup;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/Block/AdminPreviewBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ private function handleFilters(AdminInterface $admin, BlockContextInterface $blo
{
$filters = $blockContext->getSetting('filters');

if ($sortBy = $filters[DatagridInterface::SORT_BY] ?? null) {
$sortBy = $filters[DatagridInterface::SORT_BY] ?? null;
if (null !== $sortBy) {
$sortFilters = [DatagridInterface::SORT_BY => $sortBy];
if ($sortOrder = $filters[DatagridInterface::SORT_ORDER] ?? null) {

$sortOrder = $filters[DatagridInterface::SORT_ORDER] ?? null;
if (null !== $sortOrder) {
$sortFilters[DatagridInterface::SORT_ORDER] = $sortOrder;
unset($filters[DatagridInterface::SORT_ORDER]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Form/DataTransformer/ArrayToModelTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-template T of object
* @phpstan-implements DataTransformerInterface<T, T|array<string, mixed>>
*/
final class ArrayToModelTransformer implements DataTransformerInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/Form/DataTransformer/BooleanToStringTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* which allows you to use non-strings in reverseTransform() method.
*
* @author Peter Gribanov <[email protected]>
*
* @phpstan-implements DataTransformerInterface<bool, string>
*/
final class BooleanToStringTransformer implements DataTransformerInterface
{
Expand Down
3 changes: 3 additions & 0 deletions src/Form/DataTransformer/FilterDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* @phpstan-implements DataTransformerInterface<FilterData, array{type?: int|numeric-string|null, value?: mixed}>
*/
final class FilterDataTransformer implements DataTransformerInterface
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*
* @phpstan-template T of object
* @phpstan-template P
* @phpstan-implements DataTransformerInterface<T|array<T>|\Traversable<T>, int|string|array<int|string|array<string>>>
*/
final class ModelToIdPropertyTransformer implements DataTransformerInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Form/DataTransformer/ModelToIdTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-template T of object
* @phpstan-implements DataTransformerInterface<T, int|string>
*/
final class ModelToIdTransformer implements DataTransformerInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Form/DataTransformer/ModelsToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-template T of object
* @phpstan-implements DataTransformerInterface<\Traversable<T>, array<int|string>>
*/
final class ModelsToArrayTransformer implements DataTransformerInterface
{
Expand Down
7 changes: 2 additions & 5 deletions src/Form/DataTransformerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
final class DataTransformerResolver implements DataTransformerResolverInterface
{
/**
* @var array<string, DataTransformerInterface>
* @var array<string, DataTransformerInterface<mixed, mixed>>
*/
private $globalCustomTransformers = [];

/**
* @param array<string, DataTransformerInterface> $customGlobalTransformers
* @param array<string, DataTransformerInterface<mixed, mixed>> $customGlobalTransformers
*/
public function __construct(array $customGlobalTransformers = [])
{
Expand All @@ -44,9 +44,6 @@ public function addCustomGlobalTransformer(string $fieldType, DataTransformerInt
$this->globalCustomTransformers[$fieldType] = $dataTransformer;
}

/**
* @param ModelManagerInterface<object> $modelManager
*/
public function resolve(
FieldDescriptionInterface $fieldDescription,
ModelManagerInterface $modelManager
Expand Down
5 changes: 5 additions & 0 deletions src/Form/DataTransformerResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
*/
interface DataTransformerResolverInterface
{
/**
* @param DataTransformerInterface<mixed, mixed> $dataTransformer
*/
public function addCustomGlobalTransformer(string $fieldType, DataTransformerInterface $dataTransformer): void;

/**
* @param ModelManagerInterface<object> $modelManager
*
* @return DataTransformerInterface<mixed, mixed>
*/
public function resolve(
FieldDescriptionInterface $fieldDescription,
Expand Down
22 changes: 17 additions & 5 deletions src/Form/Extension/Field/Type/FormTypeFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function __construct(array $defaultClasses, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$sonataAdmin = [
'name' => null,
'admin' => null,
'name' => false,
'admin' => false,
'value' => null,
'edit' => 'standard',
'inline' => 'natural',
Expand Down Expand Up @@ -93,7 +93,11 @@ public function buildView(FormView $view, FormInterface $form, array $options):
/*
* We have a child, so we need to upgrade block prefix
*/
if ($view->parent && $view->parent->vars['sonata_admin_enabled'] && !($sonataAdmin['admin'] ?? false)) {
if (
null !== $view->parent
&& true === $view->parent->vars['sonata_admin_enabled']
&& false === $sonataAdmin['admin']
) {
$blockPrefixes = $view->vars['block_prefixes'];
$baseName = str_replace('.', '_', $view->parent->vars['sonata_admin_code']);

Expand Down Expand Up @@ -171,8 +175,7 @@ public static function getExtendedTypes(): iterable

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
$resolver->setDefaults([
'sonata_admin' => null,
'sonata_field_description' => null,

Expand All @@ -182,13 +185,22 @@ public function configureOptions(OptionsResolver $resolver): void
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle version 4.x and will be removed in 5.0.
*
* return the value related to FieldDescription, if the associated object does no
* exists => a temporary one is created.
*
* @return mixed
*/
public function getValueFromFieldDescription(?object $object, FieldDescriptionInterface $fieldDescription)
{
@trigger_error(sprintf(
'The method "%s()" is deprecated since sonata-project/admin-bundle 4.x and will be removed in 5.0.',
__METHOD__
), \E_USER_DEPRECATED);

$value = null;

if (null === $object) {
Expand Down
5 changes: 3 additions & 2 deletions src/Security/Handler/AclSecurityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
use Symfony\Component\Security\Acl\Permission\MaskBuilderInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
Expand Down Expand Up @@ -64,13 +65,13 @@ final class AclSecurityHandler implements AclSecurityHandlerInterface

/**
* @var string
* @phpstan-var class-string
* @phpstan-var class-string<MaskBuilderInterface>
*/
private $maskBuilderClass;

/**
* @param string[] $superAdminRoles
* @phpstan-param class-string $maskBuilderClass
* @phpstan-param class-string<MaskBuilderInterface> $maskBuilderClass
*/
public function __construct(
TokenStorageInterface $tokenStorage,
Expand Down
5 changes: 4 additions & 1 deletion src/Twig/Extension/GroupExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* @phpstan-import-type Item from \Sonata\AdminBundle\Admin\Pool
*/
final class GroupExtension extends AbstractExtension
{
/**
Expand Down Expand Up @@ -48,7 +51,7 @@ public function getFunctions(): array
* label: string,
* label_catalogue: string,
* icon: string,
* item_adds: array,
* item_adds: Item[],
* items: array<AdminInterface<object>>,
* keep_open: bool,
* on_top: bool,
Expand Down
5 changes: 3 additions & 2 deletions src/Util/AdminAclManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Acl\Permission\MaskBuilderInterface;

/**
* @author Thomas Rabaix <[email protected]>
Expand All @@ -28,12 +29,12 @@ final class AdminAclManipulator implements AdminAclManipulatorInterface
/**
* @var string
*
* @phpstan-var class-string
* @phpstan-var class-string<MaskBuilderInterface>
*/
private $maskBuilderClass;

/**
* @phpstan-param class-string $maskBuilderClass
* @phpstan-param class-string<MaskBuilderInterface> $maskBuilderClass
*/
public function __construct(string $maskBuilderClass)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Util/AdminObjectAclManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
use Symfony\Component\Security\Acl\Permission\MaskBuilderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
Expand All @@ -45,12 +46,12 @@ final class AdminObjectAclManipulator
/**
* @var string
*
* @phpstan-var class-string
* @phpstan-var class-string<MaskBuilderInterface>
*/
private $maskBuilderClass;

/**
* @phpstan-param class-string $maskBuilderClass
* @phpstan-param class-string<MaskBuilderInterface> $maskBuilderClass
*/
public function __construct(FormFactoryInterface $formFactory, string $maskBuilderClass)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Util/FormBuilderIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-extends \RecursiveArrayIterator<string|int, FormBuilderInterface>
*/
final class FormBuilderIterator extends \RecursiveArrayIterator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Util/FormViewIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-implements \RecursiveIterator<string, FormView>
*/
final class FormViewIterator implements \RecursiveIterator
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Sonata\AdminBundle\Model\AuditReaderInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Request\AdminFetcherInterface;
use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap;
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController;
Expand Down Expand Up @@ -62,6 +61,7 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
Expand Down Expand Up @@ -192,7 +192,7 @@ protected function setUp(): void

$this->formFactory = $this->createStub(FormFactoryInterface::class);

$this->adminObjectAclManipulator = new AdminObjectAclManipulator($this->formFactory, AdminPermissionMap::class);
$this->adminObjectAclManipulator = new AdminObjectAclManipulator($this->formFactory, MaskBuilder::class);

$this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class)
->getMock();
Expand Down
Loading

0 comments on commit 0ccf024

Please sign in to comment.