Validator::validate()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
nc 7
nop 2
dl 0
loc 20
rs 8.4444
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Domain Policy Class
5
 * @package Ticaje_BookingApi
6
 * @author  Hector Luis Barrientos <[email protected]>
7
 */
8
9
namespace Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS;
10
11
use Exception;
12
13
/**
14
 * Trait Validator
15
 * @package Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS
16
 */
17
trait Validator
18
{
19
    /**
20
     * @param $decodedRule
21
     * @param $type
22
     *
23
     * @return bool
24
     * @throws Exception
25
     */
26
    private function validate($decodedRule, $type): bool
27
    {
28
        switch ($type) {
29
            case 'single':
30
                if (isset($decodedRule['date'])) {
31
                    return $decodedRule;
32
                }
33
                break;
34
            case 'period':
35
                if (isset($decodedRule['from']) && isset($decodedRule['to'])) {
36
                    return true;
37
                }
38
                break;
39
            case 'recurrent_day':
40
                if (isset($decodedRule['dayOfWeek'])) {
41
                    return true;
42
                }
43
                break;
44
        }
45
        $this->throwException();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. 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...
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51
    private function throwException()
52
    {
53
        throw new Exception(self::VALIDATION_ERROR);
0 ignored issues
show
Bug introduced by
The constant Ticaje\BookingApi\Domain...dator::VALIDATION_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54
    }
55
}
56