HeadingsGuesser::guess()   F
last analyzed

Complexity

Conditions 30
Paths 268

Size

Total Lines 130
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 30
eloc 45
nc 268
nop 1
dl 0
loc 130
rs 3.4683
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php namespace Anomaly\Streams\Platform\Ui\Table\Component\Header\Guesser;
2
3
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
4
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
5
use Anomaly\Streams\Platform\Support\Str;
6
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
7
use Illuminate\Contracts\Config\Repository;
8
use Illuminate\Translation\Translator;
9
10
/**
11
 * Class HeadingsGuesser
12
 *
13
 * @link   http://pyrocms.com/
14
 * @author PyroCMS, Inc. <[email protected]>
15
 * @author Ryan Thompson <[email protected]>
16
 */
17
class HeadingsGuesser
18
{
19
20
    /**
21
     * The string utility.
22
     *
23
     * @var Str
24
     */
25
    protected $string;
26
27
    /**
28
     * The config repository.
29
     *
30
     * @var Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * The module collection.
36
     *
37
     * @var ModuleCollection
38
     */
39
    protected $modules;
40
41
    /**
42
     * The translator utility.
43
     *
44
     * @var Translator
45
     */
46
    protected $translator;
47
48
    /**
49
     * Create a new HeadingsGuesser instance.
50
     *
51
     * @param Str              $string
52
     * @param Repository       $config
53
     * @param ModuleCollection $modules
54
     * @param Translator       $translator
55
     */
56
    public function __construct(Str $string, Repository $config, ModuleCollection $modules, Translator $translator)
57
    {
58
        $this->string     = $string;
59
        $this->config     = $config;
60
        $this->modules    = $modules;
61
        $this->translator = $translator;
62
    }
63
64
    /**
65
     * Guess the field for a column.
66
     *
67
     * @param TableBuilder $builder
68
     */
69
    public function guess(TableBuilder $builder)
70
    {
71
        $columns = $builder->getColumns();
72
        $stream  = $builder->getTableStream();
73
74
        $module = $this->modules->active();
75
76
        foreach ($columns as &$column) {
0 ignored issues
show
Bug introduced by
The expression $columns of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
78
            /*
79
             * If the heading is already set then
80
             * we don't have anything to do.
81
             */
82
            if (isset($column['heading'])) {
83
                continue;
84
            }
85
86
            /*
87
             * If the heading is false, then no
88
             * header is desired at all.
89
             */
90
            if (isset($column['heading']) && $column['heading'] === false) {
91
                continue;
92
            }
93
94
            /*
95
             * No stream means we can't
96
             * really do much here.
97
             */
98
            if (!$stream instanceof StreamInterface) {
99
                continue;
100
            }
101
102
            if (!isset($column['field']) && is_string($column['value'])) {
103
                $column['field'] = $column['value'];
104
            }
105
106
            /*
107
             * If the heading matches a field
108
             * with dot format then reduce it.
109
             */
110
            if (isset($column['field']) && preg_match("/^entry.([a-zA-Z\\_]+)/", $column['field'], $match)) {
111
                $column['field'] = $match[1];
112
            }
113
114
            /*
115
             * Detect some built in columns.
116
             */
117
            if (in_array($column['field'], ['id', 'created_at', 'created_by', 'updated_at', 'updated_by'])) {
118
                $column['heading'] = 'streams::entry.' . $column['field'];
119
120
                continue;
121
            }
122
123
            /*
124
             * Detect entry title.
125
             */
126
            if (in_array($column['field'], ['view_link', 'edit_link']) && $field = $stream->getTitleField()) {
127
                $column['heading'] = $field->getName();
128
129
                continue;
130
            }
131
132
            $field = $stream->getField(array_get($column, 'field'));
133
134
            /*
135
             * Detect the title column.
136
             */
137
            $title = $stream->getTitleField();
138
139
            if (
140
                $title &&
141
                !$field &&
142
                $column['field'] == 'title' &&
143
                $this->translator->has($heading = $title->getName())
144
            ) {
145
                $column['heading'] = $heading;
146
            }
147
148
            /*
149
             * Use the name from the field.
150
             */
151
            if ($field && $heading = $field->getName()) {
152
                $column['heading'] = $heading;
153
            }
154
155
            /*
156
             * If no field look for
157
             * a name anyways.
158
             */
159
            if ($module && !$field && $this->translator->has(
160
                    $heading = $module->getNamespace('field.' . $column['field'] . '.name')
161
                )
162
            ) {
163
                $column['heading'] = $heading;
164
            }
165
166
            /*
167
             * If no translatable heading yet and
168
             * the heading matches the value (default)
169
             * then humanize the heading value.
170
             */
171
            if (!isset($column['heading']) && $this->config->get('streams::system.lazy_translations')) {
172
                $column['heading'] = ucwords($this->string->humanize($column['field']));
173
            }
174
175
            /*
176
             * If we have a translatable heading and
177
             * the heading does not have a translation
178
             * then humanize the heading value.
179
             */
180
            if (
181
                isset($column['heading']) &&
182
                str_is('*.*.*::*', $column['heading']) &&
183
                !$this->translator->has($column['heading']) &&
184
                $this->config->get('streams::system.lazy_translations')
185
            ) {
186
                $column['heading'] = ucwords($this->string->humanize($column['field']));
187
            }
188
189
            /*
190
             * Last resort.
191
             */
192
            if ($module && !isset($column['heading'])) {
193
                $column['heading'] = $module->getNamespace('field.' . $column['field'] . '.name');
194
            }
195
        }
196
197
        $builder->setColumns($columns);
198
    }
199
}
200