Completed
Branch dev (1e869a)
by
unknown
05:31
created

AdminPageFramework_PageLoadInfo_Base   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A _shouldProceed() 0 8 3
A _replyToSetPageLoadInfoInFooter() 0 1 1
B _replyToGetPageLoadInfo() 0 25 2
B _convertToNumber() 0 18 6
A _convertBytesToHR() 0 7 1
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
     */
71
    public function _replyToGetPageLoadInfo( $sFooterHTML ) {
72
        
73
        if ( self::$_bLoadedPageLoadInfo ) { 
74
            return; 
75
        }
76
        self::$_bLoadedPageLoadInfo = true;     
77
        
78
        $_nSeconds            = timer_stop( 0 );
79
        $_nQueryCount         = get_num_queries();
80
        $_nMemoryUsage        = round( $this->_convertBytesToHR( memory_get_usage() ), 2 );
81
        $_nMemoryPeakUsage    = round( $this->_convertBytesToHR( memory_get_peak_usage() ), 2 );
82
        $_nMemoryLimit        = round( $this->_convertBytesToHR( $this->_convertToNumber( WP_MEMORY_LIMIT ) ), 2 );
83
        $_sInitialMemoryUsage = round( $this->_convertBytesToHR( $this->_nInitialMemoryUsage ), 2 );
84
85
        return $sFooterHTML
86
            . "<div id='admin-page-framework-page-load-stats'>"
87
                . "<ul>"
88
                    . "<li>" . sprintf( $this->oMsg->get( 'queries_in_seconds' ), $_nQueryCount, $_nSeconds ) . "</li>"
89
                    . "<li>" . sprintf( $this->oMsg->get( 'out_of_x_memory_used' ), $_nMemoryUsage, $_nMemoryLimit, round( ( $_nMemoryUsage / $_nMemoryLimit ), 2 ) * 100 . '%' ) . "</li>"
90
                    . "<li>" . sprintf( $this->oMsg->get( 'peak_memory_usage' ), $_nMemoryPeakUsage ) . "</li>"
91
                    . "<li>" . sprintf( $this->oMsg->get( 'initial_memory_usage' ), $_sInitialMemoryUsage ) . "</li>"
92
                . "</ul>"
93
            . "</div>";
94
        
95
    }
96
97
        /**
98
         * Transforms the php.ini notation for numbers (like '2M') to an integer
99
         *
100
         * @access private
101
         * @param $nSize
102
         * @return int
103
         * @remark This is influenced by the work of Mike Jolley.
104
         * @see http://mikejolley.com/projects/wp-page-load-stats/
105
         * @internal
106
         */
107
        private function _convertToNumber( $nSize ) {
108
            
109
            $_nReturn     = substr( $nSize, 0, -1 );
110
            switch( strtoupper( substr( $nSize, -1 ) ) ) {
111
                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...
112
                    $_nReturn *= 1024;
113
                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...
114
                    $_nReturn *= 1024;
115
                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...
116
                    $_nReturn *= 1024;
117
                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...
118
                    $_nReturn *= 1024;
119
                case 'K':
120
                    $_nReturn *= 1024;
121
            }
122
            return $_nReturn;
123
            
124
        }
125
126
        /**
127
         * Converts bytes to HR.
128
         *
129
         * @access private
130
         * @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...
131
         * @remark This is influenced by the work of Mike Jolley.
132
         * @see http://mikejolley.com/projects/wp-page-load-stats/
133
         */
134
        private function _convertBytesToHR( $nBytes ) {
135
            $_aUnits = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
136
            $_nLog = log( $nBytes, 1024 );
137
            $_iPower = ( int ) $_nLog;
138
            $_iSize = pow( 1024, $_nLog - $_iPower );
139
            return $_iSize . $_aUnits[ $_iPower ];
140
        }
141
142
}
143