Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.13.1@afd8874a9e4562eac42a02de90e42e430c3a1db1">
<file src="src/DependencyInjection/Compiler/ExtensionCompilerPass.php">
<InvalidReturnStatement occurrences="1">
<code>$extensionMap</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="1">
<code>array&lt;string, array&lt;string, array&lt;string, array&lt;string, array&lt;string, string&gt;&gt;&gt;&gt;&gt;</code>
</InvalidReturnType>
</file>
<!-- The next error is due to not installed Doctrine ORM\ODM -->
<file src="src/Admin/AdminHelper.php">
<UndefinedClass occurrences="1">
<code>DoctrinePersistentCollection</code>
</UndefinedClass>
</file>
<file src="src/Datagrid/Datagrid.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>array_merge(array_flip($keys), $this-&gt;filters)</code>
Expand All @@ -25,6 +11,12 @@
<code>array_merge(array_flip($keys), $this-&gt;elements)</code>
</InvalidPropertyAssignmentValue>
</file>
<!-- Will be fixed in Symfony 5.3 https://github.com/symfony/symfony/pull/40604 -->
<file src="src/Translator/Extractor/AdminExtractor.php">
<MoreSpecificImplementedParamType occurrences="1">
<code>$resource</code>
</MoreSpecificImplementedParamType>
</file>
<!-- This error is made on purpose for php version < 8 -->
<file src="tests/Fixtures/Entity/FooToStringNull.php">
<InvalidToString occurrences="1">
Expand All @@ -34,10 +26,4 @@
<code>null</code>
</NullableReturnStatement>
</file>
<!-- Will be fixed in Symfony 5.3 https://github.com/symfony/symfony/pull/40604 -->
<file src="src/Translator/Extractor/AdminExtractor.php">
<MoreSpecificImplementedParamType occurrences="1">
<code>$resource</code>
</MoreSpecificImplementedParamType>
</file>
</files>
17 changes: 11 additions & 6 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ final public function getDataSourceIterator(): SourceIteratorInterface
final public function initialize(): void
{
if (!$this->classnameLabel) {
$this->classnameLabel = substr($this->getClass(), strrpos($this->getClass(), '\\') + 1);
$namespaceSeparatorPos = strrpos($this->getClass(), '\\');
$this->classnameLabel = false !== $namespaceSeparatorPos
? substr($this->getClass(), $namespaceSeparatorPos + 1)
: $this->getClass();
}

$this->configure();
Expand Down Expand Up @@ -508,8 +511,9 @@ final public function getFilterParameters(): array
$parameters = ParametersManipulator::merge($parameters, $filters);

// always force the parent value
if ($this->isChild() && null !== $this->getParentAssociationMapping()) {
$name = str_replace('.', '__', $this->getParentAssociationMapping());
$parentAssociationMapping = $this->getParentAssociationMapping();
if ($this->isChild() && null !== $parentAssociationMapping) {
$name = str_replace('.', '__', $parentAssociationMapping);
$parameters[$name] = ['value' => $this->getRequest()->get($this->getParent()->getIdParameter())];
}
}
Expand Down Expand Up @@ -2296,12 +2300,13 @@ private function buildDatagrid(): ?DatagridInterface
$this->configureDatagridFilters($mapper);

// ok, try to limit to add parent filter
$parentAssociationMapping = $this->getParentAssociationMapping();
if (
$this->isChild()
&& null !== $this->getParentAssociationMapping()
&& !$mapper->has($this->getParentAssociationMapping())
&& null !== $parentAssociationMapping
&& !$mapper->has($parentAssociationMapping)
) {
$mapper->add($this->getParentAssociationMapping(), null, [
$mapper->add($parentAssociationMapping, null, [
'show_filter' => false,
'label' => false,
'field_type' => ModelHiddenType::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/AdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function appendFormFieldElement(AdminInterface $admin, object $subject, s
$finalForm->setData($form->getData());

// back up delete field
if (\count($toDelete) > 0) {
if ($childFormBuilder && \count($toDelete) > 0) {
$i = 0;
foreach ($finalForm->get($childFormBuilder->getName()) as $childField) {
if ($childField->has(self::FORM_FIELD_DELETE)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Extension/LockExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function configureFormFields(FormMapper $form): void
$data = $event->getData();
$form = $event->getForm();

if (null === $data || $form->getParent()) {
if (!\is_object($data) || $form->getParent()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/UrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function generateUrl(string $name, array $parameters = [], int $reference
* @param array<string, mixed> $parameters
*
* @return array<string, mixed>
* @phpstan-return array{route: string, routeParameters: array<string, string>, routeAbsolute: bool}
* @phpstan-return array{route: string, routeParameters: array<string, mixed>, routeAbsolute: bool}
*/
public function generateMenuUrl(string $name, array $parameters = [], int $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH): array;

Expand Down
5 changes: 4 additions & 1 deletion src/Block/AdminStatsBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function __construct(Environment $twig, Pool $pool)

public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
$template = $blockContext->getTemplate();
\assert(null !== $template);

$admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('code'));

$datagrid = $admin->getDatagrid();
Expand All @@ -55,7 +58,7 @@ public function execute(BlockContextInterface $blockContext, ?Response $response

$datagrid->buildPager();

return $this->renderPrivateResponse($blockContext->getTemplate(), [
return $this->renderPrivateResponse($template, [
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
'admin' => $admin,
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Exporter/AdminExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public function getAvailableFormats(AdminInterface $admin): array
public function getExportFilename(AdminInterface $admin, string $format): string
{
$class = $admin->getClass();
$namespaceSeparatorPos = strripos($class, '\\');

return sprintf(
'export_%s_%s.%s',
strtolower(substr($class, strripos($class, '\\') + 1)),
strtolower(false !== $namespaceSeparatorPos ? substr($class, $namespaceSeparatorPos + 1) : $class),
date('Y_m_d_H_i_s', strtotime('now')),
$format
);
Expand Down
12 changes: 6 additions & 6 deletions src/Command/ExplainAdminCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(sprintf(
' - % -25s % -15s % -15s',
$name,
$fieldDescription->getType(),
$fieldDescription->getTemplate()
$fieldDescription->getType() ?? '',
$fieldDescription->getTemplate() ?? ''
));
}

Expand All @@ -94,8 +94,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(sprintf(
' - % -25s % -15s % -15s',
$name,
$fieldDescription->getType(),
$fieldDescription->getTemplate()
$fieldDescription->getType() ?? '',
$fieldDescription->getTemplate() ?? ''
));
}

Expand All @@ -111,8 +111,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(sprintf(
' - % -25s % -15s % -15s',
$name,
$fieldDescription->getType(),
$fieldDescription->getTemplate()
$fieldDescription->getType() ?? '',
$fieldDescription->getTemplate() ?? ''
));
}

Expand Down
6 changes: 4 additions & 2 deletions src/Command/GenerateObjectAclCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ public function execute(InputInterface $input, OutputInterface $output): int

$securityIdentity = new UserSecurityIdentity($username, $this->getUserModelClass($input, $output));
}
if (!$input->getOption('step') && $input->getOption('object_owner')) {
$securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserModelClass($input, $output));

$objectOwner = $input->getOption('object_owner');
if (!$input->getOption('step') && $objectOwner) {
$securityIdentity = new UserSecurityIdentity($objectOwner, $this->getUserModelClass($input, $output));
}

$manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType());
Expand Down
2 changes: 2 additions & 0 deletions src/Command/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static function validateUsername(?string $username): string

/**
* @throws \InvalidArgumentException
*
* @phpstan-return class-string
*/
public static function validateClass(string $class): string
{
Expand Down
45 changes: 33 additions & 12 deletions src/DependencyInjection/Compiler/ExtensionCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function process(ContainerBuilder $container): void
}

/**
* @param array<string, array<string, array<string, array<string, array<string, mixed>>>>> $extensionMap
* @param array<string, array<string, array<string, array<string, mixed>>>> $extensionMap
*/
private function getExtensionsForAdmin(string $id, Definition $admin, ContainerBuilder $container, array $extensionMap): array
{
Expand Down Expand Up @@ -156,19 +156,29 @@ private function getManagedClass(Definition $admin, ContainerBuilder $container)
}

/**
* @param array<string, array<string, array<string, string>|bool>> $config
* @param array<string, array<string, array<string, string>|int|bool>> $config
*
* @return array<string, array<string, array<string, array<string, array<string, string>>>>> an array with the following structure.
* @return array<string, array<string, array<string, array<string, int>>>> an array with the following structure
*
* [
* 'global' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]],
* 'excludes' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]],
* 'admins' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]],
* 'implements' => ['<interface>' => ['<extension_id>' => ['priority' => <int>]]],
* 'extends' => ['<class>' => ['<extension_id>' => ['priority' => <int>]]],
* 'instanceof' => ['<class>' => ['<extension_id>' => ['priority' => <int>]]],
* 'uses' => ['<trait>' => ['<extension_id>' => ['priority' => <int>]]],
* ]
* @phpstan-param array<string, array{
* global: bool,
* excludes: array<string, string>,
* admins: array<string, string>,
* implements: array<class-string, string>,
* extends: array<class-string, string>,
* instanceof: array<class-string, string>,
* uses: array<class-string, string>,
* priority: int,
* }> $config
* @phpstan-return array{
* global: array<string, array<string, array{priority: int}>>,
* excludes: array<string, array<string, array{priority: int}>>,
* admins: array<string, array<string, array{priority: int}>>,
* implements: array<string, array<class-string, array{priority: int}>>,
* extends: array<string, array<class-string, array{priority: int}>>,
* instanceof: array<string, array<class-string, array{priority: int}>>,
* uses: array<string, array<class-string, array{priority: int}>>,
* }
*/
private function flattenExtensionConfiguration(array $config): array
{
Expand All @@ -189,6 +199,17 @@ private function flattenExtensionConfiguration(array $config): array
$options['global'] = [];
}

/**
* @phpstan-var array{
* global: array<string, string>,
* excludes: array<string, string>,
* admins: array<string, string>,
* implements: array<class-string, string>,
* extends: array<class-string, string>,
* instanceof: array<class-string, string>,
* uses: array<class-string, string>,
* } $optionsMap
*/
$optionsMap = array_intersect_key($options, $extensionMap);

foreach ($optionsMap as $key => $value) {
Expand Down
8 changes: 5 additions & 3 deletions src/FieldDescription/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
protected $mappingType;

/**
* @var string|null the field name (of the form)
* @var string the field name (of the form)
*/
protected $fieldName;

Expand All @@ -105,6 +105,8 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface

/**
* @var array<string, mixed> the option collection
* @psalm-var FieldDescriptionOptions
* @phpstan-var array<string, mixed>
*/
protected $options = [];

Expand Down Expand Up @@ -158,7 +160,7 @@ public function __construct(
}
}

public function getFieldName(): ?string
public function getFieldName(): string
{
return $this->fieldName;
}
Expand Down Expand Up @@ -388,7 +390,7 @@ abstract protected function setParentAssociationMappings(array $parentAssociatio
*
* @return mixed
*/
protected function getFieldValue(?object $object, ?string $fieldName)
protected function getFieldValue(?object $object, string $fieldName)
{
if ($this->isVirtual() || null === $object) {
return null;
Expand Down
11 changes: 5 additions & 6 deletions src/FieldDescription/FieldDescriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* identifier?: bool,
* inline?: string,
* label?: bool|string|null,
* link_parameters?: array,
* link_parameters?: array<string, mixed>,
* multiple?: bool,
* placeholder?: string,
* required?: bool,
Expand All @@ -53,7 +53,7 @@
* translation_domain?: string,
* type?: string,
* virtual_field?: bool
* }
* }&array<string, mixed>|array<empty, empty>
*/
interface FieldDescriptionInterface
{
Expand Down Expand Up @@ -81,10 +81,8 @@ interface FieldDescriptionInterface

/**
* Returns the field name.
*
* @return string|null the field name
*/
public function getFieldName(): ?string;
public function getFieldName(): string;

public function setName(string $name): void;

Expand Down Expand Up @@ -255,7 +253,8 @@ public function mergeOption(string $name, array $options = []): void;
/**
* Merge options values.
*
* @param array<string, mixed> $options
* @psalm-param FieldDescriptionOptions $options
* @phpstan-param array<string, mixed> $options
*/
public function mergeOptions(array $options = []): void;

Expand Down
6 changes: 5 additions & 1 deletion src/Form/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* This class is use to simulate the Form API.
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* @psalm-import-type FieldDescriptionOptions from \Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface
*/
final class FormMapper extends BaseGroupedMapper
{
Expand Down Expand Up @@ -67,9 +69,11 @@ public function reorder(array $keys): self
/**
* @param FormBuilderInterface|string $name
* @param array<string, mixed> $options
* @param array<string, mixed> $fieldDescriptionOptions
*
* @return static
*
* @psalm-param FieldDescriptionOptions $fieldDescriptionOptions
* @phpstan-param array<string, mixed> $fieldDescriptionOptions
*/
public function add($name, ?string $type = null, array $options = [], array $fieldDescriptionOptions = []): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/Maker/AdminMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ final class AdminMaker extends AbstractMaker

/**
* @var string
* @phpstan-var class-string
*/
private $modelClass;

Expand All @@ -65,7 +66,7 @@ final class AdminMaker extends AbstractMaker
private $adminClassBasename;

/**
* @var string
* @var string|null
*/
private $controllerClassBasename;

Expand Down
4 changes: 3 additions & 1 deletion src/Route/DefaultRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public function generateMenuUrl(

// if the admin is linked to a parent FieldDescription (ie, embedded widget)
if ($admin->hasParentFieldDescription()) {
/** @var array<string, mixed> $linkParameters */
$linkParameters = $admin->getParentFieldDescription()->getOption('link_parameters', []);
// merge link parameter if any provided by the parent field
$parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', []));
$parameters = array_merge($parameters, $linkParameters);

$parameters['uniqid'] = $admin->getUniqid();
$parameters['code'] = $admin->getCode();
Expand Down
Loading