Passed
Push — master ( 3fa2aa...35993a )
by Jesse
02:11
created

Relationship::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\Hydration\Mapper\Instruction\Relation;
6
7
use Stratadox\Hydration\Hydrates;
8
use Stratadox\Hydration\Hydrator\MappedHydrator;
9
use Stratadox\Hydration\Hydrator\OneOfTheseHydrators;
10
use Stratadox\Hydration\Mapper\DefinesRelationships;
11
use Stratadox\Hydration\Mapper\FindsKeys;
12
use Stratadox\Hydration\Mapper\InstructsHowToMap;
13
use Stratadox\Hydration\Mapper\Mapper;
14
use Stratadox\Hydration\Mapper\RepresentsChoice;
15
use Stratadox\Hydration\ProducesProxyLoaders;
16
17
/**
18
 * @package Stratadox\Hydrate
19
 * @author Stratadox
20
 */
21
abstract class Relationship implements DefinesRelationships
22
{
23
    protected $class;
24
    protected $key;
25
    protected $container;
26
    protected $loader;
27
    protected $shouldNest;
28
    protected $properties;
29
    protected $decisionKey;
30
    protected $choices;
31
32
    final protected function __construct(
33
        string $class,
34
        FindsKeys $key = null,
35
        string $container = null,
36
        ProducesProxyLoaders $loader = null,
37
        bool $nested = false,
38
        array $properties = [],
39
        string $decisionKey = null,
40
        array $choices = []
41
    ) {
42
        $this->class = $class;
43
        $this->key = $key;
44
        $this->container = $container;
45
        $this->loader = $loader;
46
        $this->shouldNest = $nested;
47
        $this->properties = $properties;
48
        $this->decisionKey = $decisionKey;
49
        $this->choices = $choices;
50
    }
51
52
    public static function ofThe(
53
        string $class,
54
        FindsKeys $key = null
55
    ) : DefinesRelationships
56
    {
57
        return new static($class, $key);
58
    }
59
60
    public function containedInA(
61
        string $container
62
    ) : DefinesRelationships
63
    {
64
        return new static(
65
            $this->class,
66
            $this->key,
67
            $container,
68
            $this->loader,
69
            $this->shouldNest,
70
            $this->properties,
71
            $this->decisionKey,
72
            $this->choices
73
        );
74
    }
75
76
    public function loadedBy(
77
        ProducesProxyLoaders $loader
78
    ) : DefinesRelationships
79
    {
80
        return new static(
81
            $this->class,
82
            $this->key,
83
            $this->container,
84
            $loader,
85
            $this->shouldNest,
86
            $this->properties,
87
            $this->decisionKey,
88
            $this->choices
89
        );
90
    }
91
92
    public function nested() : DefinesRelationships
93
    {
94
        return new static(
95
            $this->class,
96
            $this->key,
97
            $this->container,
98
            $this->loader,
99
            true,
100
            $this->properties,
101
            $this->decisionKey,
102
            $this->choices
103
        );
104
    }
105
106
    public function with(
107
        string $property,
108
        InstructsHowToMap $instruction = null
109
    ) : DefinesRelationships
110
    {
111
        return new static(
112
            $this->class,
113
            $this->key,
114
            $this->container,
115
            $this->loader,
116
            $this->shouldNest,
117
            $this->properties + [$property => $instruction],
118
            $this->decisionKey,
119
            $this->choices
120
        );
121
    }
122
123
    public function selectBy(
124
        string $decisionKey,
125
        array $choices
126
    ) : DefinesRelationships
127
    {
128
        return new static(
129
            $this->class,
130
            $this->key,
131
            $this->container,
132
            $this->loader,
133
            $this->shouldNest,
134
            $this->properties,
135
            $decisionKey,
136
            $choices
137
        );
138
    }
139
140
    protected function keyOr(string $property) : string
141
    {
142
        return $this->key ? $this->key->find() : $property;
143
    }
144
145
    protected function hydrator() : Hydrates
146
    {
147
        if (isset($this->decisionKey)) {
148
            return $this->choiceHydrator();
149
        }
150
        $mapped = Mapper::forThe($this->class);
151
        foreach ($this->properties as $property => $instruction) {
152
            $mapped = $mapped->property($property, $instruction);
153
        }
154
        return MappedHydrator::fromThis($mapped->map());
155
    }
156
157
    private function choiceHydrator() : Hydrates
158
    {
159
        return OneOfTheseHydrators::decideBasedOnThe(
160
            $this->decisionKey,
0 ignored issues
show
Bug introduced by
It seems like $this->decisionKey can also be of type null; however, parameter $key of Stratadox\Hydration\Hydr...ors::decideBasedOnThe() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

160
            /** @scrutinizer ignore-type */ $this->decisionKey,
Loading history...
161
            array_map(function (RepresentsChoice $choice) {
162
                return $choice->hydrator();
163
            }, $this->choices)
164
        );
165
    }
166
}
167