Completed
Branch dev (71673f)
by
unknown
05:17
created

AdminPageFramework_Utility   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A showDeprecationNotice() 0 12 2
A callBack() 0 10 2
A hasBeenCalled() 0 7 2
A getOutputBuffer() 0 9 1
A getObjectInfo() 0 7 1
A getAOrB() 0 3 2
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods which do not use WordPress functions.
12
 *
13
 * @since       2.0.0
14
 * @since       3.7.3       Became not abstract for the xdebug max nesting level fatal error workaround.
15
 * @extends     AdminPageFramework_Utility_SystemInformation
16
 * @package     AdminPageFramework
17
 * @subpackage  Utility
18
 * @internal
19
 */
20
class AdminPageFramework_Utility extends AdminPageFramework_Utility_HTMLAttribute {
21
    
22
    /**
23
     * Shows a message for a deprecated item.
24
     * 
25
     * Uses the `E_USER_NOTICE` error level so that the message won't be shown if `WP_DEBUG` is `false`.
26
     * 
27
     * @remark      This method is overridden by the `AdminPageFramework_FrameworkUtility` class.
28
     * @return      void
29
     * @since       3.8.8
30
     */
31
    static public function showDeprecationNotice( $sDeprecated, $sAlternative='', $sProgramName='Admin Page Framework' ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
32
        trigger_error(
33
            $sProgramName . ': ' . sprintf(
34
                $sAlternative
35
                    ? '<code>%1$s</code> has been deprecated. Use <code>%2$s</code> instead.'
36
                    : '<code>%1$s</code> has been deprecated.',
37
                $sDeprecated, // %1$s
38
                $sAlternative // %2%s
39
            ),
40
            E_USER_NOTICE
41
        );
42
    }
43
           
44
    /**
45
     * Calls back a user defined function.
46
     * 
47
     * This is meant to be used to filter a value using a callback. When a callback is not available, the first parameter element will be returned.
48
     * so set a default return value to the first element of the parameter array.
49
     * 
50
     * @since       3.7.0
51
     * @since       3.8.5               Moved from `AdminPageFramework_Form_Base`. Added the default value to the `$asParameters`second parameter.
52
     * @param       callable            $oCallable
53
     * @param       string|array        $asParameters       Parameters to pass to the callback function.
54
     */
55
    public function callBack( $oCallable, $asParameters=array() ) {
56
        $_aParameters   = self::getAsArray( 
57
            $asParameters, 
58
            true // preserve empty
59
        );
60
        $_mDefaultValue = self::getElement( $_aParameters, 0 );
61
        return is_callable( $oCallable )
62
            ? call_user_func_array( $oCallable, $_aParameters )
63
            : $_mDefaultValue;
64
    }          
65
           
66
    /**
67
     * Checks if the given id (usually a function name) has been called throughout the page load.
68
     * 
69
     * This is used to check if a function which needs to be done only once has been already called or not.
70
     * 
71
     * @since       3.7.0
72
     * @return      boolean
73
     */
74
    static public function hasBeenCalled( $sID ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
75
        if ( isset( self::$_aCallStack[ $sID ] ) ) {
76
            return true;
77
        }
78
        self::$_aCallStack[ $sID ] = true;
79
        return false;
80
    }
81
        /**
82
         * Stores calls.
83
         * @internal
84
         */
85
        static private $_aCallStack = array();    
86
       
87
    /**
88
     * Captures the output buffer of the given function.
89
     * @since       3.6.3
90
     * @return      string      The captured output buffer.
91
     */
92
    static public function getOutputBuffer( $oCallable, array $aParameters=array() ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
93
        
94
        ob_start(); 
95
        echo call_user_func_array( $oCallable, $aParameters );
96
        $_sContent = ob_get_contents(); 
97
        ob_end_clean(); 
98
        return $_sContent;        
99
        
100
    }
101
                  
102
    /**
103
     * Generates brief object information.
104
     * 
105
     * @remark      Meant to be used for the `__toString()` method.
106
     * @since       3.6.0
107
     * @return      string
108
     */   
109
    static public function getObjectInfo( $oInstance ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
110
        
111
        $_iCount     = count( get_object_vars( $oInstance ) );
112
        $_sClassName = get_class( $oInstance );
113
        return '(object) ' . $_sClassName . ': ' . $_iCount . ' properties.';
114
        
115
    }
116
                   
117
    
118
    /**
119
     * Returns one or the other.
120
     * 
121
     * Saves one conditional statement.
122
     * 
123
     * @remark      Use this only when the performance is not critical.
124
     * @since       3.5.3
125
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mValue     The value to evaluate.
126
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mTrue      The value to return when the first parameter value yields true.
127
     * @param       boolean|integer|double|string|array|object|resource|NULL        $mTrue      The value to return when the first parameter value yields false.
128
     * @return      mixed
129
     */
130
    static public function getAOrB( $mValue, $mTrue=null, $mFalse=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
131
        return $mValue ? $mTrue : $mFalse;
132
    }    
133
    
134
}
135