PropertyColumn::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Column;
6
7
use Psi\Component\Grid\View\Cell;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
11
12
class PropertyColumn extends AbstractColumn
13
{
14
    /**
15
     * @var PropertyAccessor
16
     */
17
    private $accessor;
18
19
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
20
    {
21
        $this->accessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
0 ignored issues
show
Documentation Bug introduced by
It seems like $propertyAccessor ?: \Sy...reatePropertyAccessor() of type object<Symfony\Component...pertyAccessorInterface> or object<Symfony\Component...ccess\PropertyAccessor> is incompatible with the declared type object<Psi\Component\Gri...olumn\PropertyAccessor> of property $accessor.

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...
22
    }
23
24
    public function buildCell(Cell $cell, array $options)
25
    {
26
        $property = $options['property'];
27
28
        // if the column name is the same as the property name, then assume that
29
        // the user has not overridden the property and, if the context is an array,
30
        // access it as such.
31
        if ($options['column_name'] === $options['property'] && false === is_object($cell->getContext())) {
32
            $property = '[' . $property . ']';
33
        }
34
35
        $cell->value = $this->accessor->getValue($cell->getContext(), $property);
36
    }
37
38
    public function configureOptions(OptionsResolver $options)
39
    {
40
        $options->setDefault('property', null);
41
        $options->setAllowedTypes('property', ['string', 'null']);
42
        $options->setNormalizer('property', function (OptionsResolver $options, $value) {
43
            if (null !== $value) {
44
                return $value;
45
            }
46
47
            return $options['column_name'];
48
        });
49
    }
50
}
51