ValidatedDescription::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Description;
6
7
use Psi\Component\Description\Schema\Schema;
8
9
/**
10
 * Decorator for description which validates the descriptions based
11
 * on the given Schema.
12
 *
13
 * Note that for performance reasons this should only be used in a development
14
 * environemnt.
15
 */
16
class ValidatedDescription implements DescriptionInterface
17
{
18
    /**
19
     * @var Description
20
     */
21
    private $description;
22
23
    /**
24
     * @var Scehma
25
     */
26
    private $schema;
27
28
    public function __construct(DescriptionInterface $description, Schema $schema)
29
    {
30
        $this->description = $description;
0 ignored issues
show
Documentation Bug introduced by
$description is of type object<Psi\Component\Des...n\DescriptionInterface>, but the property $description was declared to be of type object<Psi\Component\Description\Description>. 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...
31
        $this->schema = $schema;
0 ignored issues
show
Documentation Bug introduced by
It seems like $schema of type object<Psi\Component\Description\Schema\Schema> is incompatible with the declared type object<Psi\Component\Description\Scehma> of property $schema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function get($descriptorKey): DescriptorInterface
38
    {
39
        $this->schema->validateKey($descriptorKey);
40
41
        return $this->description->get($descriptorKey);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function has($descriptor): bool
48
    {
49
        $this->schema->validateKey($descriptor);
50
51
        return $this->description->has($descriptor);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function all(): array
58
    {
59
        return $this->description->all();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function set(string $key, DescriptorInterface $descriptor, int $priority = 0)
66
    {
67
        $this->schema->validate($key, $descriptor);
68
        $this->description->set($key, $descriptor, $priority);
69
    }
70
}
71