Passed
Branch dev (8bd4f4)
by Michael
24:22
created

AdminPageFramework_Debug_Utility::getObjectName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2021, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * A utility class for the debug classes.
12
 *
13
 * @since   3.9.0
14
 * @package AdminPageFramework/Common/Utility
15
 */
16
class AdminPageFramework_Debug_Utility extends AdminPageFramework_FrameworkUtility {
17
18
    /**
19
     * Returns an object name if it is an object. Otherwise, the value itself.
20
     * This is used to convert objects into a string in array-walk functions
21
     * as objects tent to get large when they are converted to a string representation.
22
     * @since  3.8.9
23
     * @since  3.8.32  Changed the visibility scope to public from private to be passed as a callback for outside the current class scope.
24
     * And renamed from `___getObjectName()`.
25
     * @since  3.9.0   Moved from `AdminPageFramework_Debug_Base`.
26
     * @param  mixed   $mItem
27
     * @return mixed
28
     */
29
    static public function getObjectName( $mItem ) {
30
        if ( is_object( $mItem ) ) {
31
            return '(object) ' . get_class( $mItem );
32
        }
33
        return $mItem;
34
    }
35
36
    /**
37
     * Slices an array by the given depth.
38
     *
39
     * @since  3.4.4
40
     * @since  3.8.9   Changed it not to convert an object into an array.
41
     * @since  3.8.9   Changed the scope to private.
42
     * @since  3.8.9   Renamed from `getSliceByDepth()`.
43
     * @since  3.8.22  Show a message when truncated by depth. Added the `$sMore` parameter.
44
     * @param  array   $aSubject
45
     * @param  integer $iDepth
46
     * @param  string  $sMore
47
     * @return array
48
     */
49
    static public function getSlicedByDepth( array $aSubject, $iDepth=0, $sMore='(array truncated) ...' ) {
50
51
        foreach ( $aSubject as $_sKey => $_vValue ) {
52
53
            if ( is_array( $_vValue ) ) {
54
55
                $_iDepth = $iDepth;
56
                if ( $iDepth > 0 ) {
57
                    $aSubject[ $_sKey ] = self::getSlicedByDepth( $_vValue, --$iDepth );
58
                    $iDepth = $_iDepth;
59
                    continue;
60
                }
61
62
                if ( strlen( $sMore ) ) {
63
                    $aSubject[ $_sKey ] = $sMore;
64
                    continue;
65
                }
66
                unset( $aSubject[ $_sKey ] );
67
68
            }
69
70
        }
71
        return $aSubject;
72
73
    }
74
75
    /**
76
     * @param  string $sString
77
     * @since  3.8.9
78
     * @since  3.9.0  Changed the visibility scope to public from protected. Moved from `AdminPageFramework_Debug_Base`.
79
     * @return string
80
     */
81
    static public function getArrayRepresentationSanitized( $sString ) {
82
83
        // Fix extra line breaks after `Array()`
84
        $sString = preg_replace(
85
            '/\)(\r\n?|\n)(?=(\r\n?|\n)\s+[\[)])/', // needle
86
            ')', // replacement
87
            $sString // subject
88
        );
89
90
        // Fix empty array output
91
        return preg_replace(
92
            '/Array(\r\n?|\n)\s+\((\r\n?|\n)\s+\)/', // needle
93
            'Array()', // replacement
94
            $sString // subject
95
        );
96
97
    }
98
99
}