Completed
Push — master ( 296743...12eab9 )
by Kirill
36:17
created

BaseValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 0
loc 75
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getGroupName() 0 4 1
A getValidator() 0 4 1
A getCallStack() 0 4 1
A throw() 0 10 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Reflection\Validation\Base;
11
12
use Railt\SDL\Exceptions\SchemaException;
13
use Railt\SDL\Reflection\Validation\Validator;
14
use Railt\SDL\Runtime\CallStack;
15
use Railt\SDL\Runtime\CallStackInterface;
16
use Railt\SDL\Support;
17
18
/**
19
 * Class BaseValidator
20
 */
21
abstract class BaseValidator implements ValidatorInterface
22
{
23
    use Support;
24
25
    /**
26
     * @var Validator
27
     */
28
    protected $validator;
29
30
    /**
31
     * @var CallStack
32
     */
33
    private $stack;
34
35
    /**
36
     * @var string
37
     */
38
    private $name;
39
40
    /**
41
     * BaseValidator constructor.
42
     * @param Validator $factory
43
     * @param CallStackInterface $stack
44
     * @param null|string $name
45
     */
46 283
    public function __construct(Validator $factory, CallStackInterface $stack, ?string $name)
47
    {
48 283
        $this->validator = $factory;
49 283
        $this->stack     = $stack;
0 ignored issues
show
Documentation Bug introduced by
$stack is of type object<Railt\SDL\Runtime\CallStackInterface>, but the property $stack was declared to be of type object<Railt\SDL\Runtime\CallStack>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
50 283
        $this->name      = $name ?? static::class;
51 283
    }
52
53
    /**
54
     * @return string
55
     */
56 283
    public function getGroupName(): string
57
    {
58 283
        return $this->name;
59
    }
60
61
    /**
62
     * @param string $group
63
     * @return ValidatorInterface
64
     * @throws \OutOfBoundsException
65
     */
66 123
    public function getValidator(string $group): ValidatorInterface
67
    {
68 123
        return $this->validator->group($group);
69
    }
70
71
    /**
72
     * @return CallStack|CallStackInterface
73
     */
74 6517
    public function getCallStack(): CallStackInterface
75
    {
76 6517
        return $this->stack;
77
    }
78
79
    /**
80
     * @param string $exception
81
     * @param string $message
82
     * @param array ...$args
83
     * @return void
84
     */
85
    public function throw(string $exception, string $message, ...$args): void
86
    {
87
        $message = \sprintf($message, ...$args);
88
89
        if (\is_subclass_of($exception, SchemaException::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Railt\SDL\Exceptions\SchemaException::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
90
            throw new $exception($message, $this->stack);
91
        }
92
93
        throw new $exception($message);
94
    }
95
}
96