AssertTrait::assertIsGranted()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\Traits;
12
13
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
14
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15
use Symfony\Component\Security\Core\Security;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
trait AssertTrait
19
{
20 12
    private function assertIsValidObject(
21
        ValidatorInterface $validator,
22
        object $object,
23
        array $constraints = null,
24
        array $groups = null
25
    ): void {
26 12
        $violations = $validator->validate($object, $constraints, $groups);
0 ignored issues
show
Bug introduced by
It seems like $constraints defined by parameter $constraints on line 23 can also be of type array; however, Symfony\Component\Valida...orInterface::validate() does only seem to accept object<Symfony\Component...dator\Constraint>>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $groups defined by parameter $groups on line 24 can also be of type array; however, Symfony\Component\Valida...orInterface::validate() does only seem to accept string|object<Symfony\Co...ts\GroupSequence>>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
27 12
        if ($violations->count() > 0) {
28 1
            throw new ValidationException($violations);
29
        }
30 11
    }
31
32 11
    private function assertIsGranted(Security $security, string $attribute, object $subject = null): void
33
    {
34 11
        $isGranted = $security->isGranted($attribute, $subject);
35 11
        if (false === $isGranted) {
36 1
            throw new AccessDeniedException();
37
        }
38 11
    }
39
}
40