Conditions | 28 |
Paths | 114 |
Total Lines | 113 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
63 | private static function checkExpose(Builder $builder): void |
||
64 | { |
||
65 | $columns = $builder->getColumns(); |
||
66 | $fullKey = $builder->getFullKey(); |
||
67 | $value = $builder->getValue(); |
||
68 | |||
69 | // Check if the key itself exists at first |
||
70 | if (isset($columns[$fullKey])) { |
||
71 | $column = $columns[$fullKey]; |
||
72 | |||
73 | // If boolean, set expose to the boolean value |
||
74 | if (is_bool($column)) { |
||
75 | $builder->setExpose($column); |
||
76 | } |
||
77 | |||
78 | // If callable, set the expose to true, and run the method and passes the builder as parameter |
||
79 | elseif (is_callable($column)) { |
||
80 | $builder->setExpose(true); |
||
81 | $callbackReturn = $column($builder); |
||
82 | |||
83 | // If builder is returned, no nothing |
||
84 | if ($callbackReturn instanceof BuilderInterface) { |
||
85 | $builder = $callbackReturn; |
||
86 | } |
||
87 | |||
88 | // If string is returned, set expose to true and parse with mb_sprintfn or mb_sprintf |
||
89 | elseif (is_string($callbackReturn)) { |
||
90 | $builder->setExpose(true); |
||
91 | $builder->setValue(self::getValue($callbackReturn, $value)); |
||
92 | } |
||
93 | |||
94 | // If bool is returned, set expose to boolean value |
||
95 | elseif (is_bool($callbackReturn)) { |
||
96 | $builder->setExpose($callbackReturn); |
||
97 | } |
||
98 | |||
99 | // If array is returned, parse the columns from the current context key and merge it with the builder |
||
100 | elseif (is_array($callbackReturn)) { |
||
101 | $columns = self::parseColumnsRecursive($callbackReturn, $builder->getFullKey()); |
||
102 | |||
103 | // If not set, set expose to false by default |
||
104 | if (!isset($columns[$builder->getFullKey()])) { |
||
105 | $columns[$builder->getFullKey()] = false; |
||
106 | } |
||
107 | |||
108 | //@TODO handle with array_merge_recursive and handle array inside the columns parameters ^^ |
||
109 | $builder->setColumns(array_merge($builder->getColumns(), $columns)); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // If is string, set expose to true and parse with mb_sprintfn or mb_sprintf |
||
114 | elseif (is_string($column)) { |
||
115 | $builder->setExpose(true); |
||
116 | $builder->setValue(self::getValue($column, $value)); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Otherwise, check if a parent key exists |
||
121 | else { |
||
122 | $parentKey = $fullKey; |
||
123 | while ($parentIndex = strrpos($parentKey, '.')) { |
||
124 | $parentKey = substr($parentKey, 0, $parentIndex); |
||
125 | if (isset($columns[$parentKey])) { |
||
126 | $column = $columns[$parentKey]; |
||
127 | if (is_bool($column)) { |
||
128 | $builder->setExpose($column); |
||
129 | } |
||
130 | elseif (is_callable($column)) { |
||
131 | $builder->setExpose(true); |
||
132 | $callbackReturn = $column($builder); |
||
133 | if ($callbackReturn instanceof BuilderInterface) { |
||
134 | $builder = $callbackReturn; |
||
135 | } |
||
136 | elseif (is_string($callbackReturn)) { |
||
137 | $builder->setExpose(true); |
||
138 | $builder->setValue(self::getValue($callbackReturn, $value)); |
||
139 | } |
||
140 | elseif (is_bool($callbackReturn)) { |
||
141 | $builder->setExpose($callbackReturn); |
||
142 | } |
||
143 | elseif (is_array($callbackReturn)) { |
||
144 | // Since it is a parent, we're not supposed to re-re-merge the existing columns |
||
145 | } |
||
146 | } |
||
147 | elseif (is_string($column)) { |
||
148 | $builder->setExpose(false); |
||
149 | $builder->setValue(self::getValue($column, $value)); |
||
150 | } |
||
151 | break; |
||
152 | // break at the first parent found |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // Try to find a subentity, or field that has the true value |
||
158 | $value = $builder->getValue(); |
||
159 | if ((is_array($value) || is_object($value) || is_callable($value))) { |
||
160 | $subColumns = is_null($columns) ? $columns : array_filter($columns, function ($columnValue, $columnKey) use ($builder) { |
||
161 | |||
162 | $ret = strpos($columnKey, $builder->getFullKey()) === 0; |
||
163 | if ($ret && $columnValue === true) { |
||
164 | // expose the current instance (which is the parent of the sub column) |
||
165 | $builder->setExpose(true); |
||
166 | } |
||
167 | |||
168 | return $ret; |
||
169 | }, ARRAY_FILTER_USE_BOTH); |
||
170 | } |
||
171 | |||
172 | // check for protected setting |
||
173 | $key = $builder->getKey(); |
||
174 | if (!$builder->getProtected() && is_string($key) && strpos($key, '_') === 0) { |
||
175 | $builder->setExpose(false); |
||
176 | } |
||
289 |