Passed
Pull Request — master (#1)
by Pieter
02:44
created

LocalizationAwareObjectAccess::getSetterMapping()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
rs 9.2222
1
<?php
2
3
4
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess;
5
6
use ReflectionClass;
7
use W2w\Lib\ApieObjectAccessNormalizer\Exceptions\CouldNotConvertException;
8
use W2w\Lib\ApieObjectAccessNormalizer\Getters\ReflectionLocalizedGetterMethod;
9
use W2w\Lib\ApieObjectAccessNormalizer\Interfaces\LocalizationAwareInterface;
10
use W2w\Lib\ApieObjectAccessNormalizer\Setters\ReflectionLocalizedSetterMethod;
11
12
class LocalizationAwareObjectAccess extends ObjectAccess
13
{
14
    /**
15
     * @var (ReflectionMethod|ReflectionProperty)[][][]
16
     */
17
    private $getterCache = [];
18
19
    /**
20
     * @var (ReflectionMethod|ReflectionProperty)[][][]
21
     */
22
    private $setterCache = [];
23
24
    /**
25
     * @var LocalizationAwareInterface
26
     */
27
    private $localizationAware;
28
29
    /**
30
     * @var callable
31
     */
32
    private $conversionFn;
33
34
    public function __construct(LocalizationAwareInterface  $localizationAware, callable $conversionFn, bool $publicOnly = true, bool $disabledConstructor = false)
35
    {
36
        $this->localizationAware = $localizationAware;
37
        $this->conversionFn = $conversionFn;
38
        parent::__construct($publicOnly, $disabledConstructor);
39
    }
40
41
    protected function getGetterMapping(ReflectionClass $reflectionClass): array
42
    {
43
        $className= $reflectionClass->getName();
44
        if (isset($this->getterCache[$className])) {
45
            return $this->getterCache[$className];
46
        }
47
48
        $attributes = parent::getGetterMapping($reflectionClass);
49
50
        $reflectionMethods = $this->getAvailableMethods($reflectionClass);
51
        foreach ($reflectionMethods as $method) {
52
            if ($method->getNumberOfRequiredParameters() === 1
53
                && !$method->isStatic()
54
                && preg_match('/^(get|has|is)[A-Z0-9]/i', $method->name)) {
55
                $fieldName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
56
                $attributes[$fieldName][] = new ReflectionLocalizedGetterMethod(
57
                    $method,
58
                    $this->localizationAware,
59
                    $this->conversionFn
60
                );
61
            }
62
        }
63
64
        foreach ($attributes as &$methods) {
65
            $this->sort($methods);
66
        }
67
68
        return $this->getterCache[$className] = $attributes;
69
    }
70
71
    protected function getSetterMapping(ReflectionClass $reflectionClass): array
72
    {
73
        $className= $reflectionClass->getName();
74
        if (isset($this->setterCache[$className])) {
75
            return $this->setterCache[$className];
76
        }
77
78
        $attributes = parent::getSetterMapping($reflectionClass);
79
80
        $reflectionMethods = $this->getAvailableMethods($reflectionClass);
81
        foreach ($reflectionMethods as $method) {
82
            if ($method->getNumberOfRequiredParameters() === 2
83
                && !$method->isStatic()
84
                && preg_match('/^(set)[A-Z0-9]/i', $method->name)) {
85
                $fieldName = lcfirst(substr($method->name, 3));
86
                $attributes[$fieldName][] = new ReflectionLocalizedSetterMethod(
87
                    $method,
88
                    $this->localizationAware,
89
                    $this->conversionFn
90
                );
91
            }
92
        }
93
94
        return $this->setterCache[$className] = $attributes;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    protected function handleAlternateSetter(string $fieldName, $option, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
    protected function handleAlternateSetter(string $fieldName, $option, /** @scrutinizer ignore-unused */ $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fieldName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

100
    protected function handleAlternateSetter(/** @scrutinizer ignore-unused */ string $fieldName, $option, $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
    {
102
        throw new CouldNotConvertException('ReflectionMethod|ReflectionProperty', get_class($option));
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    protected function handleAlternateGetter(string $fieldName, $option)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

108
    protected function handleAlternateGetter(/** @scrutinizer ignore-unused */ string $fieldName, $option)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110
        throw new CouldNotConvertException('ReflectionMethod|ReflectionProperty', get_class($option));
111
    }
112
}
113