Completed
Push — master ( 6c0f04...09807d )
by Ryan
08:37
created

PlaceholdersGuesser   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 118
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 118
loc 118
wmc 24
lcom 0
cbo 4
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D guess() 109 109 24

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\Field\Guesser;
2
3
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
4
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
5
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
6
7
/**
8
 * Class PlaceholdersGuesser
9
 *
10
 * @link          http://anomaly.is/streams-platform
11
 * @author        AnomalyLabs, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Field\Guesser
14
 */
15 View Code Duplication
class PlaceholdersGuesser
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...
16
{
17
18
    /**
19
     * Guess the field placeholders.
20
     *
21
     * @param FormBuilder $builder
22
     */
23
    public function guess(FormBuilder $builder)
24
    {
25
        $fields = $builder->getFields();
26
        $stream = $builder->getFormStream();
27
28
        foreach ($fields as &$field) {
0 ignored issues
show
Bug introduced by
The expression $fields 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...
29
30
            $locale = array_get($field, 'locale');
31
32
            /**
33
             * If the placeholder are already set then use it.
34
             */
35
            if (isset($field['placeholder'])) {
36
37
                if (str_is('*::*', $field['placeholder'])) {
38
                    $field['placeholder'] = trans($field['placeholder'], [], null, $locale);
39
                }
40
41
                continue;
42
            }
43
44
            /**
45
             * If we don't have a field then we
46
             * can not really guess anything here.
47
             */
48
            if (!isset($field['field'])) {
49
                continue;
50
            }
51
52
            /**
53
             * No stream means we can't
54
             * really do much here.
55
             */
56
            if (!$stream instanceof StreamInterface) {
57
                continue;
58
            }
59
60
            $assignment = $stream->getAssignment($field['field']);
61
            $object     = $stream->getField($field['field']);
62
63
            /**
64
             * No assignment means we still do
65
             * not have anything to do here.
66
             */
67
            if (!$assignment instanceof AssignmentInterface) {
68
                continue;
69
            }
70
71
            /**
72
             * Next try using the fallback assignment
73
             * placeholder system as generated verbatim.
74
             */
75
            $placeholder = $assignment->getPlaceholder() . '.default';
76
77
            if (!isset($field['placeholder']) && str_is('*::*', $placeholder) && trans()->has(
0 ignored issues
show
Bug introduced by
The method has cannot be called on trans() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
78
                    $placeholder,
79
                    $locale
80
                )
81
            ) {
82
                $field['placeholder'] = trans($placeholder, [], null, $locale);
83
            }
84
85
            /**
86
             * Next try using the default assignment
87
             * placeholder system as generated verbatim.
88
             */
89
            $placeholder = $assignment->getPlaceholder();
90
91
            if (
92
                !isset($field['placeholder'])
93
                && str_is('*::*', $placeholder)
94
                && trans()->has($placeholder, $locale)
0 ignored issues
show
Bug introduced by
The method has cannot be called on trans() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
95
                && is_string($translated = trans($placeholder, [], null, $locale))
96
            ) {
97
                $field['placeholder'] = $translated;
98
            }
99
100
            /**
101
             * Check if it's just a standard string.
102
             */
103
            if (!isset($field['placeholder']) && $placeholder && !str_is('*::*', $placeholder)) {
104
                $field['placeholder'] = $placeholder;
105
            }
106
107
            /**
108
             * Next try using the default field
109
             * placeholder system as generated verbatim.
110
             */
111
            $placeholder = $object->getPlaceholder();
112
113
            if (
114
                !isset($field['placeholder'])
115
                && str_is('*::*', $placeholder)
116
                && trans()->has($placeholder, $locale)
0 ignored issues
show
Bug introduced by
The method has cannot be called on trans() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
                && is_string($translated = trans($placeholder, [], null, $locale))
118
            ) {
119
                $field['placeholder'] = $translated;
120
            }
121
122
            /**
123
             * Check if it's just a standard string.
124
             */
125
            if (!isset($field['placeholder']) && $placeholder && !str_is('*::*', $placeholder)) {
126
                $field['placeholder'] = $placeholder;
127
            }
128
        }
129
130
        $builder->setFields($fields);
131
    }
132
}
133