Conditions | 10 |
Paths | 130 |
Total Lines | 100 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function render(Formatter $formatter, array $schema, string $role): ?string |
||
32 | { |
||
33 | $title = \sprintf('%s:', $formatter->title('Relations')); |
||
34 | $relations = $schema[SchemaInterface::RELATIONS] ?? []; |
||
35 | |||
36 | if (\count($relations) === 0) { |
||
37 | return $title . ' ' . $formatter->error('not defined'); |
||
38 | } |
||
39 | |||
40 | $rows = [$title]; |
||
41 | |||
42 | foreach ($relations as $field => $relation) { |
||
43 | $type = self::STR_RELATION[$relation[Relation::TYPE] ?? ''] ?? '?'; |
||
44 | $target = $relation[Relation::TARGET] ?? '?'; |
||
45 | $loading = self::STR_PREFETCH_MODE[$relation[Relation::LOAD] ?? ''] ?? 'default'; |
||
46 | $relSchema = $relation[Relation::SCHEMA]; |
||
47 | $innerKey = $relSchema[Relation::INNER_KEY] ?? '?'; |
||
48 | $outerKey = $relSchema[Relation::OUTER_KEY] ?? '?'; |
||
49 | $where = $relSchema[Relation::WHERE] ?? []; |
||
50 | $cascade = $relSchema[Relation::CASCADE] ?? null; |
||
51 | $cascadeStr = $cascade ? 'cascaded' : 'not cascaded'; |
||
52 | $nullable = $relSchema[Relation::NULLABLE] ?? null; |
||
53 | $nullableStr = $nullable ? 'nullable' : ($nullable === false ? 'not null' : 'n/a'); |
||
54 | $morphKey = $relSchema[Relation::MORPH_KEY] ?? null; |
||
55 | |||
56 | // Many-To-Many relation(s) options |
||
57 | $mmInnerKey = $relSchema[Relation::THROUGH_INNER_KEY] ?? '?'; |
||
58 | $mmOuterKey = $relSchema[Relation::THROUGH_OUTER_KEY] ?? '?'; |
||
59 | $mmEntity = $relSchema[Relation::THROUGH_ENTITY] ?? null; |
||
60 | $mmWhere = $relSchema[Relation::THROUGH_WHERE] ?? []; |
||
61 | |||
62 | |||
63 | $row = \sprintf( |
||
64 | ' %s->%s %s %s', |
||
65 | $formatter->entity($role), |
||
66 | $formatter->property($field), |
||
67 | $type, |
||
68 | $formatter->entity($target) |
||
69 | ); |
||
70 | |||
71 | if ($morphKey !== null) { |
||
72 | $row .= \sprintf( |
||
73 | ', %s: %s', |
||
74 | $formatter->title('morphed key'), |
||
75 | $this->renderKeys($formatter, $morphKey) |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | $rows[] = $row . \sprintf(', %s loading, %s', $formatter->info($loading), $formatter->info($cascadeStr)); |
||
80 | |||
81 | $row = \sprintf( |
||
82 | ' %s %s.%s <=', |
||
83 | $nullableStr, |
||
84 | $formatter->entity($role), |
||
85 | $this->renderKeys($formatter, $innerKey) |
||
86 | ); |
||
87 | |||
88 | if ($mmEntity !== null) { |
||
89 | $row .= \sprintf( |
||
90 | ' %s.%s | %s.%s ', |
||
91 | $formatter->entity($mmEntity), |
||
92 | $this->renderKeys($formatter, $mmInnerKey), |
||
93 | $formatter->entity($mmEntity), |
||
94 | $this->renderKeys($formatter, $mmOuterKey) |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | // todo: composite $outerKey |
||
99 | $rows[] = $row . \sprintf( |
||
100 | '=> %s.%s', |
||
101 | $formatter->entity($target), |
||
102 | $this->renderKeys($formatter, $outerKey) |
||
103 | ); |
||
104 | |||
105 | if (count($where)) { |
||
106 | $rows[] = \sprintf( |
||
107 | '%s: %s', |
||
108 | $formatter->title('Where'), |
||
109 | \str_replace( |
||
110 | ["\r\n", "\n"], |
||
111 | $formatter::LINE_SEPARATOR . ' ', |
||
112 | $formatter::LINE_SEPARATOR . print_r($where, true) |
||
|
|||
113 | ) |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | if (count($mmWhere)) { |
||
118 | $rows[] = \sprintf( |
||
119 | '%s: %s', |
||
120 | $formatter->title('Through where'), |
||
121 | \str_replace( |
||
122 | ["\r\n", "\n"], |
||
123 | $formatter::LINE_SEPARATOR . ' ', |
||
124 | $formatter::LINE_SEPARATOR . print_r($mmWhere, true) |
||
125 | ) |
||
126 | ); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return \implode($formatter::LINE_SEPARATOR, $rows); |
||
131 | } |
||
153 |