Failed Conditions
Push — issue#699 ( 7155bc )
by Guilherme
07:26
created

SupportHandler::getValidationMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\SupportBundle\Service;
12
13
use LoginCidadao\CoreBundle\Entity\PersonRepository;
14
use LoginCidadao\CoreBundle\Entity\SentEmail;
15
use LoginCidadao\CoreBundle\Entity\SentEmailRepository;
16
use LoginCidadao\CoreBundle\Model\PersonInterface;
17
use LoginCidadao\SupportBundle\Model\PersonalData;
18
use LoginCidadao\SupportBundle\Model\SupportPerson;
19
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20
21
class SupportHandler
22
{
23
    /** @var AuthorizationCheckerInterface */
24
    private $authChecker;
25
26
    /** @var PersonRepository */
27
    private $personRepository;
28
29
    /** @var SentEmailRepository */
30
    private $sentEmailRepository;
31
32
    /**
33
     * SupportHandler constructor.
34
     * @param AuthorizationCheckerInterface $authChecker
35
     * @param PersonRepository $personRepository
36
     * @param SentEmailRepository $sentEmailRepository
37
     */
38
    public function __construct(
39
        AuthorizationCheckerInterface $authChecker,
40
        PersonRepository $personRepository,
41
        SentEmailRepository $sentEmailRepository
42
    ) {
43
        $this->authChecker = $authChecker;
44
        $this->personRepository = $personRepository;
45
        $this->sentEmailRepository = $sentEmailRepository;
46
    }
47
48
    public function getSupportPerson($id): SupportPerson
49
    {
50
        $person = $this->personRepository->find($id);
51
        if ($person instanceof PersonInterface) {
52
            return new SupportPerson($person, $this->authChecker);
53
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 51 is false. This is incompatible with the type-hinted return LoginCidadao\SupportBundle\Model\SupportPerson. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
54
    }
55
56
    public function getInitialMessage($id): ?SentEmail
57
    {
58
        /** @var SentEmail $sentEmail */
59
        $sentEmail = $this->sentEmailRepository->find($id);
60
61
        return $sentEmail;
62
    }
63
64
    public function getValidationMap(SupportPerson $person): array
65
    {
66
        return array_filter([
67
            'cpf' => $this->personalDataToValidationArray($person->getCpf(), true),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->personalDataToVal...person->getCpf(), true) targeting LoginCidadao\SupportBund...DataToValidationArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
            'birthday' => $this->personalDataToValidationArray($person->getBirthday(), true),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->personalDataToVal...n->getBirthday(), true) targeting LoginCidadao\SupportBund...DataToValidationArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
            'email' => $this->personalDataToValidationArray($person->getEmail(), true),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->personalDataToVal...rson->getEmail(), true) targeting LoginCidadao\SupportBund...DataToValidationArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
            'phoneNumber' => $this->personalDataToValidationArray($person->getPhoneNumber(), true),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->personalDataToVal...getPhoneNumber(), true) targeting LoginCidadao\SupportBund...DataToValidationArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        ]);
72
    }
73
74
    private function personalDataToValidationArray(PersonalData $data, bool $skipIfValueSet = false): ?array
75
    {
76
        if (false === $data->isValueFilled()) {
77
            return null;
78
        }
79
        if ($skipIfValueSet && $data->getValue() !== null) {
80
            return null;
81
        }
82
83
        return [
84
            'name' => $data->getName(),
85
            'hash' => $data->getHash(),
86
            'challenge' => $data->getChallenge(),
87
        ];
88
    }
89
}
90