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

_convertToNumber()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 18
rs 8.8571
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
 * Collects data of page loads in admin pages.
12
 *
13
 * @since       2.1.7
14
 * @package     AdminPageFramework
15
 * @subpackage  Common/Factory/Debug
16
 * @internal
17
 */
18
abstract class AdminPageFramework_PageLoadInfo_Base extends AdminPageFramework_FrameworkUtility {
19
20
    public $oProp;
21
    
22
    public $oMsg;
23
    
24
    protected $_nInitialMemoryUsage;
25
    
26
    /**
27
     * Sets up hooks and properties.
28
     */
29
    public function __construct( $oProp, $oMsg ) {
30
                
31
        if ( ! $this->_shouldProceed( $oProp ) ) {
32
            return;
33
        }
34
            
35
        $this->oProp                = $oProp;
36
        $this->oMsg                 = $oMsg;
37
        $this->_nInitialMemoryUsage = memory_get_usage();
38
        
39
        add_action( 'in_admin_footer', array( $this, '_replyToSetPageLoadInfoInFooter' ), 999 );    
40
41
    }
42
        /**
43
         * @since       3.8.5
44
         * @return      boolean
45
         */
46
        private function _shouldProceed( $oProp ) {
47
        
48
            if ( $oProp->bIsAdminAjax || ! $oProp->bIsAdmin ) {
49
                return false;
50
            }     
51
            return ( boolean ) $oProp->bShowDebugInfo;
52
            
53
        }
54
    
55
    /**
56
     * @remark Should be overridden in an extended class.
57
     */
58
    public function _replyToSetPageLoadInfoInFooter() {}
59
    
60
    /**
61
     * Indicates whether the page load info is inserted or not.
62
     */
63
    static private $_bLoadedPageLoadInfo = false;    
64
        
65
    /**
66
     * Display gathered information.
67
     *
68
     * @access      public
69
     * @internal
70
     * @return      string
71
     */
72
    public function _replyToGetPageLoadInfo( $sFooterHTML ) {
73
        
74
        // 3.8.8+ The `bShowDebugInfo` property may be updated by the user during the page load.
75
        if ( ! $this->oProp->bShowDebugInfo ) {
76
            return $sFooterHTML;
77
        }
78
        
79
        if ( self::$_bLoadedPageLoadInfo ) { 
80
            return $sFooterHTML; 
81
        }
82
        self::$_bLoadedPageLoadInfo = true;     
83
        
84
        return $sFooterHTML
85
            . $this->_getPageLoadStats();
86
        
87
    }
88
        /**
89
         * Returns the output of page load stats.
90
         * @since       3.8.8
91
         * @return      string
92
         */
93
        private function _getPageLoadStats() {
94
            
95
            $_nSeconds            = timer_stop( 0 );
96
            $_nQueryCount         = get_num_queries();
97
            $_nMemoryUsage        = round( $this->_convertBytesToHR( memory_get_usage() ), 2 );
98
            $_nMemoryPeakUsage    = round( $this->_convertBytesToHR( memory_get_peak_usage() ), 2 );
99
            $_nMemoryLimit        = round( $this->_convertBytesToHR( $this->_convertToNumber( WP_MEMORY_LIMIT ) ), 2 );
100
            $_sInitialMemoryUsage = round( $this->_convertBytesToHR( $this->_nInitialMemoryUsage ), 2 );
101
            return "<div id='admin-page-framework-page-load-stats'>"
102
                . "<ul>"
103
                    . "<li>" . sprintf( $this->oMsg->get( 'queries_in_seconds' ), $_nQueryCount, $_nSeconds ) . "</li>"
104
                    . "<li>" . sprintf( $this->oMsg->get( 'out_of_x_memory_used' ), $_nMemoryUsage, $_nMemoryLimit, round( ( $_nMemoryUsage / $_nMemoryLimit ), 2 ) * 100 . '%' ) . "</li>"
105
                    . "<li>" . sprintf( $this->oMsg->get( 'peak_memory_usage' ), $_nMemoryPeakUsage ) . "</li>"
106
                    . "<li>" . sprintf( $this->oMsg->get( 'initial_memory_usage' ), $_sInitialMemoryUsage ) . "</li>"
107
                . "</ul>"
108
            . "</div>";          
109
            
110
        }
111
112
        /**
113
         * Transforms the php.ini notation for numbers (like '2M') to an integer
114
         *
115
         * @access private
116
         * @param $nSize
117
         * @return int
118
         * @remark This is influenced by the work of Mike Jolley.
119
         * @see http://mikejolley.com/projects/wp-page-load-stats/
120
         * @internal
121
         */
122
        private function _convertToNumber( $nSize ) {
123
            
124
            $_nReturn     = substr( $nSize, 0, -1 );
125
            switch( strtoupper( substr( $nSize, -1 ) ) ) {
126
                case 'P':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
127
                    $_nReturn *= 1024;
128
                case 'T':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
129
                    $_nReturn *= 1024;
130
                case 'G':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
131
                    $_nReturn *= 1024;
132
                case 'M':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
133
                    $_nReturn *= 1024;
134
                case 'K':
135
                    $_nReturn *= 1024;
136
            }
137
            return $_nReturn;
138
            
139
        }
140
141
        /**
142
         * Converts bytes to HR.
143
         *
144
         * @access private
145
         * @param mixed $bytes
0 ignored issues
show
Bug introduced by
There is no parameter named $bytes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
146
         * @remark This is influenced by the work of Mike Jolley.
147
         * @see http://mikejolley.com/projects/wp-page-load-stats/
148
         */
149
        private function _convertBytesToHR( $nBytes ) {
150
            $_aUnits = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
151
            $_nLog = log( $nBytes, 1024 );
152
            $_iPower = ( int ) $_nLog;
153
            $_iSize = pow( 1024, $_nLog - $_iPower );
154
            return $_iSize . $_aUnits[ $_iPower ];
155
        }
156
157
}
158