Skip to content

Commit

Permalink
bump PHPStan to level 5
Browse files Browse the repository at this point in the history
  • Loading branch information
vladyslavstartsev authored and greg0ire committed Aug 18, 2020
1 parent e0c1537 commit 65d2bbb
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .phpstan/phpstan-baseline.neon
Expand Up @@ -229,6 +229,12 @@ parameters:
count: 1
path: ../src/Block/AdminStatsBlockService.php

-
# will be fixed in v4. Currently BC break
message: "#^Parameter \\#1 \\$resolver of method .+(Type|Extension)\\:\\:configureOptions\\(\\) expects Symfony\\\\Component\\\\OptionsResolver\\\\OptionsResolver, Symfony\\\\Component\\\\OptionsResolver\\\\OptionsResolverInterface given\\.$#"
count: 17
path: ../src/Form/*

-
# will be fixed in v4. Currently BC break
message: "#^Parameter \\$resolver of method Sonata\\\\AdminBundle\\\\Form\\\\Extension\\\\Field\\\\Type\\\\FormTypeFieldExtension\\:\\:setDefaultOptions\\(\\) has invalid typehint type Symfony\\\\Component\\\\OptionsResolver\\\\OptionsResolverInterface\\.$#"
Expand Down
2 changes: 1 addition & 1 deletion .phpstan/phpstan.neon.dist
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 4
level: 5
paths:
- ../src
excludes_analyse:
Expand Down
7 changes: 1 addition & 6 deletions src/Admin/AbstractAdmin.php
Expand Up @@ -2692,12 +2692,7 @@ public function getPermissionsShow($context)

public function showIn($context)
{
switch ($context) {
case self::CONTEXT_DASHBOARD:
case self::CONTEXT_MENU:
default:
return $this->isGranted($this->getPermissionsShow($context));
}
return $this->isGranted($this->getPermissionsShow($context));
}

public function createObjectSecurity($object)
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/AdminInterface.php
Expand Up @@ -253,8 +253,8 @@ public function setSecurityHandler(SecurityHandlerInterface $securityHandler);
public function getSecurityHandler();

/**
* @param string $name
* @param object|null $object
* @param string|array $name
* @param object|null $object
*
* @return bool
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Admin/BaseFieldDescription.php
Expand Up @@ -556,7 +556,9 @@ private function callCachedGetter(object $object, string $fieldName, array $para
$getterKey = $this->getFieldGetterKey($object, $fieldName);
if ('getter' === self::$fieldGetters[$getterKey]['method']) {
return $object->{self::$fieldGetters[$getterKey]['getter']}(...$parameters);
} elseif ('call' === self::$fieldGetters[$getterKey]['method']) {
}

if ('call' === self::$fieldGetters[$getterKey]['method']) {
return $object->{$fieldName}(...$parameters);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/BreadcrumbsBuilder.php
Expand Up @@ -121,7 +121,7 @@ public function buildBreadcrumbs(
}

if ('list' === $action) {
$menu->setUri(false);
$menu->setUri('');

return $menu;
}
Expand Down
47 changes: 47 additions & 0 deletions src/Security/Handler/AclSecurityHandler.php
Expand Up @@ -165,6 +165,8 @@ public function getObjectAcl(ObjectIdentityInterface $objectIdentity)
{
try {
$acl = $this->aclProvider->findAcl($objectIdentity);
// todo - remove `assert` statement after https://github.com/phpstan/phpstan-symfony/pull/92 is released
\assert($acl instanceof MutableAclInterface);
} catch (AclNotFoundException $e) {
return null;
}
Expand All @@ -185,8 +187,16 @@ public function findObjectAcls(\Traversable $oids, array $sids = [])
return $acls;
}

/**
* NEXT_MAJOR: change signature to `addObjectOwner(MutableAclInterface $acl, ?UserSecurityIdentity $securityIdentity = null): void`.
*
* @param MutableAclInterface $acl
*
* @return void
*/
public function addObjectOwner(AclInterface $acl, ?UserSecurityIdentity $securityIdentity = null)
{
// NEXT_MAJOR: remove `if` condition
if (!$acl instanceof MutableAclInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must implement "%s".',
Expand All @@ -200,8 +210,18 @@ public function addObjectOwner(AclInterface $acl, ?UserSecurityIdentity $securit
}
}

/**
* Add the object class ACE's to the object ACL.
*
* NEXT_MAJOR: change signature to `addObjectClassAces(MutableAclInterface $acl, array $roleInformation = []): void`.
*
* @param MutableAclInterface $acl
*
* @return void
*/
public function addObjectClassAces(AclInterface $acl, array $roleInformation = [])
{
// NEXT_MAJOR: remove `assert` statement
\assert($acl instanceof MutableAclInterface);
$builder = new $this->maskBuilderClass();

Expand Down Expand Up @@ -231,13 +251,26 @@ public function addObjectClassAces(AclInterface $acl, array $roleInformation = [
}
}

/**
* NEXT_MAJOR: change signature to `createAcl(ObjectIdentityInterface $objectIdentity): MutableAclInterface`.
*
* @return MutableAclInterface
*/
public function createAcl(ObjectIdentityInterface $objectIdentity)
{
return $this->aclProvider->createAcl($objectIdentity);
}

/**
* NEXT_MAJOR: change signature to `updateAcl(MutableAclInterface $acl): void`.
*
* @param MutableAclInterface $acl
*
* @@return void
*/
public function updateAcl(AclInterface $acl)
{
// NEXT_MAJOR: remove `assert` statement
\assert($acl instanceof MutableAclInterface);
$this->aclProvider->updateAcl($acl);
}
Expand All @@ -247,6 +280,13 @@ public function deleteAcl(ObjectIdentityInterface $objectIdentity)
$this->aclProvider->deleteAcl($objectIdentity);
}

/**
* NEXT_MAJOR: change signature to `findClassAceIndexByRole(MutableAclInterface $acl, string $role): int|string|false`.
*
* @param MutableAclInterface $acl
*
* @return array-key|false
*/
public function findClassAceIndexByRole(AclInterface $acl, $role)
{
foreach ($acl->getClassAces() as $index => $entry) {
Expand All @@ -258,6 +298,13 @@ public function findClassAceIndexByRole(AclInterface $acl, $role)
return false;
}

/**
* NEXT_MAJOR: change signature to `findClassAceIndexByUsername(MutableAclInterface $acl, string $username): int|string|false`.
*
* @param MutableAclInterface $acl
*
* @return array-key|false
*/
public function findClassAceIndexByUsername(AclInterface $acl, $username)
{
foreach ($acl->getClassAces() as $index => $entry) {
Expand Down
33 changes: 29 additions & 4 deletions src/Security/Handler/AclSecurityHandlerInterface.php
Expand Up @@ -15,6 +15,7 @@

use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Model\AclInterface;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ public function getObjectPermissions();
/**
* Get the ACL for the passed object identity.
*
* @return AclInterface|null or NULL if not found
* @return MutableAclInterface|null
*/
public function getObjectAcl(ObjectIdentityInterface $objectIdentity);

Expand All @@ -67,23 +68,43 @@ public function findObjectAcls(\Traversable $oids, array $sids = []);

/**
* Add an object owner ACE to the object ACL.
*
* NEXT_MAJOR: change signature to `addObjectOwner(MutableAclInterface $acl, ?UserSecurityIdentity $securityIdentity = null): void`.
*
* @param MutableAclInterface $acl
*
* @return void
*/
public function addObjectOwner(AclInterface $acl, ?UserSecurityIdentity $securityIdentity = null);

/**
* Add the object class ACE's to the object ACL.
*
* NEXT_MAJOR: change signature to `addObjectClassAces(MutableAclInterface $acl, array $roleInformation = []): void`.
*
* @param MutableAclInterface $acl
*
* @return void
*/
public function addObjectClassAces(AclInterface $acl, array $roleInformation = []);

/**
* Create an object ACL.
*
* @return AclInterface
* NEXT_MAJOR: change signature to `createAcl(ObjectIdentityInterface $objectIdentity): MutableAclInterface`
*
* @return MutableAclInterface
*/
public function createAcl(ObjectIdentityInterface $objectIdentity);

/**
* Update the ACL.
*
* NEXT_MAJOR: change signature to `updateAcl(MutableAclInterface $acl): void`
*
* @param MutableAclInterface $acl
*
* @return void
*/
public function updateAcl(AclInterface $acl);

Expand All @@ -95,18 +116,22 @@ public function deleteAcl(ObjectIdentityInterface $objectIdentity);
/**
* Helper method to find the index of a class ACE for a role.
*
* NEXT_MAJOR: change signature to `findClassAceIndexByRole(MutableAclInterface $acl, string $role): array-key|false`
*
* @param string $role
*
* @return mixed index if found, FALSE if not found
* @return array-key|false index if found, FALSE if not found
*/
public function findClassAceIndexByRole(AclInterface $acl, $role);

/**
* Helper method to find the index of a class ACE for a username.
*
* NEXT_MAJOR: change signature to `findClassAceIndexByUsername(MutableAclInterface $acl, string $username): array-key|false`
*
* @param string $username
*
* @return mixed index if found, FALSE if not found
* @return array-key|false index if found, FALSE if not found
*/
public function findClassAceIndexByUsername(AclInterface $acl, $username);
}
4 changes: 3 additions & 1 deletion src/Twig/Extension/StringExtension.php
Expand Up @@ -80,7 +80,9 @@ public function deprecatedTruncate(Environment $env, ?string $text, int $length

if ($this->legacyExtension instanceof TextExtension) {
return twig_truncate_filter($env, $text, $length, $preserve, $ellipsis);
} elseif ($this->legacyExtension instanceof DeprecatedTextExtension) {
}

if ($this->legacyExtension instanceof DeprecatedTextExtension) {
return $this->legacyExtension->twigTruncateFilter($env, $text, $length, $preserve, $ellipsis);
}

Expand Down
22 changes: 12 additions & 10 deletions src/Util/AdminObjectAclData.php
Expand Up @@ -16,7 +16,9 @@
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Security\Acl\Domain\Acl;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;

/**
* AdminObjectAclData holds data manipulated by {@link AdminObjectAclManipulator}.
Expand Down Expand Up @@ -58,17 +60,17 @@ class AdminObjectAclData
protected $masks = [];

/**
* @var Form
* @var FormInterface
*/
protected $aclUsersForm;

/**
* @var Form
* @var FormInterface
*/
protected $aclRolesForm;

/**
* @var Acl
* @var MutableAclInterface
*/
protected $acl;

Expand Down Expand Up @@ -145,7 +147,7 @@ public function getAclRoles()
*
* @return AdminObjectAclData
*/
public function setAcl(Acl $acl)
public function setAcl(MutableAclInterface $acl)
{
$this->acl = $acl;

Expand All @@ -155,7 +157,7 @@ public function setAcl(Acl $acl)
/**
* Gets ACL.
*
* @return Acl
* @return MutableAclInterface
*/
public function getAcl()
{
Expand Down Expand Up @@ -196,7 +198,7 @@ public function setForm(Form $form)
*
* NEXT_MAJOR: remove this method.
*
* @return Form
* @return FormInterface
*
* @deprecated since sonata-project/admin-bundle version 3.0. Use getAclUsersForm() instead
*/
Expand All @@ -215,7 +217,7 @@ public function getForm()
*
* @return AdminObjectAclData
*/
public function setAclUsersForm(Form $form)
public function setAclUsersForm(FormInterface $form)
{
$this->aclUsersForm = $form;

Expand All @@ -225,7 +227,7 @@ public function setAclUsersForm(Form $form)
/**
* Gets ACL users form.
*
* @return Form
* @return FormInterface
*/
public function getAclUsersForm()
{
Expand All @@ -237,7 +239,7 @@ public function getAclUsersForm()
*
* @return AdminObjectAclData
*/
public function setAclRolesForm(Form $form)
public function setAclRolesForm(FormInterface $form)
{
$this->aclRolesForm = $form;

Expand All @@ -247,7 +249,7 @@ public function setAclRolesForm(Form $form)
/**
* Gets ACL roles form.
*
* @return Form
* @return FormInterface
*/
public function getAclRolesForm()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Util/AdminObjectAclManipulator.php
Expand Up @@ -157,7 +157,7 @@ public function updateAcl(AdminObjectAclData $data)
/**
* Builds ACL.
*/
protected function buildAcl(AdminObjectAclData $data, Form $form, \Traversable $aclValues)
protected function buildAcl(AdminObjectAclData $data, FormInterface $form, \Traversable $aclValues)
{
$masks = $data->getMasks();
$acl = $data->getAcl();
Expand Down
4 changes: 2 additions & 2 deletions tests/Admin/BreadcrumbsBuilderTest.php
Expand Up @@ -115,7 +115,7 @@ public function testGetBreadcrumbs(): void

$menu->expects($this->once())
->method('setUri')
->with($this->identicalTo(false));
->with($this->identicalTo(''));

$menu->expects($this->exactly(5))
->method('getParent')
Expand Down Expand Up @@ -492,7 +492,7 @@ public function testUnitBuildBreadcrumbs(string $action): void
);
if ('list' === $action) {
$admin->isChild()->willReturn(true);
$menu->setUri(false)->shouldBeCalled();
$menu->setUri('')->shouldBeCalled();
} else {
$menu->setUri()->shouldNotBeCalled();
}
Expand Down

0 comments on commit 65d2bbb

Please sign in to comment.