Completed
Pull Request — 2.x (#3830)
by Scott Kingsley
05:08 queued 01:06
created

PodsBuilderModuleSingle::_start_table_edit()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 83
Code Lines 50

Duplication

Lines 42
Ratio 50.6 %

Importance

Changes 0
Metric Value
cc 6
eloc 50
nc 20
nop 2
dl 42
loc 83
rs 8.3634
c 0
b 0
f 0

How to fix   Long Method   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @package Pods\Components
4
 * @subpackage Builder
5
 */
6
if ( !class_exists( 'LayoutModule' ) )
7
    return;
8
9
if ( !class_exists( 'PodsBuilderModuleSingle' ) ) {
10
    class PodsBuilderModuleSingle extends LayoutModule {
11
12
        var $_name = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
13
        var $_var = 'pods-builder-single';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_var.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
14
        var $_description = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_description.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
        var $_editor_width = 500;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_editor_width.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
        var $_can_remove_wrappers = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_can_remove_wrappers.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
        /**
19
         * Register the Module
20
         */
21 View Code Duplication
        public function __construct () {
0 ignored issues
show
Duplication introduced by
This method 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...
22
            $this->_name = __( 'Pods - Single Item', 'pods' );
23
            $this->_description = __( 'Display a single Pod item', 'pods' );
24
            $this->module_path = dirname( __FILE__ );
25
26
            $this->LayoutModule();
27
        }
28
29
        /**
30
         * Set default variables
31
         *
32
         * @param $defaults
33
         *
34
         * @return mixed
35
         */
36
        function _get_defaults ( $defaults ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
            $new_defaults = array(
38
                'pod_type' => '',
39
                'slug' => '',
40
                'template' => '',
41
                'template_custom' => '',
42
                'sidebar' => 'none'
43
            );
44
45
            return ITUtility::merge_defaults( $new_defaults, $defaults );
46
        }
47
48
        /**
49
         * Output something before the table form
50
         *
51
         * @param object $form Form class
52
         * @param bool $results
53
         */
54
        function _before_table_edit ( $form, $results = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
?>
56
    <p><?php echo $this->_description; ?></p>
57
<?php
58
        }
59
60
        /**
61
         * Output something at the start of the table form
62
         *
63
         * @param object $form Form class
64
         * @param bool $results
65
         */
66
        function _start_table_edit ( $form, $results = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
            $api = pods_api();
68
            $all_pods = $api->load_pods( array( 'names' => true ) );
69
70
            $pod_types = array();
71
72
            foreach ( $all_pods as $pod_name => $pod_label ) {
0 ignored issues
show
Bug introduced by
The expression $all_pods of type array|object|integer|double|string|null|boolean 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...
73
                $pod_types[ $pod_name ] = $pod_label . ' (' . $pod_name . ')';
74
            }
75
?>
76
    <tr>
77
        <td valign="top">
78
            <label for="pod_type"><?php _e( 'Pod', 'pods' ); ?></label>
79
        </td>
80
        <td>
81
            <?php
82 View Code Duplication
                if ( 0 < count( $all_pods ) )
83
                    $form->add_drop_down( 'pod_type', $pod_types );
84
                else
85
                    echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>';
86
            ?>
87
        </td>
88
    </tr>
89
    <tr>
90
        <td valign="top">
91
            <label for="slug"><?php _e( 'Slug or ID', 'pods' ); ?></label>
92
        </td>
93
        <td>
94
            <?php $form->add_text_box( 'slug' ); ?>
95
        </td>
96
    </tr>
97
98
    <?php
99 View Code Duplication
        if ( class_exists( 'Pods_Templates' ) ) {
100
            $all_templates = (array) $api->load_templates( array() );
101
102
            $templates = array(
103
                '' => '- ' . __( 'Custom Template', 'pods' ) . ' -'
104
            );
105
106
            foreach ( $all_templates as $template ) {
107
                $templates[ $template[ 'name' ] ] = $template[ 'name' ];
108
            }
109
    ?>
110
        <tr>
111
            <td valign="top">
112
                <label for="template"><?php _e( 'Template', 'pods' ); ?></label>
113
            </td>
114
            <td>
115
                <?php
116
                    if ( 0 < count( $all_templates ) )
117
                        $form->add_drop_down( 'template', $templates );
118
                    else
119
                        echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>';
120
                ?>
121
            </td>
122
        </tr>
123
    <?php
124
        }
125
        else {
126
    ?>
127
        <tr>
128
            <td valign="top">
129
                <label for="template"><?php _e( 'Template', 'pods' ); ?></label>
130
            </td>
131
            <td>
132
                <?php $form->add_text_box( 'template' ); ?>
133
            </td>
134
        </tr>
135
    <?php
136
        }
137
    ?>
138
139
    <tr>
140
        <td valign="top">
141
            <label for="template_custom"><?php _e( 'Custom Template', 'pods' ); ?></label>
142
        </td>
143
        <td>
144
            <?php $form->add_text_area( 'template_custom', array( 'style' => 'width:90%; max-width:100%; min-height:100px;', 'rows' => '8' ) ); ?>
145
        </td>
146
    </tr>
147
<?php
148
        }
149
150
        /**
151
         * Module Output
152
         */
153
        function _render ( $fields ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
154
            $args = array(
155
                'name' => trim( pods_var_raw( 'pod_type', $fields[ 'data' ], '' ) ),
156
                'slug' => trim( pods_var_raw( 'slug', $fields[ 'data' ], '' ) ),
157
                'template' => trim( pods_var_raw( 'template', $fields[ 'data' ], '' ) )
158
            );
159
160
            $content = trim( pods_var_raw( 'template_custom', $fields[ 'data' ], '' ) );
161
162 View Code Duplication
            if ( 0 < strlen( $args[ 'name' ] ) && 0 < strlen( $args[ 'slug' ] ) && ( 0 < strlen( $args[ 'template' ] ) || 0 < strlen( $content ) ) )
163
                echo pods_shortcode( $args, ( isset( $content ) ? $content : null ) );
164
        }
165
166
    }
167
}
168
169
new PodsBuilderModuleSingle();