Completed
Branch dev (69a0d4)
by
unknown
18:27
created

AdminPageFramework_Utility_HTMLAttribute   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 156
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInlineCSS() 0 7 2
A generateInlineCSS() 0 3 1
B getStyleAttribute() 0 25 5
A generateStyleAttribute() 0 3 1
A getClassAttribute() 0 20 4
A generateClassAttribute() 0 7 1
A getDataAttributeArray() 0 12 4
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2015 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods which do not use WordPress functions.
12
 *
13
 * @since       DEVVER
14
 * @subpackage  Utility
15
 * @internal
16
 */
17
abstract class AdminPageFramework_Utility_HTMLAttribute extends AdminPageFramework_Utility_SystemInformation {
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
19
    /**
20
     * Generates inline CSS rules from the given array.
21
     * 
22
     * For example,
23
     * <code>
24
     * array(
25
     *      'width'  => '32px',
26
     *      'height' => '32px',
27
     * )
28
     * </code>
29
     * will be
30
     * <code>
31
     * 'width: 32px; height: 32px;'
32
     * </code>
33
     * 
34
     * @since       3.6.0
35
     * @return      string
36
     */
37
    static public function getInlineCSS( array $aCSSRules ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
38
        $_aOutput = array();
39
        foreach( $aCSSRules as $_sProperty => $_sValue ) {
40
            $_aOutput[] = $_sProperty . ': ' . $_sValue;
41
        }
42
        return implode( '; ', $_aOutput );
43
    }    
44
        /**
45
         * @since           3.2.0
46
         * @deprecated      3.6.0       Use `getInlineCSS()` instead.
47
         */
48
        static public function generateInlineCSS( array $aCSSRules ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
49
            return self::getInlineCSS( $aCSSRules );
50
        }
51
    
52
    /**
53
     * Generates a string of inline styles for the style attribute value from multiple arguments.
54
     * 
55
     * Duplicated items will be merged.
56
     * 
57
     * For example,
58
     * `
59
     * getStyleAttribute( array( 'margin-top' => '10px', 'display: inline-block' ), 'float:right; display: none;' )
60
     * `
61
     * will generate
62
     * `
63
     * margin-top: 10px; display: inline-block; float:right;
64
     * `
65
     * @since       3.6.0
66
     * @return      string
67
     */
68
    static public function getStyleAttribute( $asInlineCSSes ) {
1 ignored issue
show
Unused Code introduced by
The parameter $asInlineCSSes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
69
        
70
        $_aCSSRules = array();
71
        foreach( array_reverse( func_get_args() ) as $_asCSSRules ) {
72
            
73
            // For array, store in the container.
74
            if ( is_array( $_asCSSRules ) ) {
75
                $_aCSSRules = array_merge( $_asCSSRules, $_aCSSRules );
76
                continue;
77
            }
78
            
79
            // At this point, it is a string. Break them down to array elements.
80
            $__aCSSRules = explode( ';', $_asCSSRules );
81
            foreach( $__aCSSRules as $_sPair ) {
82
                $_aCSSPair = explode( ':', $_sPair );
83
                if ( ! isset( $_aCSSPair[ 0 ], $_aCSSPair[ 1 ] ) ) {
84
                    continue;
85
                }
86
                $_aCSSRules[ $_aCSSPair[ 0 ] ] = $_aCSSPair[ 1 ];
87
            }
88
            
89
        }
90
        return self::getInlineCSS( array_unique( $_aCSSRules ) );
91
        
92
    }
93
        /**
94
         * @since           3.3.1
95
         * @deprecated      3.6.0       Use `getStyleAttribute()` instead.
96
         */
97
        static public function generateStyleAttribute( $asInlineCSSes ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
98
            self::getStyleAttribute( $asInlineCSSes );
99
        }
100
    
101
    /**
102
     * Generates a string of class selectors from multiple arguments.
103
     * 
104
     * For example, 
105
     * <code>
106
     * $sClasses = getClassAttribute( array( 'button, button-primary' ), 'remove_button button' );
107
     * </code>
108
     * Will generates
109
     * <code>
110
     *  button button-primary remove_button
111
     * </code>
112
     * 
113
     * @remark      Duplicated items will be merged.
114
     * @since       3.6.0
115
     * @todo        Fix an issue that when a multidimensional array is passed, it causes a warning:  Notice: Array to string conversion.
116
     * @return      string
117
     */
118
    static public function getClassAttribute( /* $asClassSelectors1, $asClassSelectors12, ... */ ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
        
120
        $_aClasses  = array();
121
        foreach( func_get_args() as $_asClasses ) {
122
            if ( ! in_array( gettype( $_asClasses ), array( 'array', 'string' ) ) ) {
123
                continue;
124
            }            
125
            $_aClasses = array_merge( 
126
                $_aClasses,
127
                is_array( $_asClasses )
128
                    ? $_asClasses
129
                    : explode( ' ', $_asClasses )
130
            );
131
        }
132
        $_aClasses  = array_unique( array_filter( $_aClasses ) );
133
        
134
        // @todo examine if it is okay to remove the trim() function below.
135
        return trim( implode( ' ', $_aClasses ) );
136
        
137
    }    
138
        /**
139
         * Generates a string of class selectors from multiple arguments.
140
         * 
141
         * @since       3.2.0
142
         * @return      string
143
         * @deprecated  3.6.0
144
         */
145
        static public function generateClassAttribute( /* $asClassSelectors1, $asClassSelectors12 ... */ ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
146
            $_aParams = func_get_args();
147
            return call_user_func_array(
148
                array( __CLASS__ , 'getClassAttribute' ), 
149
                $_aParams
150
            );        
151
        }
152
    
153
    /**
154
     * Returns an array for generating a data attribute from the given associative array.
155
     * 
156
     * @since       3.4.0
157
     * @return      array
158
     */
159
    static public function getDataAttributeArray( array $aArray ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
160
        
161
        $_aNewArray = array();
162
        foreach( $aArray as $sKey => $v ) {
163
            if ( in_array( gettype( $v ), array( 'array', 'object' ) ) ) {
164
                continue;
165
            }            
166
            $_aNewArray[ "data-{$sKey}" ] = $v ? $v : '0';
167
        }
168
        return $_aNewArray;
169
        
170
    }
171
    
172
}