Completed
Branch FET-10580-form-html-parser (9e1c68)
by
unknown
210:04 queued 197:24
created

EE_Vsprintf_Layout   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 54
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B layout_form_loop() 8 22 4

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
2
defined('EVENT_ESPRESSO_VERSION') || exit('No direct access allowed');
3
4
5
/**
6
 * Allows client code to pass in a format string used as the first argument for
7
 * `vsprintf` and that will be included in vsprintf to include all the subsections
8
 * for the layout.
9
 *
10
 * @package    ${PluginName}
11
 * @subpackage ${context}
12
 * @author     Darren Ethier
13
 * @since      ${version}
14
 */
15
class EE_Vsprintf_Layout extends EE_No_Layout
16
{
17
18
    /**
19
     * Will hold the incoming format string to be used for the vsprintf first argument.
20
     * @var string
21
     */
22
    protected $_v_format = '';
23
24
25
26
    /**
27
     * EE_Vsprintf_Layout constructor.
28
     *
29
     * @param array $options  Expects an array with 'vsprintf_format' as a key to represent the format argument that
30
     *                        will be passed to the vsprintf function.  Make sure the number of placeholders matches
31
     *                        the number of subsections in your form.
32
     */
33
    public function __construct($options = array())
34
    {
35
        $this->_v_format = isset($options['vsprintf_format'])
36
            ? (string) $options['vsprintf_format']
37
            : '';
38
        parent::__construct($options);
39
    }
40
41
42
43
    public function layout_form_loop()
44
    {
45
        $vsprintf_args = array();
46
        foreach ($this->_form_section->subsections() as $name => $subsection) {
47
            if ($subsection instanceof EE_Form_Input_Base) {
48
                $vsprintf_args[$name] = apply_filters(
49
                    'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_' . $name . '__in_' . $this->_form_section->name(),
50
                    $this->layout_input($subsection),
51
                    $this->_form_section,
52
                    $subsection
53
                );
54 View Code Duplication
            } elseif ($subsection instanceof EE_Form_Section_Base) {
55
                $vsprintf_args[$name] = apply_filters(
56
                    'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_' . $name . '__in_' . $this->_form_section->name(),
57
                    $this->layout_subsection($subsection),
0 ignored issues
show
Compatibility introduced by
$subsection of type object<EE_Form_Section_Validatable> is not a sub-type of object<EE_Form_Section_Proper>. It seems like you assume a child class of the class EE_Form_Section_Validatable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58
                    $this->_form_section,
59
                    $subsection
60
                );
61
            }
62
        }
63
        return vsprintf($this->_v_format, $vsprintf_args);
64
    }
65
66
67
68
}
69