Completed
Push — master ( 311e24...ae64ed )
by Ryan
06:49
created

TextGuesser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
c 1
b 1
f 0
lcom 1
cbo 6
dl 111
loc 111
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
C guess() 44 44 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\Streams\Platform\Ui\Form\Component\Button\Guesser;
2
3
use Anomaly\Streams\Platform\Addon\Module\Module;
4
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection;
5
use Anomaly\Streams\Platform\Support\Str;
6
use Anomaly\Streams\Platform\Ui\Button\ButtonRegistry;
7
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
8
use Illuminate\Contracts\Config\Repository;
9
use Illuminate\Translation\Translator;
10
11
/**
12
 * Class TextGuesser
13
 *
14
 * @link          http://pyrocms.com/
15
 * @author        PyroCMS, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Button\Guesser
18
 */
19 View Code Duplication
class TextGuesser
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
22
    /**
23
     * The config repository.
24
     *
25
     * @var Repository
26
     */
27
    protected $config;
28
29
    /**
30
     * The string utility.
31
     *
32
     * @var Str
33
     */
34
    protected $string;
35
36
    /**
37
     * The button registry.
38
     *
39
     * @var ButtonRegistry
40
     */
41
    protected $buttons;
42
43
    /**
44
     * The module collection.
45
     *
46
     * @var ModuleCollection
47
     */
48
    protected $modules;
49
50
    /**
51
     * The translator utility.
52
     *
53
     * @var Translator
54
     */
55
    protected $translator;
56
57
    /**
58
     * Create a new TextGuesser instance.
59
     *
60
     * @param Str              $string
61
     * @param Repository       $config
62
     * @param ButtonRegistry   $buttons
63
     * @param ModuleCollection $modules
64
     * @param Translator       $translator
65
     */
66
    public function __construct(
67
        Str $string,
68
        Repository $config,
69
        ButtonRegistry $buttons,
70
        ModuleCollection $modules,
71
        Translator $translator
72
    ) {
73
        $this->config     = $config;
74
        $this->string     = $string;
75
        $this->buttons    = $buttons;
76
        $this->modules    = $modules;
77
        $this->translator = $translator;
78
    }
79
80
    /**
81
     * Guess the button from the hint.
82
     *
83
     * @param FormBuilder $builder
84
     */
85
    public function guess(FormBuilder $builder)
86
    {
87
        $buttons = $builder->getButtons();
88
89
        $module = $this->modules->active();
90
91
        /**
92
         * This will break if we can't figure
93
         * out what the active module is.
94
         */
95
        if (!$module instanceof Module) {
96
            return;
97
        }
98
99
        foreach ($buttons as &$button) {
0 ignored issues
show
Bug introduced by
The expression $buttons 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...
100
101
            if (isset($button['text'])) {
102
                continue;
103
            }
104
105
            if (!isset($button['button'])) {
106
                continue;
107
            }
108
109
            $text = $module->getNamespace('button.' . $button['button']);
110
111
            if (!isset($button['text']) && $this->translator->has($text)) {
112
                $button['text'] = $text;
113
            }
114
115
            if (
116
                (!isset($button['text']) || !$this->translator->has($button['text']))
117
                && $this->config->get('streams::system.lazy_translations')
118
            ) {
119
                $button['text'] = $this->string->humanize($button['button']);
120
            }
121
122
            if (!isset($button['text'])) {
123
                $button['text'] = $text;
124
            }
125
        }
126
127
        $builder->setButtons($buttons);
128
    }
129
}
130