ConditionalPropertyAccessor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 46
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setAnyValue() 0 15 2
A getValue() 0 21 3
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Service;
16
17
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty;
18
use Spaark\CompositeUtils\Exception\PropertyNotWritableException;
19
use Spaark\CompositeUtils\Exception\PropertyNotReadableException;
20
use Spaark\CompositeUtils\Exception\CannotReadPropertyException;
21
22
/**
23
 * This class is used to access properties of a composite, enforcing
24
 * data type requirements and access permissions
25
 */
26
class ConditionalPropertyAccessor extends PropertyAccessor
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31 6
    protected function setAnyValue(ReflectionProperty $property, $value)
32
    {
33 6
        if ($property->writable)
34
        {
35 3
            parent::setAnyValue($property, $value);
36
        }
37
        else
38
        {
39 3
            throw new PropertyNotWritableException
40
            (
41 3
                get_class($this->object),
42 3
                $property->name
43
            );
44
        }
45 3
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 7
    public function getValue(string $property)
51
    {
52 7
        if (!$this->reflect->properties->containsKey($property))
0 ignored issues
show
Documentation introduced by
$property is of type string, but the function expects a object<Spaark\CompositeU...Collection\Map\KeyType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
        {
54 1
            throw new CannotReadPropertyException
55
            (
56 1
                get_class($this->object),
57 1
                $property
58
            );
59
        }
60 6
        elseif (!$this->reflect->properties[$property]->readable)
61
        {
62 3
            throw new PropertyNotReadableException
63
            (
64 3
                get_class($this->object),
65 3
                $property
66
            );
67
        }
68
69 3
        return parent::getValue($property);
70
    }
71
}
72