Completed
Branch dev (a5fbd1)
by Michael
05:49
created

AdminPageFramework_Debug::getAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 debugging methods.
12
 * 
13
 * Use the methods of this class to check variable contents. 
14
 *
15
 * @image           http://admin-page-framework.michaeluno.jp/image/common/utility/debug.png
16
 * @since           2.0.0
17
 * @since           3.1.3       Extends AdminPageFramework_WPUtility
18
 * @since           3.7.1       Extends AdminPageFramework_FrameworkUtility
19
 * @extends         AdminPageFramework_FrameworkUtility
20
 * @package         AdminPageFramework
21
 * @subpackage      Common/Utility
22
 */
23
class AdminPageFramework_Debug extends AdminPageFramework_Debug_Log {
24
            
25
    /**
26
     * Prints out the given variable contents
27
     * 
28
     * If a file pass is given to the second parameter, it saves the output in the file.
29
     * 
30
     * @since       3.2.0
31
     * @remark      An alias of the dumpArray() method.
32
     * @param       array|string    $asArray        The variable to check its contents.
33
     * @param       string          $sFilePath      The file path for a log file.
34
     * @return      void
35
     */
36
    static public function dump( $asArray, $sFilePath=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
37
        echo self::get( $asArray, $sFilePath );
38
    }    
39
    
40
    /**
41
     * Returns a string representation of a given value with details.
42
     * @since       3.8.9
43
     * @return      string
44
     */
45
    static public function getDetails( $mValue, $bEscape=true ) {    
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
46
        $_sValueWithDetails = self::_getArrayRepresentationSanitized(
47
            self::_getLegibleDetails( $mValue )
48
        );
49
        return $bEscape
50
            ? "<pre class='dump-array'>" 
51
                    . htmlspecialchars( $_sValueWithDetails ) 
52
                . "</pre>" 
53
            : $_sValueWithDetails; // non-escape is used for exporting data into file.    
54
    }
55
56
    /**
57
     * Retrieves the output of the given variable contents.
58
     * 
59
     * If a file pass is given to the second parameter, it saves the output in the file.
60
     * 
61
     * @remark      An alias of getArray() method.
62
     * @since       3.2.0
63
     * @param       array|string    $asArray        The variable to check its contents.
64
     * @param       string          $sFilePath      The file path for a log file.
65
     * @param       boolean         $bEscape        Whether to escape characters.
66
     */
67
    static public function get( $asArray, $sFilePath=null, $bEscape=true ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
68
69
        if ( $sFilePath ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sFilePath of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
70
            self::log( $asArray, $sFilePath );     
71
        }
72
        
73
        return $bEscape
74
            ? "<pre class='dump-array'>" 
75
                    . htmlspecialchars( self::_getLegible( $asArray ) ) // `esc_html()` breaks with complex HTML code.
76
                . "</pre>" 
77
            : self::_getLegible( $asArray ); // non-escape is used for exporting data into file.    
78
        
79
    }
80
       
81
    /**
82
     * Logs the given variable output to a file.
83
     * 
84
     * <h4>Example</h4>
85
     * <code>
86
     * $_aValues = array( 'foo', 'bar' );
87
     * AdminPageFramework_Debug::log( $aValues );
88
     * </code>
89
     * 
90
     * @remark      The alias of the `logArray()` method.
91
     * @since       3.1.0
92
     * @since       3.1.3       Made it leave milliseconds and elapsed time from the last call of the method.
93
     * @since       3.3.0       Made it indicate the data type.
94
     * @since       3.3.1       Made it indicate the data length.
95
     * @param       mixed       $mValue         The value to log.  
96
     * @param       string      $sFilePath      The log file path.
97
     * @return      void
98
     **/
99
    static public function log( $mValue, $sFilePath=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
100
        self::_log( $mValue, $sFilePath );
101
    }       
102
            
103
    /* Deprecated Methods */
104
 
105
    /**
106
     * Prints out the given variable contents.
107
     * 
108
     * If a file pass is given, it saves the output in the file.
109
     * 
110
     * @since unknown
111
     * @deprecated      3.2.0
112
     */
113
    static public function dumpArray( $asArray, $sFilePath=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
114
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::dump()' );
115
        AdminPageFramework_Debug::dump( $asArray, $sFilePath );
116
    }     
117
    
118
    /**
119
     * Retrieves the output of the given array contents.
120
     * 
121
     * If a file pass is given, it saves the output in the file.
122
     * 
123
     * @since       2.1.6 The $bEncloseInTag parameter is added.
124
     * @since       3.0.0 Changed the $bEncloseInTag parameter to bEscape.
125
     * @deprecated  3.2.0
126
     */
127
    static public function getArray( $asArray, $sFilePath=null, $bEscape=true ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
128
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::get()' );
129
        return AdminPageFramework_Debug::get( $asArray, $sFilePath, $bEscape );
130
    }
131
132
    /**
133
     * Logs the given array output into the given file.
134
     * 
135
     * @since       2.1.1
136
     * @since       3.0.3   Changed the default log location and file name.
137
     * @deprecated  3.1.0   Use the `log()` method instead.
138
     */
139
    static public function logArray( $asArray, $sFilePath=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
140
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::log()' );
141
        AdminPageFramework_Debug::log( $asArray, $sFilePath );     
142
    }
143
    
144
    /**
145
     * Returns a string representation of the given value.
146
     * @since       3.5.0
147
     * @param       mixed       $mValue     The value to get as a string
148
     * @internal
149
     * @return      string
150
     * @deprecated  3.8.9
151
     */
152
    static public function getAsString( $mValue ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
153
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__ );
154
        return self::_getLegible( $mValue );
155
    }    
156
                
157
            
158
}
159