getPHPInfo()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 59
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 59
rs 8.2666
c 0
b 0
f 0
cc 7
nc 8
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2022, Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods to return various system information.
12
 *
13
 * @since       3.4.6
14
 * @extends     AdminPageFramework_Utility_File
15
 * @package     AdminPageFramework/Utility
16
 * @internal
17
 */
18
abstract class AdminPageFramework_Utility_SystemInformation extends AdminPageFramework_Utility_File {
19
    
20
    /**
21
     * Caches the result of PHP information array.
22
     * @since       3.4.6
23
     */
24
    static private $_aPHPInfo;
25
    
26
    /**
27
     * Returns the PHP information as an array.
28
     * 
29
     * @since       3.4.6
30
     */
31
    static public function getPHPInfo() {
32
33
        if ( isset( self::$_aPHPInfo ) ) {
34
            return self::$_aPHPInfo;
35
        }
36
    
37
        ob_start();
38
        phpinfo( -1 );
39
40
        $_sOutput = preg_replace(
41
            array(
42
                '#^.*<body>(.*)</body>.*$#ms', '#<h2>PHP License</h2>.*$#ms',
43
                '#<h1>Configuration</h1>#',  "#\r?\n#", "#</(h1|h2|h3|tr)>#", '# +<#',
44
                "#[ \t]+#", '#&nbsp;#', '#  +#', '# class=".*?"#', '%&#039;%',
45
                '#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>'
46
                    .'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',
47
                '#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',
48
                '#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',
49
                "# +#",
50
                '#<tr>#',
51
                '#</tr>#'
52
            ),
53
            array(
54
                '$1', '', '', '', '</$1>' . "\n", '<', ' ', ' ', ' ', '', ' ',
55
                '<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'.
56
                "\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',
57
                '<tr><td>PHP Credits Egg</td><td>$1</td></tr>',
58
                '<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" . '<tr><td>Zend Egg</td><td>$1</td></tr>',
59
                ' ',
60
                '%S%',
61
                '%E%'
62
            ),
63
            ob_get_clean()
64
        );
65
66
        $_aSections = explode( '<h2>', strip_tags( $_sOutput, '<h2><th><td>' ) );
67
        unset( $_aSections[ 0 ] );
68
69
        $_aOutput = array();
70
        foreach( $_aSections as $_sSection ) {
71
            $_iIndex = substr( $_sSection, 0, strpos( $_sSection, '</h2>' ) );
72
            preg_match_all(
73
                '#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',
74
                $_sSection, 
75
                $_aAskApache, 
76
                PREG_SET_ORDER
77
            );
78
            foreach( $_aAskApache as $_aMatches ) {
79
                if ( ! isset( $_aMatches[ 1 ], $_aMatches[ 2 ] ) ) {
80
                    array_slice( $_aMatches, 2 );
81
                    continue;
82
                }
83
                $_aOutput[ $_iIndex ][ $_aMatches[ 1 ] ] = ! isset( $_aMatches[ 3 ] ) || $_aMatches[ 2 ] == $_aMatches[ 3 ]
84
                    ? $_aMatches[ 2 ] 
85
                    : array_slice( $_aMatches, 2 );
86
            }
87
        }
88
        self::$_aPHPInfo = $_aOutput;
89
        return self::$_aPHPInfo;   
90
    
91
    }  
92
            
93
    /**
94
     * Returns an array of constants.
95
     * 
96
     * @since       3.4.6
97
     * @param       array|string      $asCategory      The category key names of the returning array.
98
     */
99
    static public function getDefinedConstants( $asCategories=null, $asRemovingCategories=null ) {
100
        
101
        $_aCategories           = is_array( $asCategories ) ? $asCategories : array( $asCategories );
102
        $_aCategories           = array_filter( $_aCategories );
103
        $_aRemovingCategories   = is_array( $asRemovingCategories ) ? $asRemovingCategories : array( $asRemovingCategories );
104
        $_aRemovingCategories   = array_filter( $_aRemovingCategories );
105
        $_aConstants            = get_defined_constants( true );
106
        
107
        if ( empty( $_aCategories ) ) {
108
            return self::dropElementsByKey( $_aConstants, $_aRemovingCategories );
109
        }
110
        return self::dropElementsByKey( 
111
            array_intersect_key( $_aConstants, array_flip( $_aCategories ) ),
112
            $_aRemovingCategories
113
        );
114
                
115
    }        
116
        
117
    /**
118
     * Returns PHP error log path.
119
     * 
120
     * @since       3.4.6
121
     * @return      array|string        The error log path. It can be multiple. If so an array holding them will be returned.
122
     */
123
    static public function getPHPErrorLogPath() {
124
                
125
        $_aPHPInfo = self::getPHPInfo();
126
        return isset( $_aPHPInfo['PHP Core']['error_log'] ) 
127
            ? $_aPHPInfo['PHP Core']['error_log']
128
            : '';
129
        
130
    }
131
    
132
    /**
133
     * Returns a PHP error log.
134
     * @since       3.4.6
135
     */
136
    static public function getPHPErrorLog( $iLines=1 ) {
137
        
138
        $_sLog = self::getFileTailContents( self::getPHPErrorLogPath(), $iLines );
139
        
140
        // If empty, it could be the log file could not be located. In that case, return the last error.
141
        return $_sLog
142
            ? $_sLog
143
            : print_r( @error_get_last(), true );   
144
        
145
    }        
146
        
147
}
148