AssertTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertIsValidObject() 0 11 2
A assertIsGranted() 0 7 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