Applied symplify rules to codebase.

This commit is contained in:
Jan Böhmer 2020-01-05 22:49:00 +01:00
parent 2f20d90041
commit 388e847b17
136 changed files with 1370 additions and 789 deletions

View file

@ -25,9 +25,11 @@ declare(strict_types=1);
namespace App\Security\Annotations;
use App\Entity\Base\NamedDBElement;
use DateTime;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Collections\ArrayCollection;
use InvalidArgumentException;
use function is_string;
/**
* @Annotation
@ -90,7 +92,7 @@ class ColumnSecurity
if (class_exists($this->type)) {
$object = new $this->type();
if ($object instanceof NamedDBElement) {
if (\is_string($this->placeholder) && '' !== $this->placeholder) {
if (is_string($this->placeholder) && '' !== $this->placeholder) {
$object->setName($this->placeholder);
} else {
$object->setName('???');
@ -117,7 +119,7 @@ class ColumnSecurity
case 'bool':
return false;
case 'datetime':
return (new \DateTime())->setTimestamp(0);
return (new DateTime())->setTimestamp(0);
default:
throw new InvalidArgumentException('Unknown type! You have to specify a placeholder!');
}

View file

@ -27,6 +27,7 @@ namespace App\Security\EntityListeners;
use App\Entity\Base\DBElement;
use App\Entity\UserSystem\User;
use App\Security\Annotations\ColumnSecurity;
use function count;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\EntityManagerInterface;
@ -34,6 +35,8 @@ use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\PostLoad;
use Doctrine\ORM\Mapping\PreUpdate;
use function get_class;
use InvalidArgumentException;
use ReflectionClass;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Security;
@ -167,7 +170,7 @@ class ElementPermissionListener
*/
protected function isRunningFromCLI()
{
if (empty($_SERVER['REMOTE_ADDR']) && ! isset($_SERVER['HTTP_USER_AGENT']) && \count($_SERVER['argv']) > 0) {
if (empty($_SERVER['REMOTE_ADDR']) && ! isset($_SERVER['HTTP_USER_AGENT']) && count($_SERVER['argv']) > 0) {
return true;
}
@ -191,7 +194,7 @@ class ElementPermissionListener
} elseif ('edit' === $mode) {
$operation = $annotation->getEditOperationName();
} else {
throw new \InvalidArgumentException('$mode must be either "read" or "edit"!');
throw new InvalidArgumentException('$mode must be either "read" or "edit"!');
}
//Users must always be checked, because its return value can differ if it is the user itself or something else
@ -200,10 +203,10 @@ class ElementPermissionListener
}
//Check if we have already have saved the permission, otherwise save it to cache
if (! isset($this->perm_cache[$mode][\get_class($element)][$operation])) {
$this->perm_cache[$mode][\get_class($element)][$operation] = $this->security->isGranted($operation, $element);
if (! isset($this->perm_cache[$mode][get_class($element)][$operation])) {
$this->perm_cache[$mode][get_class($element)][$operation] = $this->security->isGranted($operation, $element);
}
return $this->perm_cache[$mode][\get_class($element)][$operation];
return $this->perm_cache[$mode][get_class($element)][$operation];
}
}

View file

@ -30,7 +30,7 @@ use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class UserChecker implements UserCheckerInterface
final class UserChecker implements UserCheckerInterface
{
public function __construct()
{

View file

@ -26,6 +26,7 @@ namespace App\Security\Voter;
use App\Entity\Attachments\Attachment;
use App\Entity\UserSystem\User;
use function in_array;
class AttachmentVoter extends ExtendedVoter
{
@ -54,7 +55,7 @@ class AttachmentVoter extends ExtendedVoter
protected function supports($attribute, $subject)
{
if ($subject instanceof Attachment) {
return \in_array($attribute, $this->resolver->listOperationsForPermission('parts_attachments'), false);
return in_array($attribute, $this->resolver->listOperationsForPermission('parts_attachments'), false);
}
return false;

View file

@ -35,13 +35,12 @@ use Symfony\Component\Security\Core\Authorization\Voter\Voter;
*/
abstract class ExtendedVoter extends Voter
{
protected $entityManager;
/**
* @var PermissionResolver
*/
protected $resolver;
protected $entityManager;
public function __construct(PermissionResolver $resolver, EntityManagerInterface $entityManager)
{
$this->resolver = $resolver;

View file

@ -58,7 +58,7 @@ class PermissionVoter extends ExtendedVoter
protected function supports($attribute, $subject)
{
//Check if the attribute has the form @permission.operation
if (preg_match('/^@\\w+\\.\\w+$/', $attribute)) {
if (preg_match('#^@\\w+\\.\\w+$#', $attribute)) {
$attribute = ltrim($attribute, '@');
[$perm, $op] = explode('.', $attribute);

View file

@ -34,6 +34,8 @@ use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\User;
use function get_class;
use function is_object;
class StructureVoter extends ExtendedVoter
{
@ -47,7 +49,7 @@ class StructureVoter extends ExtendedVoter
*/
protected function supports($attribute, $subject)
{
if (\is_object($subject)) {
if (is_object($subject)) {
$permission_name = $this->instanceToPermissionName($subject);
//If permission name is null, then the subject is not supported
return (null !== $permission_name) && $this->resolver->isValidOperation($permission_name, $attribute);
@ -63,7 +65,7 @@ class StructureVoter extends ExtendedVoter
*/
protected function instanceToPermissionName($subject): ?string
{
$class_name = \get_class($subject);
$class_name = get_class($subject);
switch ($class_name) {
case AttachmentType::class:
return 'attachment_types';

View file

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\UserSystem\User;
use function in_array;
class UserVoter extends ExtendedVoter
{
@ -39,7 +40,7 @@ class UserVoter extends ExtendedVoter
protected function supports($attribute, $subject)
{
if ($subject instanceof User) {
return \in_array($attribute, array_merge(
return in_array($attribute, array_merge(
$this->resolver->listOperationsForPermission('users'),
$this->resolver->listOperationsForPermission('self')),
false