Skip to content

Commit

Permalink
Fix phpstan level 4 on master
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 10, 2020
1 parent 3a4c36a commit ece51a9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/Action/RetrieveAutocompleteItemsAction.php
Expand Up @@ -184,12 +184,12 @@ private function retrieveFilterFieldDescription(
AdminInterface $admin,
string $field
): FieldDescriptionInterface {
$fieldDescription = $admin->getFilterFieldDescription($field);

if (!$fieldDescription) {
if (!$admin->hasFilterFieldDescription($field)) {
throw new \RuntimeException(sprintf('The field "%s" does not exist.', $field));
}

$fieldDescription = $admin->getFilterFieldDescription($field);

if (null === $fieldDescription->getTargetModel()) {
throw new \RuntimeException(sprintf('No associated entity with field "%s".', $field));
}
Expand All @@ -206,12 +206,12 @@ private function retrieveFormFieldDescription(
AdminInterface $admin,
string $field
): FieldDescriptionInterface {
$fieldDescription = $admin->getFormFieldDescription($field);

if (!$fieldDescription) {
if (!$admin->hasFormFieldDescription($field)) {
throw new \RuntimeException(sprintf('The field "%s" does not exist.', $field));
}

$fieldDescription = $admin->getFormFieldDescription($field);

if (null === $fieldDescription->getTargetModel()) {
throw new \RuntimeException(sprintf('No associated entity with field "%s".', $field));
}
Expand Down
8 changes: 4 additions & 4 deletions src/Action/SetObjectFieldValueAction.php
Expand Up @@ -86,16 +86,16 @@ public function __invoke(Request $request): JsonResponse
return new JsonResponse('Invalid permissions', Response::HTTP_FORBIDDEN);
}

if ('list' === $context) {
$fieldDescription = $admin->getListFieldDescription($field);
} else {
if ('list' !== $context) {
return new JsonResponse('Invalid context', Response::HTTP_BAD_REQUEST);
}

if (!$fieldDescription) {
if (!$admin->hasListFieldDescription($field)) {
return new JsonResponse('The field does not exist', Response::HTTP_BAD_REQUEST);
}

$fieldDescription = $admin->getListFieldDescription($field);

if (!$fieldDescription->getOption('editable')) {
return new JsonResponse('The field cannot be edited, editable option must be set to true', Response::HTTP_BAD_REQUEST);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Admin/AbstractAdmin.php
Expand Up @@ -1013,15 +1013,17 @@ public function isCurrentRoute(string $name, ?string $adminCode = null): bool
$route = $request->get('_route');

if ($adminCode) {
$admin = $this->getConfigurationPool()->getAdminByAdminCode($adminCode);
$pool = $this->getConfigurationPool();

if ($pool->hasAdminByAdminCode($adminCode)) {
$admin = $pool->getAdminByAdminCode($adminCode);
} else {
return false;
}
} else {
$admin = $this;
}

if (!$admin) {
return false;
}

return sprintf('%s_%s', $admin->getBaseRouteName(), $name) === $route;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Action/RetrieveAutocompleteItemsActionTest.php
Expand Up @@ -92,6 +92,7 @@ public function testRetrieveAutocompleteItemsActionDisabledFormelememt(): void
$this->admin->setSubject($object)->shouldBeCalled();
$this->admin->hasAccess('create')->willReturn(true);
$this->admin->getFormFieldDescriptions()->willReturn([]);
$this->admin->hasFormFieldDescription('barField')->willReturn(true);
$this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal());

$fieldDescription->getTargetModel()->willReturn(Foo::class);
Expand All @@ -117,6 +118,7 @@ public function testRetrieveAutocompleteItemsTooShortSearchString(): void
$this->admin->getNewInstance()->willReturn($object);
$this->admin->setSubject($object)->shouldBeCalled();
$this->admin->hasAccess('create')->willReturn(true);
$this->admin->hasFormFieldDescription('barField')->willReturn(true);
$this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal());
$this->admin->getFormFieldDescriptions()->willReturn([]);
$targetAdmin->checkAccess('list')->shouldBeCalled();
Expand Down Expand Up @@ -226,6 +228,7 @@ private function configureAutocompleteItemsDatagrid(): ObjectProphecy
$this->admin->getNewInstance()->willReturn($model);
$this->admin->setSubject($model)->shouldBeCalled();
$this->admin->hasAccess('create')->willReturn(true);
$this->admin->hasFormFieldDescription('barField')->willReturn(true);
$this->admin->getFormFieldDescription('barField')->willReturn($fieldDescription->reveal());
$this->admin->getFormFieldDescriptions()->willReturn([]);
$this->admin->id($model)->willReturn(123);
Expand Down
5 changes: 5 additions & 0 deletions tests/Action/SetObjectFieldValueActionTest.php
Expand Up @@ -100,6 +100,7 @@ public function testSetObjectFieldValueAction(): void
$this->admin->getObject(42)->willReturn($object);
$this->admin->getCode()->willReturn('sonata.post.admin');
$this->admin->hasAccess('edit', $object)->willReturn(true);
$this->admin->hasListFieldDescription('enabled')->willReturn(true);
$this->admin->getListFieldDescription('enabled')->willReturn($fieldDescription->reveal());
$this->admin->update($object)->shouldBeCalled();
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
Expand Down Expand Up @@ -162,6 +163,7 @@ public function testSetObjectFieldValueActionWithDate($timezone, \DateTimeZone $
$this->admin->getObject(42)->willReturn($object);
$this->admin->getCode()->willReturn('sonata.post.admin');
$this->admin->hasAccess('edit', $object)->willReturn(true);
$this->admin->hasListFieldDescription('dateProp')->willReturn(true);
$this->admin->getListFieldDescription('dateProp')->willReturn($fieldDescription->reveal());
$this->admin->update($object)->shouldBeCalled();

Expand Down Expand Up @@ -217,6 +219,7 @@ public function testSetObjectFieldValueActionOnARelationField(): void
$this->admin->getObject(42)->willReturn($object);
$this->admin->getCode()->willReturn('sonata.post.admin');
$this->admin->hasAccess('edit', $object)->willReturn(true);
$this->admin->hasListFieldDescription('bar')->willReturn(true);
$this->admin->getListFieldDescription('bar')->willReturn($fieldDescription->reveal());
$this->admin->getClass()->willReturn(\get_class($object));
$this->admin->update($object)->shouldBeCalled();
Expand Down Expand Up @@ -264,6 +267,7 @@ public function testSetObjectFieldValueActionWithViolations(): void
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
$this->admin->getObject(42)->willReturn($object);
$this->admin->hasAccess('edit', $object)->willReturn(true);
$this->admin->hasListFieldDescription('bar.enabled')->willReturn(true);
$this->admin->getListFieldDescription('bar.enabled')->willReturn($fieldDescription->reveal());
$this->validator->validate($bar)->willReturn(new ConstraintViolationList([
new ConstraintViolation('error1', null, [], null, 'enabled', null),
Expand Down Expand Up @@ -300,6 +304,7 @@ public function testSetObjectFieldEditableMultipleValue(): void
$this->admin->getObject(42)->willReturn($object);
$this->admin->getCode()->willReturn('sonata.post.admin');
$this->admin->hasAccess('edit', $object)->willReturn(true);
$this->admin->hasListFieldDescription('status')->willReturn(true);
$this->admin->getListFieldDescription('status')->willReturn($fieldDescription->reveal());
$this->admin->update($object)->shouldBeCalled();
$templateRegistry->getTemplate('base_list_field')->willReturn('admin_template');
Expand Down

0 comments on commit ece51a9

Please sign in to comment.